Unity - 依赖项的解析失败

时间:2017-11-19 09:28:10

标签: c# asp.net-core unity-container

尝试使用Unity解析时出错。

Microsoft.Practices.Unity.ResolutionFailedException occurred
  HResult=0x80131500
  Message=Resolution of the dependency failed, type = "Dell.EMC.CPM.Core.MessageCenter.Sender.ISenderService", name = "(none)".
Exception occurred while: Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container).
Exception is: NullReferenceException - Object reference not set to an instance of an object.

Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.

我在C#核心控制台应用程序中使用Unity.Interception.NetCore Unity.NetCore nuget包。

搜索网络,但没有找到任何线索,为什么我收到此错误。 这是我的代码:

我的界面:

public interface ISenderService
{
    [CommandSaving]
    Task<MessageResponse> SendRequest(MessageRequest req);
}

我的派生类:

public class MassTransitSenderService : ISenderService
{
    ...
    public async Task<MessageResponse> SendRequest(MessageRequest req)
    {
     ...
    }
}

HandlerAttribute类:

public class CommandSavingAttribute : HandlerAttribute
{
    public string Topic               // Topic is a named parameter
    { get; set; }

    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        return new CommandSavingCallHandler();
    }

}

ICallHandler类:

    public class CommandSavingCallHandler : ICallHandler
    {

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            Logger.Log("Log CommandSavingCallHandler");
        }

和解决的代码:

    var container = new UnityContainer();

    container.RegisterType<ISenderService, MassTransitSenderService>(
        new InterceptionBehavior<PolicyInjectionBehavior>(),
        new Interceptor<InterfaceInterceptor>());

    container.AddNewExtension<Interception>();

    container.Configure<Interception>().SetInterceptorFor<ISenderService>(
        new InterfaceInterceptor());
    var massTransitsenderService = container.Resolve<ISenderService>();

当我删除位于我想要拦截的函数上方的界面中的[CommandSaving]时,我得到的不是错误。

任何想法是什么问题?

0 个答案:

没有答案