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

时间:2018-07-25 04:47:47

标签: c# asp.net-web-api2 httpconfiguration

i'm trying to use Belgrade-SqlClient (Small async ADO.NET helper library)

in c# web api all is fine but when i try to register Icommand as a service

i got error:

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

this is my webapiconfig.cs where i add ICommand as service:

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

and this is Icommand:

using System.Threading.Tasks;

namespace Belgrade.SqlClient
{
    public interface ICommand : IQueryMapper, IQueryPipe
    {
        Task Exec();
    }
}

1 个答案:

答案 0 :(得分:0)

I suppose your you are using config.Services as an IServiceCollection. Then you may need to use AddTransient, like this:

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