将Autofac与Microsoft Azure移动服务集成

时间:2017-04-30 14:44:46

标签: c# azure-mobile-services

我创建了一个移动表控制器:

[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课应该在哪里生活。它已经过时了吗?有没有更新的方法呢?

1 个答案:

答案 0 :(得分:1)

您不需要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)
};