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

1

0

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")));
                });
      }
Previous answered 26/4, 2018 at 13:6 Comment(3)
How it's duplicate ?Previous
It is a duplicate, because the referenced question describes exactly how to create a lambda-based registration in Unity.Mariandi
Issue resolved! I would update the answerPrevious
E
-1

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

public class DeviceRepository<T>: InjectionMember, IDeviceRepository<T>
where T : TableEntity, new()
{
    ...
}
Eclipse answered 26/4, 2018 at 13:11 Comment(2)
InjectionMember is part of Unity and adding it's reference in repository make it tightly coupled. I wish to make changes only in web application, not in repositories or domain classes. I suspect it is syntax issue.Previous
Have a look to the docs. You need to implement InjectionMember for UnityContainer.RegisterType: msdn.microsoft.com/en-us/library/…Eclipse

© 2022 - 2024 — McMap. All rights reserved.