On This Page

Next

Build and Deploy

Previous

Direct Message

Publish.exe


This example walks through using C# to programmatically send a message to Dovetail Carrier and trigger the previous example (Direct Message).

The code for this example is located in src/Publish.

Dependencies

There are several dependencies required in order to send a properly formattted message to Dovetail Carrier. In this example, we will use the Dovetail.Carrier.Tasks nuget as it provides all the of DLLs necessary.

Configuration

First, we'll create a settings class will be used to configure the MSMQ address that Dovetail Carrier is watching.


    public class MessagingSettings : DictionaryConvertible
    {
        public MessagingSettings()
        {
            QueueAddress = "msmq://localhost/dovetail.carrier";
        }

        public string QueueAddress { get; set; }

        public IEndpoint GetEndpoint(IEndpointCache endpoints)
        {
            var uri = new Uri(QueueAddress);
            return endpoints.GetEndpoint(uri);
        }
    }

Then we'll configure StructureMap to build our settings. We'll also configure MassTransit:


    public class PublishRegistry : Registry
    {
        public PublishRegistry()
        {
            IncludeRegistry<CommonsRegistry>();

            Scan(_ =>
            {
                _.TheCallingAssembly();
                _.Convention<SettingsScanner>();
                _.WithDefaultConventions();
            });

            ForSingletonOf<IEndpointCache>().Use(EndpointCacheFactory.New(_ =>
            {
                _.UseMsmq();
                _.UseJsonSerializer();
            }));
        }
    }

Define your message

Dovetail Carrier is configured to listen messages that typed against the Dovetail.Carrier.Messages.ICarrierMessage interface. This interface simply defines a public property named Data that is a strong. The message we send doesn't need to implement this interface but the properties need to match.


    public class CarrierMessage
    {
        public string Data { get; set; }
    }

Message Publisher

Next, we'll need to retreive the proper endpoint from the cache we configured in StructureMap and use that to send a message. We've created a simple interface to define the send method and then provided a sample of the implementation:


    public class MessagePublisher : IMessagePublisher
    {
        private readonly IEndpointCache _endpoints;
        private readonly MessagingSettings _settings;

        public MessagePublisher(IEndpointCache endpoints, MessagingSettings settings)
        {
            _endpoints = endpoints;
            _settings = settings;
        }

        public void Publish(CarrierMessage message)
        {
            var endpoint = _settings.GetEndpoint(_endpoints);
            Console.WriteLine("Publishing {0} to {1}", message.Data, _settings.QueueAddress);

            // Message must be sent for the Dovetail.Carrier.Messages.ICarrierMessage interface
            endpoint.Send<ICarrierMessage>(message);
        }
    }

Putting it together

Now, we simply configure our container, get an instance of our publisher, and send a message:


    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Invalid usage. Please specify the data for the message as the only argument.");
                Console.ResetColor();
                return;
            }

            using (var container = new Container(new PublishRegistry()))
            {
                var publisher = container.GetInstance<IMessagePublisher>();
                publisher.Publish(new CarrierMessage
                {
                    Data = args[0]
                });
            }
        }
    }

Sample Usage

This executable is reusable in a variety of scenarios. Simple call publish.exe and pass in the desired message:

publish.exe type=GetStockQuote\r\nsymbol=GOOG