Cannot convert lambda expression to type 'InjectionMember[]' because it is not a delegate type. - How can use DI in MVC application [duplicate]

我只是一个虾纸丫 提交于 2019-12-12 00:47:55

问题


I have written asp.net core application and used below code for dependency injection, which is working fine as expected.

services.AddScoped<IDeviceRepository<Device>>(factory =>
{
    return new DeviceRepository<Device>(
        new AzureTableSettings(
            storageAccount: Configuration.GetConnectionString("Table_StorageAccount"),
            storageKey: Configuration.GetConnectionString("Table_StorageKey"),
            tableName: Configuration.GetConnectionString("Table_TableName")));
});

Due to some reason I am moving back to ASP.NET MVC application, and I have to use 3rd party library for dependency injection. So I have used Unity framework.

container.RegisterType<IDeviceRepository<Device>>(factory =>
{
    return new DeviceRepository<Device>(
        new AzureTableSettings(
            storageAccount: Configuration.GetConnectionString("Table_StorageAccount"),
            storageKey: Configuration.GetConnectionString("Table_StorageKey"),
            tableName: Configuration.GetConnectionString("Table_TableName")));
});

But, I am getting error as follow

Severity Code Description Project File Suppression State Line Error CS1660 Cannot convert lambda expression to type 'InjectionMember[]' because it is not a delegate type \Presentation\Web\App_Start\UnityConfig.cs Active 60

Here is my DeviceRepository Class

public class DeviceRepository<T>: IDeviceRepository<T>
where T : TableEntity, new()
{
    ...
}

My AzureTableSettings class

public class AzureTableSettings
{
    public AzureTableSettings(string storageAccount,
                                   string storageKey,
                                   string tableName)
    {
        if (string.IsNullOrEmpty(storageAccount))
            throw new ArgumentNullException("StorageAccount");

        if (string.IsNullOrEmpty(storageKey))
            throw new ArgumentNullException("StorageKey");

        if (string.IsNullOrEmpty(tableName))
            throw new ArgumentNullException("TableName");

        this.StorageAccount = storageAccount;
        this.StorageKey = storageKey;
        this.TableName = tableName;
    }

    public string StorageAccount { get; }
    public string StorageKey { get; }
    public string TableName { get; }
}

How can use Dependency Injection in ASP.NET MVC application for this type of class?

Issue Resolved and here is the Solution

First register the type as follow

     container.RegisterType<IDeviceRepository<Device>>(
        new InjectionFactory(c => CreateDeviceRepository()));

Here is the CreateDeviceRepository method

       public static IDeviceRepository<Device> CreateDeviceRepository()
       {
             return new DeviceRepository<Device>(
                    new AzureTableSettings(
               storageAccount:Configuration.GetConnectionString("Table_StorageAccount"),
        storageKey: Configuration.GetConnectionString("Table_StorageKey"),
        tableName: Configuration.GetConnectionString("Table_TableName")));
                });
      }

回答1:


For my understanding, the error tells you to inherit from InjectionMember:

public class DeviceRepository<T>: InjectionMember, IDeviceRepository<T>
where T : TableEntity, new()
{
    ...
}


来源:https://stackoverflow.com/questions/50043786/cannot-convert-lambda-expression-to-type-injectionmember-because-it-is-not-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!