I'm trying to register (as Instance per Request) the generic type and non-generic interface. On internet I have found a lot of opposite examples, but none for this.
Thus, I have a class like this:
public class UnitOfWork<TContext> : IUnitOfWork where TContext : DbContext, new()
The generic class, but non-generic interface. In Autofac I was trying to register that this way:
builder.RegisterGeneric(typeof(UnitOfWork<>)).As<IUnitOfWork>().InstancePerHttpRequest();
But I'm getting error: The service 'Namespace.UoW.IUnitOfWork' is not an open generic type definition.
Of course, because it is not. But I don't know how to register it.
Thanks.
You only need RegisterGeneric if you also have a generic interface.
In your case you only have a non generic interface IUnitOfWork and also only one DbContext derived class so you can just register your UnitOfWork<Context> as a normal type with:
builder.RegisterType<UnitOfWork<Context>>()
.As<IUnitOfWork>().InstancePerHttpRequest();
So any of your types requesting an IUnitOfWork will get an UnitOfWork<Context>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With