Domanda

I have an issue I don't fully understand. When I create message this way it works:

var message = new StartFakeJobCommand();
using (var publishChannel = ApplicationSingleton.Instance.RabbitBus.OpenPublishChannel())
{
    publishChannel.Publish(message);
}

A message is put on the queue and my listener can consume it. But when I create a message using Activator.CreateInstance like this it does not work. Nothing is published to the queue.

var t = Type.GetType(string.Format("{0}.{1},{2}", job.CommandNamespace, job.Command, job.AssemblyName));
if (t == null)
    throw new ArgumentException();

var message = Activator.CreateInstance(t);
using (var publishChannel = ApplicationSingleton.Instance.RabbitBus.OpenPublishChannel())
{
    publishChannel.Publish(message);
}

During debugging I can clearly see that the same type is created using both methods. Any idea why the second approach does not work?

This is how i subscribe to the message:

bus.Subscribe<StartFakeJobCommand>("StartFakeJobCommand_ID", message => fakeJob.Handle(message));
È stato utile?

Soluzione

The signature of Activator.CreateInstance is:

public static Object CreateInstance(
    Type type
)

The type of message is Object, so your message gets published as type Object and since you have no subscribers to Object, it is black holed.

Call publishChannel.Publish with the correct generic type to fix the problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top