views:

65

answers:

1

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 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.

A: 

Hi,

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

Nicholas Blumhardt
Hey Nick, thanks for your pointers. Your interpretation of request scope is correct. I was not aware of WCF pipeline being different than that of ASP.Net. Question has been updated with the registration details of MyProvider.This leads me to one probable option of creating another scoped container for WCF and registering the provider, along with its dependencies, in that. I would be happy to know about other options that might exist.
Nitesh Baranwal
The WCF integration already creates a scope-per-service-instance, so as long as your service class is per-call you should be able to get away with declaring MyProvider as InstancePerLifetimeScope().
Nicholas Blumhardt