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")));
});
}