WCF can communicate directly with MSMQ, whether you intend to read/write from/to MSMQ. WCF also allows the plug-in of MSMQ-like COM/COM+ components.
MSMQ is a Windows platform specific technology to guarantee the delivery of messages to a specific destination. WCF has 2 bindings which can read/write directly to/from MSMQ, without the need for specific code - just configuration.
WCF offers 2 binding strategies:
- NetMsmqBinding - a push/pull directly to/from MSMQ
- MsmqIntegrationBinding - a push/pull directly from a COM/COM+ component
To use MSMQ and WCF together is actually very straight forward. From inserting into MSMQ, you do not even need a concrete implementation on the WCF. A simple example would be:
namespace WcfServices
{
// Interface is only required for MSMQ
[ServiceContract]
public interface ISendTextService
{
[OperationContract(IsOneWay=true)]
public void Write(string text);
}
}
To hook this up to a configuration file you would do:
<configuration>
<system.serivceModel>
<bindings>
<netMsmqBinding>
<!-- exactlyOnce="false" allows you send multiple MSMQ messages from the channel instance -->
<binding exactlyOnce="false">
<!-- Remove security requirements for testing -->
<security mode="None"/>
</binding>
</bindings>
<client>
<!-- Address format requires net.msmq prefix and no $ sign -->
<!-- The MSMQ 'test' must also already exist for this to work -->
<endpoint address="net.msmq://localhost/private/test"
binding="netMsmqBinding"
contract="WcfService.ISendTextService" />
</client>
</system.serviceModel>
</configuration>
This deals with the sending to MSMQ. A client app can then do something like:
public class TestMsmq
{
static void Main(string[] args)
{
var factory = new ChannelFactory<WcfService.ISendTextService>();
var channel = factory.CreateChannel();
// This is required so that they can all be committed together
using(var scope = new TransactionScope(TransactionScopeOption.Required))
{
channel.SendText("Hello, world!");
channel.SendText("Hello, world! - Part 2");
channel.SendText("Hello, world! - Part 3");
scope.Complete();
}
}
}