我尝试使用Autofac 3.1.0配置我的Web API服务。
我已添加"标记"属性到控制器:
[AutofacControllerConfiguration]
public class DocumentController : ApiController
{
// ... snip ...
}
并注册了它:
// controllers
container.RegisterApiControllers(typeof (DocumentController).Assembly);
container.RegisterApiControllers(typeof (WorkflowController).Assembly);
// security
container.RegisterType<OAuthTokenManager>().As<IOAuthTokenManager>();
container.RegisterType<OAuthAccessToken>().As<IOAuthAccessToken>();
container.RegisterType<LegacyTypeAwareIdentifierFactory<IOAuthAccessToken>>().As<ITypeAwareIdentifierFactory<IOAuthAccessToken>>();
container.RegisterType<OAuthTokenExtractor>().As<ITokenExtractor>();
container.RegisterType<OAuthTokenValidator>().As<ITokenValidator>();
container.RegisterType<TokenHelper>().As<ITokenHelper>();
container.Register(x =>
{
if (x.IsRegistered<HttpRequestMessage>())
{
var httpRequestMethod = x.Resolve<HttpRequestMessage>();
var tokenHelper = x.Resolve<ITokenHelper>();
var token = tokenHelper.GetToken(httpRequestMethod);
var connectionContext = x.Resolve<ISqlServerConnectionContext>();
connectionContext.Token = token;
return token ?? new NullMinimalSecurityToken();
}
return new NullMinimalSecurityToken();
}).As<IMinimalSecurityToken>().InstancePerApiRequest();
container.RegisterType<SecurityTokenAuthorization>()
.AsWebApiAuthorizationFilterFor<DocumentController>()
.InstancePerApiRequest();
container.RegisterType<SecurityTokenAuthorization>()
.AsWebApiAuthorizationFilterFor<WorkflowController>()
.InstancePerApiRequest();
container.Register(c =>
{
var connectionString = ConfigurationManager.AppSettings[AppSettingsNames.DatabaseConnection];
var newConnectionContext = new SqlServerConnectionContext(connectionString) {ProductID = productId};
newConnectionContext.Open();
return newConnectionContext;
}).As<ISqlServerConnectionContext>().As<IConnectionContext>().InstancePerApiRequest().InstancePerApiControllerType(typeof(DocumentController));
我最终得到以下错误:
{
"Message":"An error has occurred.",
"ExceptionMessage":"Object reference not set to an instance of an object.",
"ExceptionType":"System.NullReferenceException",
"StackTrace":" at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.get_Properties()\r\n at Autofac.Integration.WebApi.AutofacControllerConfigurationAttribute.Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType)\r\n at System.Web.Http.Tracing.Tracers.HttpControllerSelectorTracer.System.Web.Http.Dispatcher.IHttpControllerSelector.SelectController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)"
}
如果我只是删除&#34;标记&#34;属性,我的服务工作,但这不是一个可行的解决方案,因为我有2个控制器,需要为每个控制器提供正确的数据库连接。我很乐意实施一个替代解决方案,但是在为了达到这一点一整天的努力之后,我现在正在画一个空白。
我是否错过了使用该功能的内容,或者有人可以提出替代方案?