尝试创建类型为'DocumentController'

时间:2018-08-20 11:16:48

标签: c# constructor dependencies asp.net-web-api2 autofac

我创建了一个项目,以下载解决方案中的多个文档。 而且它在我的本地IIS上运行良好,但是我陷入了一个奇怪的情况,我已经将该应用程序发布到了生产服务器,现在却出现以下错误:

  

尝试创建类型为'DocumentController'的控制器时发生错误。确保控制器具有无参数的公共构造函数。           System.InvalidOperationException            在System.Web.Http.Tracing的System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer。<> c__DisplayClass6_0.b__0()的System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,type controllerType)处。 .ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter,HttpRequestMessage请求,字符串类别,TraceLevel级别,字符串operatorName,字符串operationName,Action 1 beginTrace, Action execute, Action 1 endTrace,Action 1 errorTrace) at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.System.Web.Http.Dispatcher.IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.<>c__DisplayClass11_0.<CreateController>b__0() at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action 1 beginTrace,Action执行,Action 1 endTrace, Action System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()处System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.CreateController(HttpRequestMessage请求)处的1个错误跟踪-从上次发生异常的位置开始的堆栈跟踪在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()处抛出-从上一个引发异常的位置开始的堆栈跟踪-在System.Runtime.CompilerServices.TaskAwaiter.Th System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification中的rowForNonSuccess(任务任务),位于F:\ vsts4_work \ 12 \ E \ Platform \ PlatFrame中的InEight.Platform.Services.Handler.IEEE754CompatibleHandler.d__0.MoveNext() .Platform \ Web \ Handler \ IEEE754CompatibleHandler.cs:第20行---从上一个引发异常的位置开始的堆栈跟踪---在System.Runtime.CompilerServices的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务) System.Web.Http.Tracing.ITraceWriterExtensions.d__17 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__17 1.MoveNext()上的.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)---从上次引发异常的位置开始的堆栈跟踪-在系统。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Web.Http.HttpServer.d__24.MoveNext()的Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)                      发生了错误。           激活特定注册期间发生错误。有关详细信息,请参见内部异常。注册:激活程序= DocumentController(ReflectionActivator),服务= [InEight.Core.Web.Services.Controllers.WebApi.DocumentController],生命周期= Autofac.Core.Lifetime.CurrentScopeLifetime,共享=无,所有权= OwnedByLifetimeScope --->错误在激活特定注册期间发生。有关详细信息,请参见内部异常。注册:激活程序= DocumentsService(ReflectionActivator),服务= [InEight.Platform.Services.Data.IDocumentsService],生命周期= Autofac.Core.Lifetime.MatchingScopeLifetime,共享=共享,所有权= OwnedByLifetimeScope --->激活期间发生错误特定注册。有关详细信息,请参见内部异常。注册:激活程序= DocumentsService(ReflectionActivator),服务= [InEight.Platform.Services.IDocumentsService],生命周期= Autofac.Core.Lifetime.MatchingScopeLifetime,共享=共享,所有权= OwnedByLifetimeScope --->激活a期间发生错误特殊注册。有关详细信息,请参见内部异常。注册:激活程序= CoreDataContext(ReflectionActivator),服务= [System.Data.Entity.Infrastructure.IObjectContextAdapter,InEight.Platform.Site.Services.Contexts.ICoreDataContext,InEight.Platform.DataAccess.Contexts.IDbContext],生命周期= Autofac.Core .Lifetime.CurrentScopeLifetime,共享=无,所有权= OwnedByLifetimeScope --->调用类型为'CoreDataContext'的构造函数'Void .ctor(InEight.Platform.TenantInfo)'时引发了异常。 --->在发现“ AuthDataContextConfiguration”类型之前,实体框架已使用默认的DbConfiguration实例。必须在使用任何Entity Framework功能之前在应用程序启动时设置“ AuthDataContextConfiguration”的实例,或者必须将其注册在应用程序的配置文件中。有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkId=260883。 (有关详细信息,请参阅内部异常。)(有关详细信息,请参阅内部异常。)(有关详细信息,请参阅内部异常。)(有关详细信息,请参阅内部异常。)(有关详细信息,请参阅内部异常。)           Autofac.Core.DependencyResolutionException            在Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable 1参数)在Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration注册,IEnumerable 1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable 1参数)在Autofac.ResolutionExtensions.TryResolveService( IComponentContext上下文,服务服务,IEnumerable 1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable 1参数)位于C:\ projects \ autofac-webapi \ src \ Autofac.Integration.WebApi \ AutofacWebApiDependencyScope.cs中的Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) :第76行,位于System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage请求,类型为TypefType,Func 1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)</StackTrace> <InnerException> <Message>An error has occurred.</Message> <ExceptionMessage>An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DocumentsService (ReflectionActivator), Services = [InEight.Platform.Services.Data.IDocumentsService], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DocumentsService (ReflectionActivator), Services = [InEight.Platform.Services.IDocumentsService], Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = CoreDataContext (ReflectionActivator), Services = [System.Data.Entity.Infrastructure.IObjectContextAdapter, InEight.Platform.Site.Services.Contexts.ICoreDataContext, InEight.Platform.DataAccess.Contexts.IDbContext], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> An exception was thrown while invoking the constructor 'Void .ctor(InEight.Platform.TenantInfo)' on type 'CoreDataContext'. ---> The default DbConfiguration instance was used by the Entity Framework before the 'AuthDataContextConfiguration' type was discovered. An instance of 'AuthDataContextConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information. (See inner exception for details.) (See inner exception for details.) (See inner exception for details.) (See inner exception for details.)</ExceptionMessage> <ExceptionType>Autofac.Core.DependencyResolutionException</ExceptionType> <StackTrace> at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable 1个参数),位于Autofac.Core.Life.Lifetime的Autofac.Core.Resolving.InstanceLookup.b__5_0()处。 Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration注册,IEnumerable 1 creator) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable 1参数)的LifetimeScope.GetOrCreateAndShare(Guid id,Func 1 parameters) at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass0_0.<CanSupplyValue>b__0() at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable 1参数)在Autofac.Core.Resolving.InstanceLookup.Activate( IEnumerable`1 par千分尺)           

Web Api控制器

public class DocumentController : BaseApiController
{
    private readonly Lazy<ICoreDataContext> _CoreDataContext;
    private readonly IDocumentsService _documentsService;
    protected ICoreDataContext CoreDataContext { get { return _CoreDataContext.Value; } }

    public DocumentController(IContextFactory contextFactory, IDocumentsService documentsService, IAppLog appLog) : base(appLog, contextFactory.Tenant)
    {
        _CoreDataContext = new Lazy<ICoreDataContext>(contextFactory.GetCoreContext);
        _documentsService = documentsService;
    }

    [HttpGet]
    [Route("MultiDocuments")]
    public async Task<HttpResponseMessage> DownloadMultiDocument(string documentIds)
    {
        try
        {
            var user = GetAuthenticatedUser();

            return await _documentsService.DownloadMultiDocumentAsync(user, documentIds);
        }
        catch (Exception e)
        {
            throw HttpError(e);
        }
    }
}

从以下DocumentsService类中,我像下面这样调用新创建的项目的DocumentService类,

  public class DocumentsService : CoreDataService, IDocumentsService
{
    private readonly IProjectsService _projectsService;
    private Dictionary<long, bool> projectAvailibility;
    private Dictionary<long, bool> projectDocumentTypeIdValidity;
    private readonly Services.IDocumentsService _documentService;

    public DocumentsService(IProjectsService projectsService, IContextFactory contextFactory, IAppLog appLog, IPermissionService permissionService,Services.IDocumentsService documentService): base(contextFactory, appLog, permissionService)
    {
        _projectsService = projectsService;
        _documentService = documentService;
    }

    public async Task<HttpResponseMessage> DownloadMultiDocumentAsync(IClaimedUser user, string documentIds)
    {
        return await _documentService.DownloadMultiDocumentAsync(user, documentIds);
    }
}

还有我新项目的服务类

public class DocumentService : IDocumentsService
{
    private readonly TenantInfo tenantInfo;
    private readonly ICoreDataContext coreDataContext;
    private readonly IPermissionService permissionService;

    public DocumentService(TenantInfo tenant, ICoreDataContext coreDataContext, IAppLog log, IPermissionService permissionService)
    {
        tenantInfo = tenant;
        this.coreDataContext = coreDataContext;
        this.coreDataContext.LazyLoadingEnabled = false;
       this.permissionService = permissionService;
    }

    public async Task<HttpResponseMessage> DownloadMultiDocumentAsync(IClaimedUser user, string documentId)
    {
       ...
       return ...;
    }
}

1 个答案:

答案 0 :(得分:0)

抱歉,我犯了一个错误,但我们没有将新的项目引用包含在Web Api Controller项目中。

app