Instantiating OrderProcessorClient was slightly confusing in the code sample above. Thought of reorganizing a bit.
OrderProcessingClient.cspublic class OrderProcessorClient
{
ChannelFactory<IOrderProcessor> channelFactory = null;
private string bindingConfiguration = String.Empty;
public OrderProcessorClient(string bindingConfiguration)
{
this.bindingConfiguration = bindingConfiguration;
}
public void SubmitOrder(PurchaseOrder po)
{
MsmqIntegrationBinding binding = new MsmqIntegrationBinding(this.bindingConfiguration);
EndpointAddress address = new EndpointAddress("msmq.formatname:DIRECT=OS:.\\private$\\Orders");
channelFactory = new ChannelFactory<IOrderProcessor>(binding, address);
IOrderProcessor channel = channelFactory.CreateChannel();
MsmqMessage<PurchaseOrder> ordermsg = new MsmqMessage<PurchaseOrder>(po);
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
channel.SubmitPurchaseOrder(ordermsg);
scope.Complete();
}
Console.WriteLine("Order has been submitted:\n{0}", po);
}
public void Close()
{
channelFactory.Close();
}
}
Program.cs
static void Main(string[] args)
{
// Create the purchase order.
PurchaseOrder po = new PurchaseOrder();
po.CustomerId = "somecustomer.com";
po.PONumber = Guid.NewGuid().ToString();
PurchaseOrderLineItem lineItem1 = new PurchaseOrderLineItem();
lineItem1.ProductId = "Blue Widget";
lineItem1.Quantity = 54;
lineItem1.UnitCost = 29.99F;
PurchaseOrderLineItem lineItem2 = new PurchaseOrderLineItem();
lineItem2.ProductId = "Red Widget";
lineItem2.Quantity = 890;
lineItem2.UnitCost = 45.89F;
po.orderLineItems = new PurchaseOrderLineItem[2];
po.orderLineItems[0] = lineItem1;
po.orderLineItems[1] = lineItem2;
OrderProcessorClient client = new OrderProcessorClient("OrderProcessorBinding");
client.SubmitOrder(po);
//Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
App.config
<configuration>
<system.serviceModel>
<client>
<endpointname="OrderResponseEndpoint"
address="msmq.formatname:DIRECT=OS:.\private$\Orders"
binding="msmqIntegrationBinding"
bindingConfiguration="OrderProcessorBinding"
contract="MsmqService.IOrderProcessor">
</endpoint>
</client>
<bindings>
<msmqIntegrationBinding>
<bindingname="OrderProcessorBinding" >
<securitymode="None" />
</binding>
</msmqIntegrationBinding>
</bindings>
</system.serviceModel>
</configuration>