命名空间Microsoft.Extensions中不存在缓存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了命名空间Microsoft.Extensions中不存在缓存相关的知识,希望对你有一定的参考价值。
我正在将一些逻辑从微服务转移到天蓝色的功能。
事情进展顺利,但我遇到了依赖问题。我在微服务中有一些缓存功能,我想保留在azure函数中,但函数项目不会构建,因为它无法解析Microsoft.Extensions.Caching.Memory
命名空间。
我在新的功能项目中通过nuget安装了Microsoft.Extensions.DependencyInjection
,这样我就可以在这个功能应用程序中使用DI。我认为这就是为什么Microsoft.Extensions.Caching
不再解决的原因。
错误是常见的:The type or namespace 'Caching' does not exist in the namespace 'Microsoft.Extensions'
它们不应该在Microsoft.Extensions命名空间下公开吗?有谁知道如何解决这个问题?
谢谢
编辑:一些代码示例。
功能代码:(DI设置几乎完全是this)
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using PI.Warehouses.API.Repositories;
using ESPIWarehouseFunction.Injection;
namespace ESPIWarehouseFunction
{
public static class WarehouseFunctions
{
[FunctionName("GetAll")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req,
TraceWriter log, [Inject(typeof(IWarehouseStateRepository))]IWarehouseStateRepository repoW)
{
log.Info("GetAll Function is processing a request");
var warehouses = repoW.GetAllWarehouses();
return warehouses != null ?
(ActionResult)new OkObjectResult(warehouses) :
new BadRequestObjectResult("Something went wrong with this request");
}
}
}
使用缓存的众多代码之一:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using MediatR;
using PI.Warehouses.Domain.Aggregate.Commands;
using PI.Warehouses.Domain.Aggregate.Events;
using PI.Warehouses.Domain.Model;
using PI.Warehouses.API.Repositories;
using Newtonsoft.Json;
using PI.Warehouses.Domain.Aggregate.Exceptions;
using Microsoft.Extensions.Caching.Memory;
namespace PI.Warehouses.API.Application.Handlers.Commands
{
public class AddMaterialHandler : IRequestHandler<AddMaterialCommand, EventList>
{
private readonly IEventRepository _repoEvents;
private readonly IWarehouseStateRepository _repoWarehouse;
private readonly IMediator _mediator;
private IMemoryCache _cache;
private EventList _eventsCreated;
public AddMaterialHandler(IEventRepository repoE, IWarehouseStateRepository repoW, IMediator mediator, IMemoryCache cache)
{
_repoEvents = repoE;
_repoWarehouse = repoW;
_mediator = mediator;
_cache = cache;
_eventsCreated = new EventList();
}
// some setups stuffs ...
WarehouseState state;
if (!_cache.TryGetValue(cmd.WarehouseId, out WarehouseState cachedState))
state = await _repoWarehouse.GetWarehouseState(cmd.WarehouseId);
else
state = new WarehouseState(cachedState);
// Rest of this handler
}
}
我刚刚创建了一个干净的函数并安装了两个软件包,我使用了它们的接口(所以我都使用了它们)并且所有看起来都正常工作:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
public class MyCache : IMemoryCache
{
public void Dispose()
{
throw new System.NotImplementedException();
}
public bool TryGetValue(object key, out object value)
{
throw new System.NotImplementedException();
}
public ICacheEntry CreateEntry(object key)
{
throw new System.NotImplementedException();
}
public void Remove(object key)
{
throw new System.NotImplementedException();
}
}
public class ServicesCollection : IServiceCollection
{
public IEnumerator<ServiceDescriptor> GetEnumerator()
{
throw new System.NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(ServiceDescriptor item)
{
throw new System.NotImplementedException();
}
public void Clear()
{
throw new System.NotImplementedException();
}
public bool Contains(ServiceDescriptor item)
{
throw new System.NotImplementedException();
}
public void CopyTo(ServiceDescriptor[] array, int arrayIndex)
{
throw new System.NotImplementedException();
}
public bool Remove(ServiceDescriptor item)
{
throw new System.NotImplementedException();
}
public int Count { get; }
public bool IsReadOnly { get; }
public int IndexOf(ServiceDescriptor item)
{
throw new System.NotImplementedException();
}
public void Insert(int index, ServiceDescriptor item)
{
throw new System.NotImplementedException();
}
public void RemoveAt(int index)
{
throw new System.NotImplementedException();
}
public ServiceDescriptor this[int index]
{
get => throw new System.NotImplementedException();
set => throw new System.NotImplementedException();
}
}
}
}
如果没有代码中的示例,则很难诊断出真正的问题。
编辑:这是我的.csproj
项目:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.14" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
以上是关于命名空间Microsoft.Extensions中不存在缓存的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UNO 平台应用程序中使用 Microsoft.Extensions.DependencyInjection
解析 Microsoft.Extensions.DependencyInjection 2.x 版本实现
Microsoft.Extensions.Configuration 的 reloadOnChange 如何为 appsettings.json 工作