Search code examples
c#web-serviceswcfmessage-queuemsmq

WCF Web Service MSMQ Endpoint


I've been working on this issue for over a week now.

I've made much progress, however I've come to a problem which I'm unable to track or debug.

I'm developing in VS 2015 .NET Framework 4 on a Windows 8.1 machine.

The web service is being hosted in IIS 8 on a Windows Server 2012 machine on the same domain as my development environment.

I am able to make a call to another web service using a net.tcp endpoint and get a return successfully.

I believe IIS is setup correctly to host web service with the msmq endpoint, however the web service does not create the message queue nor does it create the message queue when running on my dev machine.

I don't know how to identify what would be the cause.

When I run the client application which dynamically creates a message queue for the web service to talk back to, it does create the message queue but the client is running as a console application.

Web Service

    [AuthenticationBehavior]
    public class LeaseService : ILeasesService
    {               
        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void SaveLease(ILease lease)
        {
        }

        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void InsertLeaseEntries(ILeaseEntry[] entries)
        {                
        }
    }
}

Web Service Host

    class Program
    {
        private static WebServiceHost _host;

        static void Main(string[] args)
        {
            var queue = @".\private$\ServerLeasesQueue";

            if (!MessageQueue.Exists(queue)) MessageQueue.Create(queue, true);

            _host = new WebServiceHost(typeof (LeaseService),
                new Uri(@"http://localhost:8080/LeaseService"));

            _host.Description.Name = "Culbreath.Leases.LeaseService";

            _host.Description.Behaviors.Add(new ServiceMetadataBehavior
            {
                HttpGetEnabled = true,
                HttpGetUrl = new Uri(@"http://localhost:8080/LeaseService")
            });

            var netMsmqBinding = new NetMsmqBinding(NetMsmqSecurityMode.Transport);
            netMsmqBinding.Security.Transport.MsmqProtectionLevel = ProtectionLevel.None;
            netMsmqBinding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;

            _host.AddServiceEndpoint(
                typeof(ILeasesService),
                netMsmqBinding,
                new Uri(@"net.msmq://localhost/private/ServerLeasesQueue"));

            _host.Open();
        }
    }

Solution

  • So through much hard searching, I found the problem. The name of the message queue must be the same as of the service file name, including the .svc.

    var queue = @".\private$\LeaseService.svc";
    if (!MessageQueue.Exists(queue)) MessageQueue.Create(queue, true);