将 Autofac 与 Microsoft Azure 移动服务集成

Integrating Autofac with Microsoft Azure Mobile Services

我已经创建了一个移动 table 控制器:

[MobileAppController]
[Authorize]
public class EventOrganiserMembershipDtoController : TableController<EventOrganiserMembershipDto>
{
    private readonly IModelContext _modelContext;

    public EventOrganiserMembershipDtoController(IModelContext modelContext)
    {
        _modelContext = modelContext;
    }

    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
        DomainManager = new EventOrganiserMembershipDomainManager((DbContext)_modelContext, controllerContext.Request);
    }
}

但是它不接受依赖注入,尽管我已经在我的项目中设置了它。

根据我所做的所有搜索,要完成这个应该很容易的任务,我需要行 ServiceConfig.Initialize(new ConfigBuilder(options))。但我不知道这个神话般的 ServiceConfig class 应该住在哪里。它过时了吗?有更新的方法吗?

您不需要 ServiceConfig 调用:我认为它是遗留移动服务器基础结构的一部分。您只需要将容器传递到脚手架中:将 IContainer 参数添加到样板代码

    public static void ConfigureMobileApp(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        new MobileAppConfiguration()
            .UseDefaultConfiguration()
            .ApplyTo(config);

        // Use Entity Framework Code First to create database tables based on your DbContext
        Database.SetInitializer(new MobileServiceInitializer());

        MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

        if (string.IsNullOrEmpty(settings.HostName))
        {
            app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
            {
                // This middleware is intended to be used locally for debugging. By default, HostName will
                // only have a value when running in an App Service application.
                SigningKey = ConfigurationManager.AppSettings["SigningKey"],
                ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
                ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
                TokenHandler = config.GetAppServiceTokenHandler()
            });
        }

        app.UseWebApi(config);
    }

并替换

HttpConfiguration config = new HttpConfiguration();

var config = new HttpConfiguration
{
    DependencyResolver = new AutofacWebApiDependencyResolver(container)
};