相关疑难解决方法(0)

如何在Asp.Net Core中注册同一接口的多个实现?

我有来自相同接口的服务

public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { } 
public class ServiceC : IService { }
Run Code Online (Sandbox Code Playgroud)

通常,其他IOC容器Unity允许您通过某些Key区分它们来注册具体实现.

在Asp.Net Core中,我如何注册这些服务并在运行时根据某些键解决它?

我没有看到任何通常用于区分具体实现的AddService方法keyname参数.

    public void ConfigureServices(IServiceCollection services)
    {            
         // How do I register services here of the same interface            
    }


    public MyController:Controller
    {
       public void DoSomeThing(string key)
       { 
          // How do get service based on key
       }
    }
Run Code Online (Sandbox Code Playgroud)

工厂模式是唯一的选择吗?

Update1
我已经阅读了这里 …

c# asp.net-core-mvc coreclr asp.net-core

183
推荐指数
21
解决办法
9万
查看次数

使用DI和IoC的工厂方法

我熟悉这些模式,但仍然不知道如何处理以下情况:

public class CarFactory
{
     public CarFactory(Dep1,Dep2,Dep3,Dep4,Dep5,Dep6)
     {
     }

     public ICar CreateCar(type)
     {
            switch(type)
            {
               case A:
                   return new Car1(Dep1,Dep2,Dep3);
               break;

               case B:
                   return new Car2(Dep4,Dep5,Dep6);
               break;
            }
     }
}
Run Code Online (Sandbox Code Playgroud)

通常,问题在于需要注入的引用量.当有更多的汽车时会更糟.

我想到的第一种方法是在工厂构造函数中注入Car1和Car2,但它违反工厂方法,因为工厂将始终返回相同的对象.第二种方法是注入servicelocator,但它的反模式到处都是.怎么解决?

编辑:

替代方式1:

public class CarFactory
{
     public CarFactory(IContainer container)
     {
        _container = container;
     }

     public ICar CreateCar(type)
     {
            switch(type)
            {
               case A:
                   return _container.Resolve<ICar1>();
               break;

               case B:
                     return _container.Resolve<ICar2>();
               break;
            }
     }
}
Run Code Online (Sandbox Code Playgroud)

替代方式2(由于树中的依赖性过多而难以使用):

public class CarFactory
{
     public CarFactory()
     {
     }

     public ICar CreateCar(type)
     {
            switch(type)
            { …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection inversion-of-control factory-pattern

36
推荐指数
2
解决办法
3万
查看次数

从静态工厂类访问ASP.NET Core DI容器

我创建了一个ASP.NET Core MVC/WebApi站点,该站点有一个RabbitMQ订阅者,基于James Still的博客文章Real-World PubSub Messaging with RabbitMQ.

在他的文章中,他使用静态类来启动队列订阅者并为排队事件定义事件处理程序.然后,此静态方法通过静态工厂类实例化事件处理程序类.

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace NST.Web.MessageProcessing
{
    public static class MessageListener
    {
        private static IConnection _connection;
        private static IModel _channel;

        public static void Start(string hostName, string userName, string password, int port)
        {
            var factory = new ConnectionFactory
            {
                HostName = hostName,
                Port = port,
                UserName = userName,
                Password = password,
                VirtualHost = "/",
                AutomaticRecoveryEnabled = true,
                NetworkRecoveryInterval = TimeSpan.FromSeconds(15)
            };

            _connection = factory.CreateConnection();
            _channel = …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection rabbitmq service-locator asp.net-core

29
推荐指数
4
解决办法
3万
查看次数

C#.Net核心依赖注入,为构造函数注入多个参数

我有一个AuthenticationStrategy类,我将在控制器构造函数中注入.

我有两个IAuthenticationProviders:InternalAuthenticationProviderExternalAuthenticationProvider.
AuthenticationStrategy构造函数中,我想注入所有提供者.
示例代码:

public class AuthenticationStrategy
{
    private readonly Dictionary<string, IAuthenticationProvider> _authenticationProviders;

    public AuthenticationStrategy(IAuthenticationProvider[] authenticationProviders)
    {
        if (authenticationProviders == null)
        {
            throw new ArgumentNullException("AuthenticationProviders");
        }

        _authenticationProviders = authenticationProviders
            .ToDictionary(x => nameof(x), x => x);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用依赖注入注入多个提供程序?示例代码:

services.AddScoped<IAuthenticationProvider, InternalAuthenticationProvider>();
services.AddScoped<IAuthenticationProvider, ExternalAuthenticationProvider>();
services.AddScoped<AuthenticationStrategy>();
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

.net c# constructor dependency-injection .net-core

5
推荐指数
1
解决办法
2845
查看次数