1

How to register in a good way one Autofac dependency resolver or resolvers if one not possible for Asp.net MVC, WebApi, SignalR working together with Owin? There are guidelines for each of them. But as stated below it does not seem to work. Here is the code which is somewhat bad as it uses different dependency resolves, someones static and they seem to have one reason to exists (so looks like code duplication).

public class Startup
{
    // This two static resolvers does not look nice
    public static IDependencyResolver SignalRDependencyResolver { get; private set; }

    public static System.Web.Http.Dependencies.IDependencyResolver WebApiDependencyResolver { get; private set; }

    public void Configuration(IAppBuilder app)
    {
        var httpConfiguration = new HttpConfiguration();

        var container = BuildAutofacContainer();

        var hubConfiguration =
            new HubConfiguration
            {
                Resolver = new AutofacDependencyResolver(container),
                EnableDetailedErrors = true
            };
        // The resolver to be used as here. Seems to be replaced by SignalR further? 
        // 1. http://stackoverflow.com/questions/20139127/signalr-sending-data-using-globalhost-connectionmanager-not-working/20202040#20202040
        // 2. http://stackoverflow.com/questions/20561196/signalr-calling-client-method-from-outside-hub-using-globalhost-connectionmanage
        SignalRDependencyResolver = hubConfiguration.Resolver;

        DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));

        WebApiDependencyResolver = new AutofacWebApiDependencyResolver(container);

        // Why the following does not work (throws that needs parameterless constructor) ? 
        // so the static resolver used
        // see http://docs.autofac.org/en/latest/integration/webapi.html#owin-integration
        // httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(httpConfiguration);
        app.UseAutofacMvc();
        app.UseWebApi(httpConfiguration);
        app.MapSignalR("/signalr", hubConfiguration);

        // AspNetIdentity hook:
        app.UseCookieAuthentication(
            new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
            });
    }

    private static IContainer BuildAutofacContainer()
    {
        var builder = new ContainerBuilder();

        // and http://autofac.readthedocs.org/en/latest/integration/mvc.html#using-plugin-assemblies 
        builder.RegisterModule<AutofacDalModule>();
        builder.RegisterModule<AutofacDomainModule>();
        builder.RegisterType<OperatorHubInternal>().As<IOperatorHubInternal>().SingleInstance();
        RegistrationExtensions.RegisterControllers(builder, typeof(MvcApplication).Assembly);
        builder.RegisterApiControllers(typeof(MvcApplication).Assembly).InstancePerRequest();
        builder.RegisterHubs(Assembly.GetExecutingAssembly());

        var mvcContainer = builder.Build();
        return mvcContainer;
    }
}
Artyom
  • 3,507
  • 2
  • 34
  • 67
  • 1
    Web API and MVC *have* different dependency resolvers - there's nothing you can do about that. What you can do is what you've done - register each resolver against the same container. What makes you say that's 'bad'? – Ant P Aug 01 '15 at 15:14
  • @AntP It looks bad as 1) there are two `static` fields, those resolvers which used by some controllers and hubs so that they can not be easily tested; 2) the initialization of Owin does not look good as the order should be kept; 3) there should be different resolvers for those parts and it is not clear why not use one resolver. – Artyom Aug 06 '15 at 09:35

0 Answers0