代码之家  ›  专栏  ›  技术社区  ›  Mohammad Reza Mrg

system.argumentexception:'不支持服务类型icommand。参数名称:serviceType

  •  0
  • Mohammad Reza Mrg  · 技术社区  · 7 年前

    我想用 Belgrade-SqlClient (Small async ADO.NET helper library)

    在C Web API中 一切都很好,但当我尝试将icommand注册为服务时

    我搞错了:

    System.ArgumentException: 'The service type ICommand is not supported. Parameter name: serviceType
    

    这是我的webapiconfig.cs,我在其中添加icommand作为服务:

    string Conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; ;
            config.Services.Add(typeof(ICommand), new Command(Conn));
    

    这是我的命令:

    using System.Threading.Tasks;
    
    namespace Belgrade.SqlClient
    {
        public interface ICommand : IQueryMapper, IQueryPipe
        {
            Task Exec();
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   popsiporkkanaa    7 年前

    我想你在用 配置服务 作为一个IServiceCollection。那么你可能需要使用 附加瞬态 ,就像这样:

    config.Services.AddTransient<ICommand>(() => new Command(Conn))