Most of the examples online deal with asp.net and register their DbContext as part of their startup service registry.
I tried registering my DbContext like this
builder.RegisterType<MyContext>()
.As<MyContext>()
.InstancePerLifetimeScope();
builder.RegisterType<DealRepository>()
.Keyed<IRepository<Deal>>(FiberModule.Key_DoNotSerialize)
.As<IRepository<Deal>>()
.SingleInstance();
builder.RegisterType<CardsDialog>()
.As<IDialog<object>>()
.InstancePerDependency();
But I'm getting this error
Inheritance security rules violated by type: 'System.Net.Http.WebRequestHandler'. Derived types must either match the security accessibility of the base type or be less accessible.
It's even more complicated as the actual MessageController.cs creates a new scope on Post
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var dialog = scope.Resolve<IDialog<object>>();
await Conversation.SendAsync(activity, () => dialog);
}
How should the registering be done?
EDIT:
As suggested, using InstancePerRequest solved the problem. But I also have a Quartz jobs that runs every X seconds that also needs a repository.
builder.RegisterType<DealJob>()
.AsSelf()
.SingleInstance();
Unable to resolve the type 'BargainBot.Repositories.MyContext' because the lifetime scope it belongs in can't be located. The following services are exposed by this registration: - BargainBot.Repositories.MyContext
Details ---> No scope with a tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. If you see this during execution of a web application, it generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance()
Should I resolve a new DbContext manually at this point? Or maybe I should change the my repo's life cycle?
Edit2: Looks like I'm still getting this error even when removing the entire Quartz job registration.
ConfigureServicesmethod inStartup.cs? Something likeservices.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));And add your Repository there as well:services.AddScoped<IDealRepository, DealRepository>();- kimbaudiStartup.csas I'm not in a asp.net context. This is in a MicrosoftBot Framework application. I'll clarify in the original question. - martinni39