Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / C#

WCF: Duplex MSMQ

Rate me:
Please Sign up or sign in to vote.
4.96/5 (29 votes)
3 Sep 2009Ms-PL11 min read 147.6K   2K   97  
How to use duplex communication over MSMQ bindings.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;


namespace SlasheneFramework.WCF
{
	public class ListenUriBindingElement : BindingElement
	{
		public ListenUriBindingElement()
		{
		}
		public ListenUriBindingElement(Uri listenUriBaseAddress)
		{
			this.listenUriBaseAddress = listenUriBaseAddress;

		}
		public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
		{
			if(listenUriBaseAddress != null)
				context.ListenUriBaseAddress = listenUriBaseAddress;
			return base.BuildChannelListener<TChannel>(context);
		}

		public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
		{
			if(listenUriBaseAddress != null)
				context.ListenUriBaseAddress = listenUriBaseAddress;
			return base.BuildChannelFactory<TChannel>(context);
		}

		public override BindingElement Clone()
		{
			return new ListenUriBindingElement(listenUriBaseAddress);
		}
		public override T GetProperty<T>(BindingContext context)
		{
			return context.GetInnerProperty<T>();
		}
		public Uri ListenUriBaseAddress
		{
			get
			{
				return listenUriBaseAddress;
			}
			set
			{
				listenUriBaseAddress = value;
			}
		}

		private Uri listenUriBaseAddress;
	}

	public class ReplyToBindingElement : BindingElement
	{
		public override BindingElement Clone()
		{
			return new ReplyToBindingElement();
		}

		public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
		{
			if(!CanBuildChannelFactory<TChannel>(context))
			{
				throw new ArgumentException("Impossible to build ReplyToBindingElement");
			}
			return (IChannelFactory<TChannel>)new ReplyToChannelFactory(context.ListenUriBaseAddress, (IChannelFactory<IOutputChannel>)base.BuildChannelFactory<TChannel>(context));
		}

		public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
		{
			return base.BuildChannelListener<TChannel>(context);
		}

		public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
		{
			return typeof(TChannel) == typeof(IOutputChannel);
		}

		public override T GetProperty<T>(BindingContext context)
		{
			return context.GetInnerProperty<T>();
		}
	}
}

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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions