I can't get my Func to pass across successfully. I inject into the webapi in my startup.cs
services.AddDbContext<MyDataContext>(
options => options.UseSqlServer(DbGlobals.DevDatabase,
b => b.MigrationsAssembly("Services.Data.EF")));
services.AddTransient<Func<IMyDataContext>, Func<MyDataContext>>();
services.AddTransient(provider => new Func<IMyDataContext>(() => provider.GetService<IMyDataContext>()));
Then in my controller I have the following;
private ClientService _service;
public ClientController(Func<IMyDataContext> context)
{
_service = new ClientService(context);
}
and the method in my service is;
private readonly Func<IMyDataContext> _contextFactory;
public ClientService(Func<IMyDataContext> contextFactory)
{
_contextFactory = contextFactory;
}
public void AddClient(Client model, string userName)
{
Func<Client> func = delegate
{
using (var context = _contextFactory())
{
....
}
};
return this.Execute(func);
}
Now from debugging if I inspect the controller injection I can see the Func is passed in and is at the service level 2. However when it comes to use it in the using statement on context = _contextFactory() it becomes null?
Any ideas what I am doing wrong here please?
context == nullor_contextFactory == null?Func<...>for your DataContext but I would assume your function does not generate a context.