Creating a Behavior to Inspect Messages on the Client


What has been accomplished thus far is the creation of a dispatch service behavior to operate on messages directed at the chat/echo service. Now a proxy channel behavior will be constructed to operate on messages sent by a chat/echo client.

Creating the Proxy Channel Behavior

Execute these steps to create a proxy channel behavior that will send copies of all messages sent by a client to the auditing service:

1.

Right-click on the solution and create a new C# class library project named ClientAuditBehavior.

2.

Add references to the System.Runtime.Serialization and System.ServiceModel assemblies.

3.

Provide a copy of the Auditing service's contract by creating a new class module named Contract.cs and replacing the default code in that module with this code:

using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; namespace AuditingService {    [ServiceContract]     public interface IAudit     {         [OperationContract(IsOneWay = true)]         void ProcessMessage(                             string reportedFrom,                             string messageType,                             string message);     } }


4.

Now provide the code for the proxy channel behavior by adding a new class module called AuditMessageInspector.cs to the ClientAuditBehavior project, and replacing the default code in that module with this code:

using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; using System.Xml; using System.IO; namespace ClientAuditBehavior {       class ClientAuditMessageInspector : IProxyMessageInspector       {         private IAudit _proxy;         public ClientAuditMessageInspector(IAudit prx)         {           proxy = prx;         }         public object BeforeSendRequest(           ref Message request,           IClientChannel channel)         {           return null;         }         public void AfterReceiveReply(           ref Message reply,           object correlationState)         {           MessageBuffer buf = reply.CreateBufferedCopy(1000000);           Message msg = buf.CreateMessage();           TextWriter strWrt = new StringWriter();           XmlTextWriter wrt = new XmlTextWriter(strWrt);           msg.WriteBody(wrt);           wrt.Flush();           strWrt.Flush();           String msgStr = strWrt.ToString();           wrt.Close();           strWrt.Close();           _proxy.ProcessMessage("Client",msg.Headers.Action, msgStr);           msg.Close();           reply = buf.CreateMessage();         }       } }


Note that the class defined in this code implements the IProxyMessageInspector interface of the Windows Communication Foundation. That interface defines a method called BeforeSendRequest() for operating on outgoing messages, and another called AfterReceiveReply() for operating on responses.

5.

The next step is to define the attribute class by which the proxy channel behavior will be attached to a channel. Do so by adding a new class module called AuditAttribute.cs to the ClientAuditBehavior project, and replacing the default code in that module with this code:

using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; namespace ClientAuditBehavior {        [AttributeUsage(AttributeTargets.Class)]        public class AuditAttribute: Attribute, IChannelBehavior        {                public void ApplyBehavior(                  ChannelDescription description,                  ProxyBehavior behavior,                  BindingParameterCollection parameters)                {                  ChannelFactory<IAudit> fact =                    new ChannelFactory<IAudit>("ClientAudit");                  IAudit prt = fact.CreateChannel();                  ClientAuditMessageInspector insp =                    new ClientAuditMessageInspector(prt);                  behavior.MessageInspectors.Add(insp);               }        } }


Creating the Client

Follow these steps to create a client for the chat/echo service to which the proxy channel behavior will be applied:

1.

Add a new C# Windows console project named Client to the solution.

2.

Add a reference to the System.ServiceModel assembly to that new project.

3.

Also add a reference to the ClientAuditBehavior project.

4.

Add a reference to the System.ServiceModel assembly to that new project.

5.

Right-click on the Service project in the solution and select Debug, and then Start New Instance from the content menu.

6.

Open the Microsoft Windows Vista DEBUG Build Environment prompt, make the directory containing the Client project file the current directory, and enter this command:

svcutil http://localhost:8000/Behaviors /out:proxy.cs /config:app.config


7.

Stop debugging the solution and add the files proxy.cs and app.config output by the command in the preceding step to the Client project.

8.

Replace the default code in the Program.cs module with this code that uses the generated proxy class in the Proxy.cs module, adding the proxy channel behavior to it first:

using System; using System.Collections.Generic; using System.Text; using System.ServiceModel; namespace Client {     class Program     {        static void Main(string[] args)        {          ChatProxy c = new ChatProxy();          c.ChannelFactory.Description.Behaviors.Add(            new ClientAuditBehavior.AuditAttribute());          System.Console.WriteLine("Welcome to Chat");          System.Console.WriteLine();          System.Console.WriteLine("Enter text below and press ENTER to send");          string cont = Console.ReadLine();          while (cont != "q")          {            Console.WriteLine("Server Said:" + c.Talk(cont));            cont = Console.ReadLine();          }        }     } }


9.

Modify the app.config file of the Client project by adding this second endpoint subelement to the client element to define how the proxy channel auditing behavior is to communicate with the auditing service:

<endpoint   name="ClientAudit"   address="http://localhost:8000/Auditor"   binding="basicHttpBinding"   contract ="ClientAuditBehavior.IAudit" />


Testing the Solution

The solution is now complete. Execute these steps to see it in action:

1.

Right-click on the solution and select Set Startup Projects from the menu.

2.

Configure the Startup project properties as shown in Figure 9.3.

Figure 9.3. Startup project configuration.


3.

Start debugging the solution. Three console windows will open: one for the audit service, one for the chat/echo service, and one for the client.

4.

When there is activity in all three console windows, type some text into the client window and press the Enter key. The expected result is shown in Figure 9.4: The client sends a message to the service, the dispatch behavior on the service passes a copy of the message to the auditing service, and, when the service's response is received by the client, the proxy channel behavior on the client passes a copy of that response to the auditing service as well.



Figure 9.4. The solution in action.





Presenting Microsoft Communication Foundation. Hands-on.
Microsoft Windows Communication Foundation: Hands-on
ISBN: 0672328771
EAN: 2147483647
Year: 2006
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net