private void ProcessOrder(Order order) { if (order.OrderDate < DateTime.Today) { throw new Exception(); } }
public void Submit(Order order) { Console.WriteLine("Begin to process thie order of the order NO.:{0}", order.OrderNo); FaultException exception = null; if (order.OrderDate < DateTime.Today) { exception = new FaultException(new FaultReason("The order has expried"), new FaultCode("sender")); Console.WriteLine("It's fail to process the order.\n\tOrder No.:{0}\n\tReasion:{1}", order.OrderNo, "The order has expried"); } else { Console.WriteLine("It's successful to process the order.\n\tOrder No.:{0}", order.OrderNo); } NetMsmqBinding binding = new NetMsmqBinding(); binding.ExactlyOnce = false; binding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None; binding.Security.Transport.MsmqProtectionLevel = ProtectionLevel.None; ChannelFactory<IOrderResponse> channelFactory = new ChannelFactory<IOrderResponse>(binding); OrderResponseContext responseContext = OrderResponseContext.Current; IOrderResponse channel = channelFactory.CreateChannel(new EndpointAddress(responseContext.ResponseAddress)); using (OperationContextScope contextScope = new OperationContextScope(channel as IContextChannel)) { channel.SubmitOrderResponse(order.OrderNo, exception); } }
static void Main(string[] args) { Order order1 = new Order(Guid.NewGuid(), DateTime.Today.AddDays(5), Guid.NewGuid(), "Supplier A"); Order order2 = new Order(Guid.NewGuid(), DateTime.Today.AddDays(-5), Guid.NewGuid(), "Supplier A"); string path = ConfigurationManager.AppSettings["msmqPath"]; Uri address = new Uri(path); OrderResponseContext context = new OrderResponseContext(); context.ResponseAddress = address; ChannelFactory<IOrderProcessor> channelFactory = new ChannelFactory<IOrderProcessor>("defaultEndpoint"); IOrderProcessor orderProcessor = channelFactory.CreateChannel(); using (OperationContextScope contextScope = new OperationContextScope(orderProcessor as IContextChannel)) { Console.WriteLine("Submit the order of order No.:{0}", order1.OrderNo); OrderResponseContext.Current = context; orderProcessor.Submit(order1); } using (OperationContextScope contextScope = new OperationContextScope(orderProcessor as IContextChannel)) { Console.WriteLine("Submit the order of order No.:{0}", order2.OrderNo); OrderResponseContext.Current = context; orderProcessor.Submit(order2); } Console.Read(); }