Resolving dependency on data model using Autofac
I have run into an issue while creating a data service and using Autofac WCF Integration to resolve a dependency on my data model. Registrations are of the form:
builder.RegisterType<MyService>()
.InstancePerDependency();
builder.RegisterType<MyModel>()
.InstancePerLifetimeScope();
where MyModel has a dependency on 开发者_StackOverflow中文版MyProvider
public MyModel (MyProvider provider)
{
_provider = provider;
}
The problem arises as this provider is registered in Request scope for reasons pertinent to my application.
builder.RegisterType<MyProvider>()
.As<MyProvider>()
.InstancePerMatchingLifetimeScope(RequestContextTag);
As might be obvious, request container is created and disposed on each ASP.Net request.
However, MyModel and MyService are registered in Application scope. I came up with two possible solutions -
- Change scope of provider (not possible as will have to remodel almost the entire app)
- Register service and model in request scope (Don't know if this is possible and if at all, correct)
Any advice/ suggestions appreciated. Thanks.
I guess by your statement "registered in Request scope" that you mean the ASP.NET "request" scope as configured via ContainerProvider
? (If not please clarify.)
The request lifetime is only seen by ASP.NET requests, and not WCF requests - WCF uses a different pipeline.
There is probably a way to achieve what you want so please post some details of the MyProvider
registration.
Make sure your per-request component doesn't depend on any of the ASP.NET "Http" types, which will not be available during a WCF request regardless of whether Autofac is used or not.
Hope this helps!
Nick
精彩评论