无法解释的零星的Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

时间:2018-03-07 09:48:04

标签: c# .net generics

我们已经看到零星的崩溃(到目前为止已有2次),我无法解释。

给出以下代码(缩减为核心):

namespace ConsoleApp1
{
  using System;
  using System.Threading.Tasks;

  public interface IMessage { }

  public class Message : IMessage { }

  public class Response : Message { }

  public class MessageA : Response { }

  public class MessageB : Response { }

  public class MessageC : Response { }

  public class MessageReceived<TMessage> where TMessage : IMessage
  {
    public TMessage Message { get; internal set; }
    public DateTimeOffset ReceiveTimestamp { get; internal set; }
  }

  public sealed class MessageDeserializer<TMessageType> where TMessageType : Message
  {
    public void PublishMessageReceivedOnMessageBus(Message message, DateTimeOffset receiveTimestamp)
    {
      var messageReceivedType = typeof(MessageReceived<>).MakeGenericType(message.GetType());
      dynamic messageReceived = Activator.CreateInstance(messageReceivedType);

      messageReceived.Message = message as dynamic;
      messageReceived.ReceiveTimestamp = receiveTimestamp;

      Console.WriteLine($"Message publish : {messageReceived}, Thread: {Environment.CurrentManagedThreadId}");
    }
  }


  class Program
  {
    static void Main(string[] args)
    {
      var p = new MessageDeserializer<Response>();

      Task.Run(() => { for (;;) p.PublishMessageReceivedOnMessageBus(new MessageA(), DateTimeOffset.Now);});
      Task.Run(() => { for (; ; ) p.PublishMessageReceivedOnMessageBus(new MessageB(), DateTimeOffset.Now); });
      Task.Run(() => { for (; ; ) p.PublishMessageReceivedOnMessageBus(new MessageC(), DateTimeOffset.Now); });
      Task.Run(() => { for (; ; ) p.PublishMessageReceivedOnMessageBus(new Response(), DateTimeOffset.Now); });
      Task.Run(() => { for (; ; ) p.PublishMessageReceivedOnMessageBus(new Message(), DateTimeOffset.Now); });

      Console.ReadLine();
    }
  }
}

我得到以下异常:

  

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException 无法隐式转换类型&#39; ConsoleApp1.MessageA&#39;至   &#39; ConsoleApp1.MessageB&#39; StackTrace:在CallSite.Target(Closure,   CallSite,Object,Object)at   System.Dynamic.UpdateDelegates.UpdateAndExecute2 [T0,T1,TRET](调用点   site,T0 arg0,T1 arg1)at ...

通过阅读代码,不应该有这样的失败。 我还创建了一个控制台应用程序,它在多个线程的循环中调用该函数,看看这是否与并发相关但我无法重现。

有没有人看到过类似的问题或有什么可能出错的想法?​​

完整筹码:

Type: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException 
Message: Cannot implicitly convert type '[...].MessageRsp' to '[...].MessageErr'  
StackTrace: 
at CallSite.Target(Closure , CallSite , Object , Object ) 
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) 
at [...].MessageDeserializer`1.PublishMessageReceivedOnMessageBus(Message message, DateTimeOffset receiveTimestamp) in in [...]MessageDeserializer.cs:line 315 
at [...].MessageDeserializer`1.DeserializeMessageBytes(Byte[] messageBytes, Boolean logResponseData) in [...] MessageDeserializer.cs:line 299 
at System.Reactive.AnonymousSafeObserver`1.OnNext(T value) at ReactiveExtensions.Contrib.SingleThreadSynchronizationContext.Run() in [...]
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Threading.ThreadHelper.ThreadStart()

0 个答案:

没有答案