1

Consider 'mixed' application that consists of couple asp.net web-form, several asp.net mvc controllers, fistful of web.api controller, a bit of web-enabled wcf services and ofcourse unit-tests. These are all 'input-points' that accept requests. Now consider each for each 'input-type' I have a call-chain that ends calling method of the following 'service':

class SomeService: IService // not Wcf or Web service, just BL
{
    private readonly IDependency1 dependency1;
    public SomeService(IDependency1 dependency1)
    {
        this.dependency1 = dependency1;
    }
    private Anything DoSomething()
    {
        var result1 = this.dependecy1.GetSomeData();
        return DoSomeFancyCalculations(result1);
    }
}

public interface IDependency1
{
    string GetSomeData();
}

I register this service once on Application_Start method as 'per-dependency'. Nothing interesting. However now I want that IDependency returns different data based on the context:

  • webform -> "<asp:Net_WebForm />"
  • mvc -> '@{ asp.net = "mvc" }'
  • webapi -> "ipabew"
  • wcf -> '{ d: 'wcf' }'
  • unit test -> 'Assert.False("i was not tested")'

For unit tests I can make separate single registration, but for other contexts decition should be made automagically. I can only think of using keyed services and additional factory that will detect current context and resolve dependency with appropriate key:

public static RequestContext DetectContext()
{
     if (null != OperationContext.Current)
     {
          return RequestContext.Wcf;
     }
     ...
}

builder.Register(
    c =>
    {
        var context = DetectContext();
        return c.ResolveKeyed<IDependency1>(context);
    }

Is there any autofac magic that can do this? It would be nice if i could write smth like this:

builder
    .RegisterType<WebFormDependency>()
    .As<IDependency1>()
    .ForWebForms()
koruyucu
  • 190
  • 1
  • 9
  • 1
    So you are having both MVC Web API and WCF run within the same app domain? Might this be the root of your troubles? – Steven Jul 29 '15 at 12:43
  • Or at least, if you'd give each technology stack its own container instance, the problem will already be much easier to solve. – Steven Jul 29 '15 at 14:41
  • Yes, they all run in the same WP and it is ok as long as i dont have dependency on 'technology'-specific features (like HttpContext, OperationContext or TestContext for unit tests). So the question is about how to differentiate this technology-specific features using autofac. – koruyucu Jul 30 '15 at 04:53
  • Related: https://stackoverflow.com/questions/15494920/what-is-the-difference-between-dependencyresolver-setresolver-and-httpconfigurat/15495934#15495934 – Steven Jul 30 '15 at 08:58
  • You are right but wrong =) I can implement some part of an 'application' as mvc views, but then if another part (module, plugin etc) of an application fits in SPA then I need webapi/wcf to support it. And autofac supports already all these 'context' perfectly. – koruyucu Jul 30 '15 at 09:46

1 Answers1

0

There is a whole FAQ with detailed examples and options on the Autofac site about this sort of thing: How do I pick a service implementation by context?

Travis Illig
  • 23,195
  • 2
  • 62
  • 85