错误:安全句柄已关闭(NServiceBus / NancyFX)

时间:2015-04-06 11:08:29

标签: nservicebus windows-authentication nancy

我已经调试好几天试图找到根本原因,而这似乎只是发送两条消息引起的。

public class HomeModule : NancyModule
{
    public HomeModule(IBus bus)
    {
        Get["/"] = _ => 
        {
            bus.Send<Command1>(m => { m.Id = "WHATEVER"; });
            return "OK";
        };
    }
}

public class Command1 : ICommand
{
    public string Id { get; set; }
}

public class Command2 : ICommand
{
    public string Id { get; set; }
}

public class Handler1 : IHandleMessages<Command1>
{
    public IBus Bus { get; set; }

    public void Handle(Command1 message)
    {
        Bus.Send<Command2>(m => { m.Id = message.Id; });
    }
}

public class Handler2 : IHandleMessages<Command2>
{
    public IBus Bus { get; set; }

    public void Handle(Command2 message)
    {

    }
}

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);

        LogManager.Use<DefaultFactory>().Directory(container.Resolve<IRootPathProvider>().GetRootPath());

        var configuration = new BusConfiguration();
        configuration.EndpointName("MyEndpoint");
        configuration.DisableFeature<NServiceBus.Features.SecondLevelRetries>(); // TODO
        if (Debugger.IsAttached) configuration.EnableInstallers();
        configuration.UsePersistence<InMemoryPersistence>();

        var bus = Bus.Create(configuration).Start();

        container.Register<IBus>(bus);
    }
}

给我错误:

  

关键值CorrId a539e440-0b7a-4283-a20a-a472006da636 \ 0   NServiceBus.ContentType text / xml   NServiceBus.ConversationId 2b38fa53-749b-43e4-aa0f-a472006da63a   NServiceBus.CorrelationId a539e440-0b7a-4283-a20a-a472006da636   NServiceBus.EnclosedMessageTypes Command1,Version = 1.0.0.0,   Culture = neutral,PublicKeyToken = null   NServiceBus.ExceptionInfo.ExceptionType System.ObjectDisposedException   NServiceBus.ExceptionInfo.HelpLink
  NServiceBus.ExceptionInfo.Message安全句柄已关闭   NServiceBus.ExceptionInfo.Source mscorlib   NServiceBus.ExceptionInfo.StackTrace System.ObjectDisposedException:   安全手柄已关闭   System.Runtime.InteropServices.SafeHandle.DangerousAddRef(布尔&安培;   成功)   System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle,   布尔和放大器;成功)   Microsoft.Win32.Win32Native.GetTokenInformation(SafeTokenHandle   TokenHandle,UInt32 TokenInformationClass,SafeLocalAllocHandle   TokenInformation,UInt32 TokenInformationLength,UInt32&amp; ReturnLength)   在   System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeTokenHandle   tokenHandle,TokenInformationClass tokenInformationClass)at   System.Security.Principal.WindowsIdentity.get_User()at   System.Security.Principal.WindowsIdentity.GetName()at   System.Security.Principal.WindowsIdentity.get_Name()at   NServiceBus.Impersonation.Windows.WindowsIdentityEnricher.MutateOutgoing(的LogicalMessage   logicalMessage,TransportMessage transportMessage)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \模拟\的Windows \ WindowsIdentityEnricher.cs:行   16点到   NServiceBus.MutateOutgoingPhysicalMessageBehavior.Invoke(OutgoingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \ MessageMutator \ MutateOutgoingPhysicalMessageBehavior.cs:行   12在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94在NServiceBus.SerializeMessagesBehavior.Invoke(OutgoingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ SerializeMessagesBehavior.cs:行   38在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.CreatePhysicalMessageBehavior.Invoke(OutgoingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ CreatePhysicalMessageBehavior.cs:行   58在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.PopulateAutoCorrelationHeadersForRepliesBehavior.Invoke(OutgoingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \英雄传奇\ PopulateAutoCorrelationHeadersForRepliesBehavior.cs:行   46在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.MutateOutgoingMessageBehavior.Invoke(OutgoingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \ MessageMutator \ MutateOutgoingMessageBehavior.cs:行   28在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94 at NServiceBus.SendValidatorBehavior.Invoke(OutgoingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ SendValidatorBehavior.cs:行   20在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.Invoke()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   52点   NServiceBus.Pipeline.PipelineExecutor.Execute [T](BehaviorChain 1 pipelineAction, T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 129 at NServiceBus.Pipeline.PipelineExecutor.InvokePipeline[TContext](IEnumerable 1   行为,TContext上下文)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ PipelineExecutor.cs:行   85点   NServiceBus.Pipeline.PipelineExecutor.InvokeSendPipeline(DeliveryOptions   deliveryOptions,LogicalMessage message)in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ PipelineExecutor.cs:行   114点   NServiceBus.Unicast.UnicastBus.InvokeSendPipeline(DeliveryOptions   sendOptions,LogicalMessage消息)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\ UnicastBus.cs:行   624在NServiceBus.Unicast.UnicastBus.SendMessage(SendOptions   sendOptions,LogicalMessage消息)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\ UnicastBus.cs:行   610在NServiceBus.Unicast.UnicastBus.Send [T](行动1 messageConstructor) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 455 at lambda_method(Closure , Object , Object ) at NServiceBus.Unicast.MessageHandlerRegistry.Invoke(Object handler, Object message, Dictionary 2字典)中   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\ MessageHandlerRegistry.cs:行   126点   NServiceBus.Unicast.MessageHandlerRegistry.InvokeHandle(对象   处理程序,对象消息)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\ MessageHandlerRegistry.cs:行   84在NServiceBus.LoadHandlersBehavior.b__1(对象   handlerInstance,Object message)in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ LoadHandlersBehavior.cs:行   40在NServiceBus.InvokeHandlersBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ InvokeHandlersBehavior.cs:行   24在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.SetCurrentMessageBeingHandledBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ SetCurrentMessageBeingHandledBehavior.cs:行   17在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94 at NServiceBus.LoadHandlersBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ LoadHandlersBehavior.cs:行   45在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.ApplyIncomingMessageMutatorsBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \ MessageMutator \ ApplyIncomingMessageMutatorsBehavior.cs:行   23在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.ExecuteLogicalMessagesBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\信息\ ExecuteLogicalMessagesBehavior.cs:行   24在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94 at NServiceBus.CallbackInvocationBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ CallbackInvocationBehavior.cs:行   22在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.DeserializeLogicalMessagesBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\信息\ DeserializeLogicalMessagesBehavior.cs:行   47在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.ApplyIncomingTransportMessageMutatorsBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \ MessageMutator \ ApplyIncomingTransportMessageMutatorsBehavior.cs:行   20在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.SubscriptionReceiverBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\订阅\ MessageDrivenSubscriptions \ SubscriptionReceiverBehavior.cs:行   31在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94 at NServiceBus.UnitOfWorkBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \的UnitOfWork \ UnitOfWorkBehavior.cs:行   42在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94 at NServiceBus.ChildContainerBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\行为\ ChildContainerBehavior.cs:行   17在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94点   NServiceBus.ProcessingStatisticsBehavior.Invoke(IncomingContext   上下文,行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \监视\ ProcessingStatisticsBehavior.cs:行   23在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.&lt;&gt; c__DisplayClass5.b__2()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   94 at NServiceBus.AuditBehavior.Invoke(IncomingContext context,   行动接下来)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \审计\ AuditBehavior.cs:行   20在NServiceBus.BehaviorChain 1.InvokeNext(T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107 at NServiceBus.BehaviorChain 1.Invoke()in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ BehaviorChain.cs:行   52点   NServiceBus.Pipeline.PipelineExecutor.Execute [T](BehaviorChain 1 pipelineAction, T context) in c:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 129 at NServiceBus.Pipeline.PipelineExecutor.InvokePipeline[TContext](IEnumerable 1   行为,TContext上下文)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ PipelineExecutor.cs:行   85点   NServiceBus.Pipeline.PipelineExecutor.InvokeReceivePhysicalMessagePipeline()   在   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \管道\ PipelineExecutor.cs:行   102点   NServiceBus.Unicast.UnicastBus.TransportMessageReceived(对象发送者,   TransportMessageReceivedEventArgs e)in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\ UnicastBus.cs:行   826在   NServiceBus.Unicast.Transport.TransportReceiver.OnTransportMessageReceived(TransportMessage   msg)in   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\传输\ TransportReceiver.cs:行   411在   NServiceBus.Unicast.Transport.TransportReceiver.ProcessMessage(TransportMessage   消息)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\传输\ TransportReceiver.cs:行   344在   NServiceBus.Unicast.Transport.TransportReceiver.TryProcess(TransportMessage   消息)   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \单播\传输\ TransportReceiver.cs:行   在NServiceBus.Transports.Msmq.MsmqDequeueStrategy.Action()中的228   C:\ BuildAgent \工作\ 3206e2123f54fce4的\ src \ NServiceBus.Core \交通服务\ MSMQ \ MsmqDequeueStrategy.cs:行   266

仅当我启用Windows身份验证并禁用匿名时才会出现此问题。

2 个答案:

答案 0 :(得分:2)

NServiceBus版本5.1+解决了这个问题 此问题已在https://groups.google.com/d/msg/particularsoftware/lA7qYOMrqbU/XXs3SfAGcYAJ

中报告过

答案 1 :(得分:0)

正如我在评论中提到的,这看起来像NancyFX中的一个错误。

问题是建议的WindowsIdentityEnricher只检查null。由于Thread.CurrentPrincipal存在,因此第一次检查通过,但Name有一个已处置错误。

我把手表放在Thread.CurrentPrincipal.Name上。它在MsmqMessageSender.Send()MsmqDequeueStrategy.OnPeekCompleted()之间的某处失败,这表明它是NService之外的错误。

作为一项工作,我添加了一个新的变异器来重新填充原则。

public class PrincipalEnricher : IMutateOutgoingTransportMessages
{
    public void MutateOutgoing(LogicalMessage logicalMessage, TransportMessage transportMessage)
    {
        Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    }
}

我对NService不太熟悉,知道它是否在WindowsIdentityEnricher变异器之前被确定性地调用,或者只是巧合。

希望这有助于某人。

PS。巧合的是,如果我启动总线并在同一个控制器中发送消息而不是在Global类中,我会注意到MVC中的相同错误。