Autofac 与移动 Azure 服务集成

Autofac integration with Mobile Azure Service

我已经创建了移动 Azure 服务项目并将其与 Autofac 集成。

这是我的IocConfig.cs,我已经在其中注册了所有Api控制器和库。

var containerBuilder = new ContainerBuilder();

//Register API controllers
containerBuilder.RegisterApiControllers(typeof(UserController).Assembly);

/*Register Libs*/
containerBuilder.RegisterType<UserLib>().As<IUserLib>();
/*Register ObjectContexts*/
containerBuilder.RegisterType<MobileServiceContext>()
                .As<DbContext>().InstancePerDependency();

var container = containerBuilder.Build();
// var resolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

//For Web API dependency resolver
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;

return container;

但是 Api 由于某些问题没有调用并抛出错误 Controller is not registered

如果我把代码放在下面,它可以正常工作,但它是一个简单的依赖项:

var options = new ConfigOptions(); 
var builder = new ConfigBuilder(options, (httpConfig, autofac) => 
    autofac.RegisterInstance(new UserLib(new MobileServiceContext())).As<IUserLib>());

HttpConfiguration config = ServiceConfig.Initialize(builder);

这里是WebApiConfig ,其中ServiceConfigurations已经完成。

public static class WebApiConfig
{
    public static void Register()
    {
        // Use this class to set configuration options for your mobile service
        ConfigOptions options = new ConfigOptions();

        // Use this class to set WebAPI configuration options
        HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options));

        // To display errors in the browser during development, uncomment the following
        // line. Comment it out again when you deploy your service for production use.
        // config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

        Database.SetInitializer(new CreateDatabaseIfNotExists<MobileServiceContext>());
        //Database.SetInitializer(new MobileServiceInitializer());
    }
}

调用IocConfig方法的代码:

protected void Application_Start()
{
    WebApiConfig.Register(); 
    IocConfig.RegisterDependencies(); 
    DatabaseConfig.InitializeDatabase();
}

我已经更改了相关服务的 Ioc 配置。现在正在运行。

public static void RegisterDependencies()
    {
        try
        {
            // Use this class to set configuration options for your mobile service
            ConfigOptions options = new ConfigOptions();

            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options, (configuration, builder) =>
            {
                //Register API controllers
                builder.RegisterApiControllers(typeof(UserController).Assembly);

                /*Register Libs*/
                builder.RegisterType<UserLib>().As<IUserLib>();

                /*Register ObjectContexts*/
                builder.RegisterType<MobileServiceContext>().As<DbContext>().InstancePerDependency();

                /*Register DataRepository Here*/
                builder.RegisterGeneric(typeof(DataRepository<>)).As(typeof(IDataRepository<>));
            }));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }