2017-02-09 12 views
0

Web APIを実行すると、次のエラーが発生します。私はエラー:コントローラにWeb API UnitOfWorkのパラメータのないpublicコンストラクタがあることを確認してください

public static class UnityConfig 
{ 
    public static void RegisterComponents() 
    { 
     var container = new UnityContainer(); 

     // register all your components with the container here 
     // it is NOT necessary to register your controllers 

     // e.g. container.RegisterType<ITestService, TestService>(); 

     container.RegisterType<IProductService, ProductService>(); 
     GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container); 
    } 
} 

コントローラ

public class ProductController : ApiController 
{ 
    private readonly IProductService _productService; 
    public ProductController() 
     : base() 
    { 

    } 
    public ProductController(IProductService productService) 
    { 
     _productService = productService; 
    } 

    [HttpGet] 
    public IHttpActionResult GetAllProducts() 
    { 
     return Ok(_productService.GetAllProducts()); 
    } 
} 

アプリケーションサービス

Unity.config Iocを

{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'ProductController'. 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) 
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) 
at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Resolution of the dependency failed, type = \"_1.DDD.WebAPIApp.Controllers.ProductController\", name = \"(none)\". 
Exception occurred while: while resolving. 
Exception is: InvalidOperationException - The current type, _3.DDD.Domain.IRepository.IProductRepository, is an interface and cannot be constructed. Are you missing a type mapping? 
----------------------------------------------- 
At the time of the exception, the container was: 

Resolving _1.DDD.WebAPIApp.Controllers.ProductController,(none) 
Resolving parameter \"productService\" of constructor _1.DDD.WebAPIApp.Controllers.ProductController(_2.DDD.Application.Services.IProductService productService) 
Resolving _2.DDD.Application.Services.ProductService,(none) (mapped from _2.DDD.Application.Services.IProductService, (none)) 
Resolving parameter \"productRepository\" of constructor _2.DDD.Application.Services.ProductService(_3.DDD.Domain.IRepository.IProductRepository productRepository) 
Resolving _3.DDD.Domain.IRepository.IProductRepository,(none) 
","ExceptionType":"Microsoft.Practices.Unity.ResolutionFailedException","StackTrace":" at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) 
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides) 
at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) 
at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides) 
at Unity.WebApi.UnityDependencyScope.GetService(Type serviceType) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"The current type, _3.DDD.Domain.IRepository.IProductRepository, 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) 
at lambda_method(Closure , IBuilderContext) 
at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.b__0(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) 
at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) 
at lambda_method(Closure , IBuilderContext) 
at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.b__0(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) 
at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) 
at lambda_method(Closure , IBuilderContext) 
at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.b__0(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) 
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) 
at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)"}}} 

用ユニットを使用しています

リポジトリ

public interface IRepository<TEntity> : IDisposable where TEntity : Entity 
{ 

    IUnitOfWork UnitOfWork { get; } 

    void Add(TEntity item); 
    void Remove(TEntity item); 
    void Modify(TEntity item); 
    void TrackItem(TEntity item); 
    void Merge(TEntity persisted, TEntity current); 
    TEntity Get(int id); 

    IEnumerable<TEntity> GetAll(); 

    IEnumerable<TEntity> GetPaged<TKProperty>(
     int pageIndex, 
     int pageCount, 
     Expression<Func<TEntity, TKProperty>> orderByExpression, 
     bool ascending); 

    IEnumerable<TEntity> GetFiltered(Expression<Func<TEntity, bool>> filter); 
} 

たUnitOfWork

public interface IUnitOfWork : IDisposable 
{ 
    void Commit(); 
    void CommitAndRefreshChanges(); 
    void RollbackChanges(); 
} 

答えて

0

は、どのような例外状態を読み出します。それは

'ProductController'と言います。コントローラにパラメータのないpublicコンストラクタがあることを確認してください

つまり、入力パラメータを持たないpublicコンストラクタProductControllerを作成する必要があります。

+0

には、パラメータのないコンストラクタが既に追加されています – Pradees

関連する問題