NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

System.Security.VerificationException,Message:Operation could destabilize the runtime.

Open sandrinodm opened this issue 13 years ago • 3 comments

The issue happens in our publisher process. This is its setup:

 Bus = NServiceBus.Configure
                  .With()
                  .DefineEndpointName("endpoint")
                  .Log4Net()
                  .DefaultBuilder()
                  .XmlSerializer()
                  .MsmqTransport()
                    .IsTransactional(true)
                    .PurgeOnStartup(false)
                  .DBSubcriptionStorage()
                  .DefiningEventsAs(t => t.Namespace != null
                     && t.Namespace.Equals("SomeNamespace"))
                  .UnicastBus()
                    .ImpersonateSender(false)
                  .CreateBus()
                  .Start(() => Configure.Instance
                    .ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());

So we have a base event class and decided to add a property bag which contains contextual information we need for some specific scenarios. This is how we implemented the event at first:

[Serializable()]
public abstract class Event
{
    public string Source { get; set; }

    public DateTime Timestamp { get; set; }

    public Dictionary<string, string> PropertyBag { get; set; }

    public Event()
    {
        this.PropertyBag = new Dictionary<string, string>();
    }
}

So far so good. Now we decided to add some helper methods and also add an indexing operator to simplify access to the property bag:

[Serializable()]
public abstract class Event
{
    public string Source { get; set; }

    public DateTime Timestamp { get; set; }

    public Dictionary<string, string> PropertyBag { get; set; }

    public Event()
    {
        this.PropertyBag = new Dictionary<string, string>();
    }

    public string this[string key]
    {
        get { return PropertyBag[key]; }
        set { PropertyBag[key] = value; }
    }
}

The result: Kaboom! Everything stopped working. The logs showed the following (scary) error:

Exception:System.Security.VerificationException,Message:Operation could destabilize the runtime.,Stacktrace: at lambda_method(Closure , Object ) at NServiceBus.Serializers.XML.XmlMessageSerializer.Write(StringBuilder builder, Type t, Object obj) in c:\TeamCity\buildAgent\work\nsb.master_2\src\impl\Serializers\NServiceBus.Serializers.XML\XmlMessageSerializer.cs:line 698 at NServiceBus.Serializers.XML.XmlMessageSerializer.WriteObject(String name, Type type, Object value, StringBuilder builder) in c:\TeamCity\buildAgent\work\nsb.master_2\src\impl\Serializers\NServiceBus.Serializers.XML\XmlMessageSerializer.cs:line 729 at NServiceBus.Serializers.XML.XmlMessageSerializer.Serialize(Object[] messages, Stream stream) in c:\TeamCity\buildAgent\work\nsb.master_2\src\impl\Serializers\NServiceBus.Serializers.XML\XmlMessageSerializer.cs:line 647 at NServiceBus.Unicast.UnicastBus.MapTransportMessageFor(Object[] rawMessages, TransportMessage result) in c:\TeamCity\buildAgent\work\nsb.master_2\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 1497 at NServiceBus.Unicast.UnicastBus.SendMessage(List`1 addresses, String correlationId, MessageIntentEnum messageIntent, Object[] messages) in c:\TeamCity\buildAgent\work\nsb.master_2\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 727 at NServiceBus.Unicast.UnicastBus.Publish[T](T[] messages) in c:\TeamCity\buildAgent\work\nsb.master_2\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 363

We were using version 3.2.7 and upgraded to 3.3.3 but this didn't fix the issue. The only workaround was to remove the indexing operator.

sandrinodm avatar Dec 19 '12 14:12 sandrinodm

Related to #627.

@sandrinodimattia. Sorry to say but indexed properties were never meant to be supported on messages and that we don't intend to fix this issue.

johnsimons avatar Dec 19 '12 22:12 johnsimons

@johnsimons I understand that you don't support indexed properties. But please work on throwing exceptions that mean something, not exceptions like: "Operation could destabilize the runtime."

sandrinodm avatar Dec 19 '12 22:12 sandrinodm

Issue created to throw a better exception, #836

johnsimons avatar Dec 19 '12 23:12 johnsimons