C# 无法解析类型为';Microsoft.AspNetCore.Hosting.IHostingEnvironment';
在我的MVC应用程序中,我从RabbitMQ队列中获取一个事件。开始处理事件C# 无法解析类型为';Microsoft.AspNetCore.Hosting.IHostingEnvironment';,c#,rabbitmq,C#,Rabbitmq,在我的MVC应用程序中,我从RabbitMQ队列中获取一个事件。开始处理事件 public void Consume() { ConnectionFactory factory = new ConnectionFactory { //.. }; IConnection connection = factory.CreateConnection(); IModel channel = connection.CreateModel();
public void Consume()
{
ConnectionFactory factory = new ConnectionFactory
{
//..
};
IConnection connection = factory.CreateConnection();
IModel channel = connection.CreateModel();
channel.QueueDeclare("MyQueueName", true, false, false, null);
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
consumer.Received += OnReceived;
channel.BasicConsume("MyQueueName", true, consumer);
}
来处理我使用的事件
private async void OnReceived(object model, BasicDeliverEventArgs deliverEventArgs)
{
var info = Encoding.UTF8.GetString(deliverEventArgs.Body);
var model = JsonConvert.DeserializeObject<MyModel>(info);
if (model != null)
{
await _groupServiceProvider.GetService<IMyService>().ProcessEvent(model);
}
}
不幸的是,当我运行我的应用程序时
尝试激活“MyProject.MyService”时,无法解析类型为“Microsoft.AspNetCore.Hosting.IHostingEnvironment”的服务
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.PopulateCallSites(ServiceProvider提供程序,ISet1 callSiteChain,ParameterInfo[]参数,Boolean throwIfCallSiteNotFound)
位于Microsoft.Extensions.DependencyInjection.ServiceLookup.Service.CreateCallSite(ServiceProvider provider,ISet
1 callSiteChain)
位于Microsoft.Extensions.DependencyInjection.ServiceProvider.GetResolveCallSite(iSeries服务,ISet1 callSiteChain)
在Microsoft.Extensions.DependencyInjection.ServiceProvider.GetServiceCallSite(输入serviceType,ISet
1 callSiteChain)
位于Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(类型serviceType,ServiceProvider ServiceProvider)
在System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary2 dictionary,TKey,Func
3 valueFactory,TArg arg)
位于Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型serviceType)
位于Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider提供程序)
在..\MyService\Consumers.cs:line中的MyService.Consumerd\uu 3.MoveNext()处
有人能解释一下我如何解决依赖关系吗
编辑
要注入依赖项,我使用Microsoft.Extensions.DependencyInjection.ServiceCollection
\u groupServiceProvider=newservicecolection()
.AddAutoMapper(类型(AutoMapperProfile))
.AddSingleton()
.AddSingleton()
//..
.BuildServiceProvider();
您已将DI容器设置为为IMyService
提供服务:例如,当调用它时,它会尝试创建一个实例:\u groupServiceProvider.GetService
。问题是您还没有为依赖项注入而连接的IHostingEnvironment
。这是什么版本的MVC,您使用的是什么DI容器?谢谢您的回答。我使用NetCore 1.1.2。要注入依赖项,我使用Microsoft.Extensions.DependencyInjection.ServiceCollection(请参见下面的编辑)以及如何注册IMyService
?您也可以添加该代码吗?这与IHostingEnvironment的方法相同。我刚刚将.AddSingleton()
添加到ServiceCollection()中
public class MyService : IMyService
{
private readonly IHostingEnvironment _env;
public MyService(IHostingEnvironment env)
{
_env = env;
}
public async Task ProcessEvent(MyModel model)
{
//.. process model
}
}
_groupServiceProvider = new ServiceCollection()
.AddAutoMapper(typeof(AutoMapperProfile))
.AddSingleton<IMyService, MyService>()
.AddSingleton<IHostingEnvironment, HostingEnvironment>()
//..
.BuildServiceProvider();