2017-06-22 71 views
-1

我到处搜索了一个解决方案,但是我还没有找到任何解决方案。以下是我的错误。解析控制器时无法构造接口错误

{ 
    "Message": "An error has occurred.", 
    "ExceptionMessage": "An error occurred when trying to create a controller of type 'TechnicianController'. Make sure that the controller has a parameterless public constructor.", 
    "ExceptionType": "System.InvalidOperationException", 
    "StackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()", 
    "InnerException": { 
    "Message": "An error has occurred.", 
    "ExceptionMessage": "Resolution of the dependency failed, type = \"BackendApp.Controllers.TechnicianController\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, System.Collections.Generic.IReadOnlyDictionary`2[System.Type,Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsExtension], is an interface and cannot be constructed. Are you missing a type mapping?\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving BackendApp.Controllers.TechnicianController,(none)\r\n Resolving parameter \"helper\" of constructor BackendApp.Controllers.TechnicianController(Events_Tenant.Common.Core.Interfaces.ITechnicianRepository technicianRepository, Events_Tenant.Common.Helpers.IHelper helper)\r\n Resolving Events_Tenant.Common.Helpers.Helper,(none) (mapped from Events_Tenant.Common.Helpers.IHelper, (none))\r\n Resolving parameter \"tenantsRepository\" of constructor Events_Tenant.Common.Helpers.Helper(Events_Tenant.Common.Core.Interfaces.ICountryRepository countryRepository, Events_Tenant.Common.Core.Interfaces.ITenantsRepository tenantsRepository, Events_Tenant.Common.Core.Interfaces.IProviderRepository venuesRepository, Events_Tenant.Common.Core.Interfaces.IVenueTypesRepository venueTypesRepository)\r\n  Resolving Events_Tenant.Common.Core.Repositories.TenantsRepository,(none) (mapped from Events_Tenant.Common.Core.Interfaces.ITenantsRepository, (none))\r\n  Resolving parameter \"catalogDbContext\" of constructor Events_Tenant.Common.Core.Repositories.TenantsRepository(Events_TenantUserApp.EF.CatalogDB.CatalogDbContext catalogDbContext)\r\n  Resolving Events_TenantUserApp.EF.CatalogDB.CatalogDbContext,(none)\r\n  Resolving parameter \"options\" of constructor Events_TenantUserApp.EF.CatalogDB.CatalogDbContext(Microsoft.EntityFrameworkCore.DbContextOptions`1[[Events_TenantUserApp.EF.CatalogDB.CatalogDbContext, Events-TenantUserApp.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] options)\r\n   Resolving Microsoft.EntityFrameworkCore.DbContextOptions`1[Events_TenantUserApp.EF.CatalogDB.CatalogDbContext],(none)\r\n   Resolving parameter \"extensions\" of constructor Microsoft.EntityFrameworkCore.DbContextOptions`1[[Events_TenantUserApp.EF.CatalogDB.CatalogDbContext, Events-TenantUserApp.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]](System.Collections.Generic.IReadOnlyDictionary`2[[System.Type, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsExtension, Microsoft.EntityFrameworkCore, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]] extensions)\r\n   Resolving System.Collections.Generic.IReadOnlyDictionary`2[System.Type,Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsExtension],(none)\r\n", 
    "ExceptionType": "Microsoft.Practices.Unity.ResolutionFailedException", 
    "StackTrace": " at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)\r\n at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)\r\n at Unity.WebApi.UnityDependencyScope.GetService(Type serviceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)", 
    "InnerException": { 
     "Message": "An error has occurred.", 
     "ExceptionMessage": "The current type, System.Collections.Generic.IReadOnlyDictionary`2[System.Type,Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptionsExtension], is an interface and cannot be constructed. Are you missing a type mapping?", 
     "ExceptionType": "System.InvalidOperationException", 
     "StackTrace": " at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)\r\n at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)\r\n at lambda_method(Closure , IBuilderContext)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)\r\n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)" 
    } 
    } 
} 
+0

它看起来像你的控制器没有一个无参数的构造函数,但它期望和接口传入。如果你想让接口自动传入,你需要使用依赖注入。如果你已经在使用依赖注入,你需要在你的容器中注册你的接口,这样它就知道你的新类型。看着它进一步看起来像你需要用你的依赖注入容器注册IHelper。 – Woot

回答

1

的问题是团结就是试图解决CatalogDbContext这是一个DbContext。默认情况下,Unity将选择参数最多的构造函数。在这种情况下,该构造函数是:

public DbContext(DbContextOptions options); 

这需要一个DbContextOptions对象,以便再次统一将选择一个构造函数和尝试实例任何相关性,这为DbContextOptions恰好是IReadOnlyDictionary<Type, IDbContextOptionsExtension>。不幸的是,Unity不知道如何实例化接口的具体实例IReadOnlyDictionary<Type, IDbContextOptionsExtension>

当控制器无法实例化并且有点红色时,“确保控制器具有无参数公共构造函数”消息由WebApi提供因为真正发生的事情是非参数构造函数失败,并且您可能不想提供无参数构造函数。

上面描述了正在发生的事情。

为什么发生这种情况是因为控制器所需的 相关性尚未配置为 Unity。

如何解决这个问题不能用任何细节给出( “配置容器”),因为没有代码显示如何连接 依赖关系。我猜你可能不想使用构造函数DbContextOptions重载DbContext - 也许指定Unity使用DbContext的默认构造函数足以解决该问题?

根据以下评论,至少有一些方法可以解决问题以填充连接字符串。

选项1 - 连接字符串

注入连接字符串并使用在的DbContext:

public class CatalogDbContext : DbContext 
{ 
    private readonly string connectionString; 

    public CatalogDbContext (string connectionString) 
    { 
     this.connectionString = connectionString; 
    } 

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     optionsBuilder.UseSqlServer(this.connectionString); 
    } 
} 

var container = new UnityContainer(); 
container.RegisterType<CatalogDbContext>(new InjectionConstructor(GetCatalogConnectionString())); 

选择2 - 选项生成器

public class CatalogDbContext : DbContext 
{ 
    public CatalogDbContext(DbContextOptions options) 
     : base(options) 
    { 
    } 
} 

var container = new UnityContainer(); 

var contextOptions = new DbContextOptionsBuilder() 
    .UseSqlServer(GetCatalogConnectionString()) 
    .Options; 

container.RegisterType<CatalogDbContext>(new InjectionConstructor(contextOptions)); 
+0

感谢@Randy的启发。实际上,我的CatalogDbContext有一个承包商,它使用DbContextOptions作为参数,而unityConfig类就像这个container.RegisterType ();我的愿望实际上是指定连接字符串并指定在UnityConfig类中使用哪个构造函数。 –

+0

我试图将这行代码转换为'services.AddDbContext (options => options.UseSqlServer(GetCatalogConnectionString()));'从'IServiceLocation'到Unity –

+1

非常感谢,你拯救了我的生命..它像一个魅力.. –