C# 将Autofac与自托管WebApi一起使用-控制器未注册
我正在自托管一个WebApi应用程序,以便进行一些集成测试 我的服务器设置如下:C# 将Autofac与自托管WebApi一起使用-控制器未注册,c#,dependency-injection,asp.net-web-api,autofac,C#,Dependency Injection,Asp.net Web Api,Autofac,我正在自托管一个WebApi应用程序,以便进行一些集成测试 我的服务器设置如下: var httpConfig = new HttpSelfHostConfiguration(BaseAddress); new ApiServiceConfiguration(httpConfig) .Configure(); var server = new HttpSelfHostServer(httpConfig); server.OpenAsync().Wait(); Server = ser
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中使用它,用于IIS托管的api版本)
以下是摘录:
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;
}
信息的价值是:
出现错误。“,“ExceptionMessage”:“尝试创建“ContentController”类型的控制器时出错。确保控制器在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)上具有无参数公共构造函数。“,“ExceptionType”:“System.InvalidOperationException”,“StackTrace”:”在System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage请求,CancellationToken CancellationToken)在System.Web.Http.Dispatcher.HttpControllerDispatcher.d_0.MoveNext()中创建控制器(HttpRequestMessage请求),“InnerException”:{“消息”:出现错误。“,“ExceptionMessage”:“Type'My.Namespace.ContentController'没有默认构造函数”,“ExceptionType”:“System.ArgumentException”,“StackTrace”:“at System.Linq.Expressions.Expression.Expression.New(Type Type)at System.Web.Http.Inteal.TypeActivator.Create[TBase](Type instanceType)在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceActivator(HttpRequestMessage请求,类型controllerType,Func`1&activator)的System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)
这向我表明,Autofac控制器注册不起作用
如果我在设置中的以下行上粘贴断点:
var server = new HttpSelfHostServer(httpConfig);
并使用即时窗口检查
httpConfig.dependencysolver
它确实与Autofac相关您需要将ContainerBuilder和dependencySolver移出静态类。您的实现使用的是后期绑定,而Autofac依赖项解析程序并没有替换默认的解析程序
您可以在错误消息中看到这一点:“确保控制器具有无参数公共构造函数。”
默认解析程序无法处理没有无参数构造函数的控制器,而无参数构造函数是构造函数注入所必需的。您需要将ContainerBuilder和DependencyResolver移出静态类。您的实现正在使用后期绑定,并且autofac依赖解析程序不会替换默认的re解算器 您可以在错误消息中看到这一点:“确保控制器具有无参数公共构造函数。”
默认冲突解决程序无法处理没有无参数构造函数的控制器,而无参数构造函数是构造函数注入所必需的。我也有同样的问题。在我的情况下,问题在于使用
Assembly.GetCallingAssembly()
。我想获取调用程序集并将其传递给Autofac以注册控制器。但在释放模式下,它不起作用
读完以下文章:
我刚刚删除了GetCallingAssembly调用,取而代之的是显式地传递程序集,比如
typeof(Startup)。assembly
我也遇到了同样的问题。在我的例子中,问题在于使用assembly.GetCallingAssembly()
。我想获取调用程序集并将其传递给Autofac以注册控制器。但在释放模式下,它不起作用
读完以下文章:
我刚刚删除了GetCallingAssembly调用,取而代之的是显式传递程序集,比如
typeof(Startup)。程序集可以发布MyController类吗?我可以……但它只是一个具有依赖性的常规控制器,并且在“活动”设置下工作(即托管在IIS/cassini中)您是将MVC4与WebApi1一起使用,还是将MVC5与WebApi2一起使用?因为我无法在新创建的MVC4/WebApi1应用程序中使用您的代码,并将控制台应用程序用作自主机来重新处理您的问题…请在客户端代码中尝试注销服务器.Configuration.DependencyResolver
仍然是autofac dependency resolver。否则,请尝试在一个新项目中重新编译并上传到某个地方。你能发布MyController类吗?我可以…但它只是一个具有依赖性的常规控制器,并且在“实时”设置下工作(即托管在IIS/cassini中)您是将MVC4与WebApi1一起使用,还是将MVC5与WebApi2一起使用?因为我无法在新创建的MVC4/WebApi1应用程序中使用您的代码,并将控制台应用程序用作自主机来重新处理您的问题…请在客户端代码中尝试注销服务器.Configuration.DependencyResolver
仍然是autofac dependency resolver。否则,请尝试在一个新项目中重新编程并上传到某个地方。