0

私の ASP.NET Web MVC コントローラー (v5.1) では、 Postal (v0.9.1) でIEMailServiceを使用するように切り替えたかったので、コントローラーで次のようにしました。

public class HomeController : Controller
{
  private readonly IEmailService _emailService;

  public HomeController(IEmailService emailService)
  {
    if (emailService == null)
      throw new ArgumentNullException("emailService");
    _emailService = emailService;
  }
}

私の Unity (v3.0) コンテナーのセットアップ:

container.RegisterType<IEmailService, EmailService>();
container.RegisterType<IEmailViewRenderer, EmailViewRenderer>();
container.RegisterType<IEmailParser, EmailParser>();

しかし、私はこの例外を取得します。これは意味がありません。
Unity は実際に私に何を伝えようとしているのでしょうか?

The type IList`1 does not have an accessible constructor.

スタックトレース:

[InvalidOperationException: The type IList`1 does not have an accessible constructor.]
   Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context) +239
   lambda_method(Closure , IBuilderContext ) +60
   Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +71
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
   Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +250
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +101
   lambda_method(Closure , IBuilderContext ) +205
   Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +71
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
   Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +250
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +101
   lambda_method(Closure , IBuilderContext ) +205
   Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +71
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
   Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +250
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +101
   lambda_method(Closure , IBuilderContext ) +231
   Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +71
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
   Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +250
   Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +101
   lambda_method(Closure , IBuilderContext ) +209
   Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context) +71
   Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
   Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
   Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +383

[ResolutionFailedException: Resolution of the dependency failed, type = "PostalTest.Controllers.HomeController", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type IList`1 does not have an accessible constructor.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving PostalTest.Controllers.HomeController,(none)
  Resolving parameter "emailService" of constructor PostalTest.Controllers.HomeController(Postal.IEmailService emailService)
    Resolving Postal.EmailService,(none) (mapped from Postal.IEmailService, (none))
    Resolving parameter "emailViewRenderer" of constructor Postal.EmailService(Postal.IEmailViewRenderer emailViewRenderer, Postal.IEmailParser emailParser, System.Func`1[[System.Net.Mail.SmtpClient, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] createSmtpClient)
      Resolving Postal.EmailViewRenderer,(none) (mapped from Postal.IEmailViewRenderer, (none))
      Resolving parameter "viewEngines" of constructor Postal.EmailViewRenderer(System.Web.Mvc.ViewEngineCollection viewEngines)
        Resolving System.Web.Mvc.ViewEngineCollection,(none)
        Resolving parameter "list" of constructor System.Web.Mvc.ViewEngineCollection(System.Collections.Generic.IList`1[[System.Web.Mvc.IViewEngine, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] list)
          Resolving System.Collections.Generic.IList`1[System.Web.Mvc.IViewEngine],(none)
]
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +446
   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides) +50
   Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides) +48
   Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides) +61
   Unity.Mvc5.UnityDependencyResolver.GetService(Type serviceType) +140
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +87

[InvalidOperationException: An error occurred when trying to create a controller of type 'PostalTest.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +226
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +157
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
4

1 に答える 1

2

Unity は使っていませんが、代わりにメールサービスのインスタンスを登録していただけませんか?

container.RegisterInstance<IEmailService>(new EmailService());
于 2014-03-07T09:42:39.640 に答える