Nielsen's Weblog : .NET [use your Context dude]
Updated: 02-04-2007; 22:35:39.

 

Subscribe to "Nielsen's Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

17. marts 2007

Yesterday I blogged about WCF routing. The one thing that might have seemed counterintuitive was that the function ProcessMessage created a channel based on the router service contract and then used the operation ProcessMessage  to send messages to the final endpoint.

Could you not have used one of the built in channel shapes ?. Yes by all means J, that would be the IDuplexSessionChannel. This shape is used for sending and receiving messages using a duplex session aware communication pattern. With a sessionful channel, messages that are sent in a session are delivered in order. WCF does not allow any credentials to be sent in clear text and as we have configured our backend to authenticationMode="UserNameOverTransport" the IDuplexSessionChannel will perform some bootstrapping calls to exchange a secure symmetric security token, which obey the WS-Trust and WS-SecureConversation specification (RST,RSTR,SCT).

The code is pretty strait forward and it looks a bit more generic than the previous code. From a performance point of view I have found this code to perform slightly better, but there is more code and I have not included any checks in regards to OneWay calls.

public Message ProcessMessage(Message message)

        {

            Message replyMsg = null;

            switch (message.Headers.To.AbsoluteUri)

            {

                case "urn:MyServer":

                    ClientCredentials credentials = new ClientCredentials();

                    credentials.UserName.UserName =

         ServiceSecurityContext.Current.PrimaryIdentity.Name;

 

                    Binding b = CreateCustomBinding();

 

BindingParameterCollection bpc = new BindingParameterCollection();

                    bpc.Add(credentials);

 

IChannelFactory<IDuplexSessionChannel> fac = b.BuildChannelFactory<IDuplexSessionChannel>(bpc);

                    fac.Open();

 

                    IDuplexSessionChannel channel = fac.CreateChannel(epa);

                    channel.Open();

                    //save message id for correlation

                    object msgID = message.Headers.MessageId;

 

Message strippedMsg = Message.CreateMessage(MessageVersion.Default,

message.Headers.Action, message.GetReaderAtBodyContents());

                    //add message id.

                    strippedMsg.Headers.MessageId = (System.Xml.UniqueId)msgID;

                    channel.Send(strippedMsg);

                    Message msg = channel.Receive();

                    replyMsg = Message.CreateMessage(MessageVersion.Default,

msg.Headers.Action, msg.GetReaderAtBodyContents());

                    //stuff the messageid back in the reply message

                    replyMsg.Headers.MessageId = (System.Xml.UniqueId)msgID;

                    channel.Close();

                    fac.Close();

                    break;

            }

            return replyMsg;

        }

"The beginning of knowledge is the discovery of something we do not understand."
- Frank Herbert

 


6:14:57 PM    comment []

© Copyright 2007 Allan Nielsen.



Click here to visit the Radio UserLand website.
 


March 2007
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Jun   Apr