簡體   English   中英

結合使用Autofac和自托管WebApi-控制器未注冊

[英]Using Autofac with self-hosted WebApi - Controllers not registering

我正在自托管一個WebApi應用程序,以便進行一些集成測試。

我這樣設置服務器:

var httpConfig = new HttpSelfHostConfiguration(BaseAddress);

new ApiServiceConfiguration(httpConfig)
    .Configure();

var server = new HttpSelfHostServer(httpConfig);
server.OpenAsync().Wait();
Server = server; //this is just a property on the containing class

ApiServiceConfiguration是允許我抽象WebApi的配置的類(因此,我可以在Global.asax中將其用於api的IIS托管版本)

這是摘錄:

public class ApiServiceConfiguration 
   {
       private readonly HttpConfiguration _httpConfiguration;

       public ApiServiceConfiguration(HttpConfiguration httpConfiguration)
       {
           _httpConfiguration = httpConfiguration;
       }

       public void Configure()
       {
           //setup routes
           RouteConfig.RegisterRoutes(_httpConfiguration.Routes);

           //setup autofac
           AutofacConfig.Setup(_httpConfiguration);

我的AutofacConfig.Setup靜態方法只是:

public static void Setup(HttpConfiguration config)
{
    var builder = new ContainerBuilder();

    // Register the Web API controllers.
    builder.RegisterApiControllers(Assembly.GetAssembly(typeof(MyController)));

    // Register dependencies.

    // Build the container.
    var container = builder.Build();

    // Configure Web API with the dependency resolver.
    //notice config is passed in as a param in containing method
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}

但是,當我從單元測試中調用api時:

using (var client = new HttpClient(Server))
{
    var result = client.PostAsync(BaseAddress + "api/content/whatever"

    var message = result.Content.ReadAsStringAsync().Result;
}

message的值為:

發生錯誤。“,” ExceptionMessage“:”嘗試創建'ContentController'類型的控制器時發生錯誤。 確保控制器具有無參數的公共構造函數。“,” ExceptionType“:” System.InvalidOperationException“,” StackTrace“:”位於System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage請求,HttpControllerDescriptor controllerDescriptor,類型controllerType)在System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage請求,CancellationToken cancellingToken)在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext( )“,” InnerException“:{” Message“:”發生錯誤。“,” ExceptionMessage“:”類型'My.Namespace.ContentController'沒有默認構造函數“,” ExceptionType“:” System.ArgumentException“ ,“ StackTrace”:“在System.Web.Http.Inteal.TypeActivator.Create [TBase](Type instanceType)在System.Web.Http.Dispatcher.DefaultHttpControllerActivator處在System.Linq.Expressions.Expression.New(類型類型) .System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage請求,HttpControllerDescriptor controllerDescriptor,類型controllerType)上的.GetInstanceOrActivator(HttpRequestMessage請求,type controllerType,Func`1&activator)

這向我表明Autofac控制器注冊無效。

如果我在設置的以下行中插入斷點:

var server = new HttpSelfHostServer(httpConfig);

並使用立即窗口檢查httpConfig.DependencyResolver它確實與Autofac有關

您需要將ContainerBuilder和DependencyResolver移出靜態類。 您的實現使用了后期綁定,而autofac依賴解析器沒有替換默認解析器。

您可以在錯誤消息中看到這一點:“確保控制器具有無參數的公共構造函數。”

默認解析器無法在沒有無參數構造函數的情況下處理Controller,這是構造函數注入所必需的。

我有同樣的問題。 就我而言,問題在於使用Assembly.GetCallingAssembly() 我想獲取調用程序集並將其傳遞給Autofac來注冊控制器。 但是在發布模式下,它不起作用。

閱讀以下文章后:

http://www.ticklishtechs.net/2010/03/04/be-careful-when-using-getcallingassembly-and-always-use-the-release-build-for-testing/

我剛剛刪除了GetCallingAssembly調用,並替換為顯式傳遞程序集,例如typeof(Startup).Assembly

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM