NServiceBus WCF Service with Error System.InvalidOperationException: Reply is neither supported for Commands nor Events

StackOverflow https://stackoverflow.com/questions/22973996

  •  30-06-2023
  •  | 
  •  

Question

Here is the error I get in my log file once I execute Bus.Reply():

System.InvalidOperationException: Reply is neither supported for Commands nor Events. Commands should be sent to their logical owner using bus.Send and bus. Events should be Published with bus.Publish.

I have a DotNet WCF Service that I have configured using Global.asax as follows in the code below:

I am making a call from a Host Service DLL with a Bus.Sends(Message) On the WCF Service I am calling Bus.Reply(responseMessage)

I want this to be full duplex so that I can do a callback to the client when completed, but I can not seem to get the configuration correct. I have loaded NServiceBus -version 4.4.2 Only, like you would configure a Web Service. The examples I have seen that do full duplex load NServiceBus.Host, but that seems wrong, while running under IIS???

What am I missing?

Thanks in Advance :)

namespace MapQuest.Service
{
using log4net;
using NServiceBus;
using NServiceBus.Installation.Environments;
using System.IO;
using System.Web;

public class ServiceBus
{
    public static IBus Bus { get; private set; }
    public static ILog Log { get; private set; }

    public static void Init()
    {
        if (Bus != null)
            return;

        lock (typeof(ServiceBus))
        {
            if (Bus != null)
                return;

            Bus = Configure.With()
                .Log4Net()
                .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands")
                   && t.Namespace.StartsWith("Regions"))
               .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events")
                   && t.Namespace.StartsWith("Regions"))
               .DefiningMessagesAs(t => t.Namespace != null && t.Namespace.EndsWith("Messages")
                   && t.Namespace.StartsWith("Regions"))
               .DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted"))
                .DefineEndpointName("MapQuest.Service")
                .DefaultBuilder()
                .UseTransport<Msmq>()
                .PurgeOnStartup(true)
                .UnicastBus()
                .LoadMessageHandlers()
                .CreateBus()
                .Start();


            log4net.Config.XmlConfigurator.Configure();
            Log = LogManager.GetLogger(typeof(ServiceBus));


        }
    }
}
}
Was it helpful?

Solution

For Full Duplex the key is to make sure you are using a generic Message for your Bus.Send and Bus.Reply (your class should inherit from IMessage or your namespace needs to end in Messages as your fluid configuration states).

If you're doing a Bus.Reply to a Command or Event you'll get the System.InvalidOperationException: Reply is neither supported for Commands nor Events exception.

OTHER TIPS

This exception is there to discourage the misuse/abuse of commands and events, yet there are valid cases when you're transitioning from a more monolithic architecture that you may need to do something like this.

The solution would be to create a different message type that you use for your replies/responses and put the original message object inside it as a property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top