|
//*****************************************************************************
// Description.....Service Bus Tester
//
// Author..........Roman Kiss, rkiss@pathcom.com
// Copyright © 2011 ATZ Consulting Inc. (see included license.rtf file)
//
// Date Created: 11/11/11
//
// Date Modified By Description
//-----------------------------------------------------------------------------
// 11/11/11 Roman Kiss Initial Revision
//
//*****************************************************************************
//
#region Namespaces
using System;
using System.Diagnostics;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using Microsoft.ServiceBus.Messaging;
#endregion
namespace RKiss.Tools.ServiceBusTester
{
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
public class VirtualService : IGenericOneWayContract
{
public void ProcessMessage(System.ServiceModel.Channels.Message msg)
{
Trace.WriteLine("VirtualService: *** Message has been received ***");
Trace.WriteLine(msg.ToString());
ChannelFactory<IGenericOneWayContract> factory = null;
//var m = (OperationContext.Current.IncomingMessageProperties[BrokeredMessageProperty.Name] as BrokeredMessageProperty).Message;
//var rc = m.GetType().GetProperty("ReceiveContext", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(m, null);
//var mr = rc.GetType().GetProperty("MessageReceiver").GetValue(rc, null) as MessageReceiver;
//ReceiveMode mode = mr.Mode;
//if (mode == ReceiveMode.PeekLock)
//{
// //m.Abandon();
// m.Complete();
//}
try
{
var config = OperationContext.Current.Host.Extensions.Find<ConsumerConfigData>();
if (config == null)
throw new Exception("Fatal error: Missing ServiceConfigData extension object");
var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
var se = new ServiceEndpoint(ContractDescription.GetContract(typeof(IGenericOneWayContract)), binding, new EndpointAddress(config.TesterAddress));
factory = new ChannelFactory<IGenericOneWayContract>(se);
var channel = factory.CreateChannel();
using (var scope = new OperationContextScope((IContextChannel)channel))
{
if (msg.Version == MessageVersion.None)
{
var message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "*", msg.GetReaderAtBodyContents().ReadSubtree());
message.Headers.Add(MessageHeader.CreateHeader(BrokeredMessageProperty.Name, BrokeredMessagePropertyExtension.XName.NamespaceName, (msg.Properties[BrokeredMessageProperty.Name] as BrokeredMessageProperty).CopyTo()));
message.Headers.Add(MessageHeader.CreateHeader(ConsumerConfigData.XName.LocalName, ConsumerConfigData.XName.NamespaceName, config));
channel.ProcessMessage(message);
}
else
{
if (msg.Properties.ContainsKey(BrokeredMessageProperty.Name))
{
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(BrokeredMessageProperty.Name, BrokeredMessagePropertyExtension.XName.NamespaceName, (msg.Properties[BrokeredMessageProperty.Name] as BrokeredMessageProperty).CopyTo()));
}
OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader(ConsumerConfigData.XName.LocalName, ConsumerConfigData.XName.NamespaceName, config));
channel.ProcessMessage(msg);
}
}
factory.Close();
Trace.WriteLine("VirtualService: --- Message has been sent to tester ---");
}
catch (CommunicationException ex)
{
if (factory != null)
{
if (factory.State == CommunicationState.Faulted)
factory.Abort();
else if (factory.State != CommunicationState.Closed)
factory.Close();
factory = null;
}
Trace.WriteLine(ex.Message);
}
catch (Exception ex)
{
if (factory != null)
{
if (factory.State == CommunicationState.Faulted)
factory.Abort();
else if (factory.State != CommunicationState.Closed)
factory.Close();
factory = null;
}
Trace.WriteLine(ex.Message);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.