【问题标题】:Integrating Autofac with Microsoft Azure Mobile Services将 Autofac 与 Microsoft Azure 移动服务集成
【发布时间】:2017-09-28 04:36:37
【问题描述】:

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

[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 班级应该住在哪里。它已经过时了吗?有更新的方法吗?

【问题讨论】:

    标签: c# azure-mobile-services


    【解决方案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)
    };
    

    【讨论】:

    • 很高兴听到您解决了这个问题。您可以将您的回复标记为可接受的答案,这可以帮助可能遇到类似问题的其他社区成员。
    • 再过 12 小时,我可以做到。这就是 Stack Overflow 的规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2015-03-25
    相关资源
    最近更新 更多