在Azure移动应用程序中自定义Autofac会导致“没有为ITableControllerConfigProvider类型注册服务”异常

时间:2015-08-27 12:00:58

标签: autofac azure-mobile-services

我正在尝试自定义使用Visual Studio创建的Azure Web应用程序。我添加了一个AccountsController来帮助使用Owin会员表进行用户注册。我想将Owin添加到网站,所以我使用这种方法自定义WebApiConfig.cs文件:

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

       var configBuilder = new ConfigBuilder(options, (configuration, builder) =>
       {
           var executingAssembly = Assembly.GetExecutingAssembly();
           var file = FileHelper.GetLoggingConfigFile(executingAssembly);

           // startup the logging
           _logger = new Logger(MethodBase.GetCurrentMethod().DeclaringType, file);

           //builder.RegisterInstance(new CustomOwinAppBuilder(configuration))
           //                            .As<IOwinAppBuilder>();

           //configure the Autofac IoC container
           AutofacBuilder.Configure(executingAssembly, _logger, builder, new MvcModule(),
               new TaskModule());

       });

       var defaultConfig = ServiceConfig.Initialize(configBuilder);

       // Make sure this is after ServiceConfig.Initialize
       // Otherwise ServiceConfig.Initialize will overwrite your changes
       StartupOwinAppBuilder.Initialize(app =>
       {
           // Configure the db context and user manager to use a single instance per request
           app.CreatePerOwinContext(TrainMobileContext.Create);
           app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

           // app.UseFacebookAuthentication("", "");
       });

       defaultConfig.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{id}",
           defaults: new { id = RouteParameter.Optional }
       );

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

AutofacBuilder使用如下语句处理大量注册:

builder.RegisterType<RepositoryProvider>().As<IRepositoryProvider>().InstancePerHttpRequest();
       builder.RegisterType<DataManager>().As<IDataManager>().InstancePerHttpRequest();
       builder.RegisterType<Logger>().As<ILogger>().InstancePerLifetimeScope();          

       // new TrainMobileUserStore(context.Get<SpaceLinxContext>())
       builder.RegisterControllers(assembly).InstancePerHttpRequest();
       builder.RegisterApiControllers(assembly);
       builder.RegisterModelBinders(assembly).InstancePerHttpRequest();
       builder.RegisterType<LogAttribute>().PropertiesAutowired();
       builder.RegisterFilterProvider();

       // Needed to allow property injection in custom action filters.
       builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
       builder.RegisterControllers(assembly).InjectActionInvoker();

然而,当我做出这些改变时,会发生两件事:

首先,默认的azure移动应用默认帮助页面消失了,我得到一个默认页面:

HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.

其次,当我尝试直接调用帮助页面或AccountsController时,会引发运行时异常:

System.InvalidOperationException occurred
HResult=-2146233079
Message=No service registered for type 'ITableControllerConfigProvider'.Please ensure that the dependency resolver has been configured correctly.
Source=Microsoft.WindowsAzure.Mobile.Service
StackTrace:
  at System.Web.Http.DependencyScopeExtensions.GetServiceOrThrow[TService](IDependencyScope services)
  at Microsoft.WindowsAzure.Mobile.Service.Tables.TableControllerConfigAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
  at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)
  at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)
  at System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType)
  at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()
  at System.Lazy`1.CreateValue()
  at System.Lazy`1.LazyInitValue()
  at System.Lazy`1.get_Value()
  at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()
  at System.Web.Http.Description.ApiExplorer.InitializeApiDescriptions()
  at System.Lazy`1.CreateValue()
  at System.Lazy`1.LazyInitValue()
  at System.Lazy`1.get_Value()
  at System.Web.Http.Description.ApiExplorer.get_ApiDescriptions()
  at MyMobileApp.Mvc.Areas.HelpPage.Controllers.HelpController.Index() in C:\tfs\MyMobileApp\dotNET\Web\MyMobileApp.Mvc\Areas\HelpPage\Controllers\HelpController.cs:line 31
InnerException:

有谁知道这可能是什么问题?我是否需要明确注册移动服务程序集?如果是,那么最好的方法是什么?

1 个答案:

答案 0 :(得分:0)

现在已经解决了。

为了解决这个问题,基本的问题是我改变了WebApiConfig.Register方法,使其不合标准。我已经改变了这个

public static void Register()

到这个

public static void Register(HttpConfiguration config)

并试图像使用Global.asax.cs的标准Mvc webapi配置那样使用它

一旦我将其更改回来,我就可以使用autofac在这样的方法中注册对象:

builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
builder.Register(c => new UserStore<ApplicationUser>(c.Resolve<ApplicationContext>())).AsImplementedInterfaces().InstancePerRequest();
builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>();
builder.Register(c => new IdentityFactoryOptions<ApplicationUserManager>
{
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("Application​")
}); 

没有任何问题

感谢