Autofac与Mobile Azure Service的集成

时间:2015-04-29 13:53:42

标签: c# autofac azure-mobile-services

我创建了Mobile Azure Service项目并将其与Autofac集成。

这是我的IocConfig.cs,其中我已经注册了所有ApiControllers和libs。

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();
}

1 个答案:

答案 0 :(得分:1)

我已使用相关服务更改了Ioc Config。它现在正在运作。

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;
        }
    }