开发者

Resolving dependency on data model using Autofac

开发者 https://www.devze.com 2023-01-04 13:25 出处:网络
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:

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 -

  1. Change scope of provider (not possible as will have to remodel almost the entire app)
  2. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消