c# - Add Message Inspector to WCF service in code rather that config file -


i working through example microsoft training kit (wcf). entails adding message inspection service.

i have far create inspection implementation class, message behavior class , message behavior class extension.

instead of adding behavior through config file add in service host file. below implementation classes...

 public class messagetrace : idispatchmessageinspector     {         private message tracemessage(messagebuffer buffer)         {             message msg = buffer.createmessage();             stringbuilder sb = new stringbuilder("message content");             sb.append(msg.tostring());             console.writeline(sb.tostring());             return buffer.createmessage();         }          public object afterreceiverequest(ref message request, iclientchannel channel, instancecontext instancecontext)         {             request = tracemessage(request.createbufferedcopy(int32.maxvalue));             return null;         }          public void beforesendreply(ref message reply, object correlationstate)         {             reply = tracemessage(reply.createbufferedcopy(int32.maxvalue));         }     }  public class tracemessagebehavior : iendpointbehavior     {         public void addbindingparameters(serviceendpoint endpoint, bindingparametercollection bindingparameters)         {}          public void applyclientbehavior(serviceendpoint endpoint, clientruntime clientruntime)         {}          public void applydispatchbehavior(serviceendpoint endpoint, endpointdispatcher endpointdispatcher)         {             messagetrace inspector = new messagetrace();             endpointdispatcher.dispatchruntime.messageinspectors.add(inspector);         }          public void validate(serviceendpoint endpoint)         {}     }   public class tracemessagebehaviorextension : behaviorextensionelement     {         public override type behaviortype         {             { return typeof(tracemessagebehavior); }         }          protected override object createbehavior()         {             return new tracemessagebehavior();         }     } 

you can implement using code in following way.

  1. firstly using attribute on service class. create new attribute inheriting iservicebehavior.

    [attributeusage(attributetargets.class)] public class traceservicebehavior : attribute, iservicebehavior { public void addbindingparameters(servicedescription servicedescription, servicehostbase servicehostbase, system.collections.objectmodel.collection<serviceendpoint> endpoints, bindingparametercollection bindingparameters) { }  public void applydispatchbehavior(servicedescription servicedescription, servicehostbase servicehostbase) {     foreach (channeldispatcher cdispatcher in servicehostbase.channeldispatchers)     {         foreach (endpointdispatcher edispatcher in cdispatcher.endpoints)         {             edispatcher.dispatchruntime.messageinspectors.add(new messagetrace());         }     } }  public void validate(servicedescription servicedescription, servicehostbase servicehostbase) { } } 

and decorate service class attribute

[traceservicebehavior] public class service1 : iservice1 {      // methods } 
  1. create servicebehavior extending iservicebehavior, same code above, removing attribute only.

    public class traceservicebehavior : iservicebehavior {

    public void addbindingparameters(servicedescription servicedescription, servicehostbase servicehostbase, system.collections.objectmodel.collection<serviceendpoint> endpoints, bindingparametercollection bindingparameters) { }  public void applydispatchbehavior(servicedescription servicedescription, servicehostbase servicehostbase) {     foreach (channeldispatcher cdispatcher in servicehostbase.channeldispatchers)     {         foreach (endpointdispatcher edispatcher in cdispatcher.endpoints)         {             edispatcher.dispatchruntime.messageinspectors.add(new messagetrace());         }     } }  public void validate(servicedescription servicedescription, servicehostbase servicehostbase) { } } 

and in servicehost, adding behavior programatically.

servicehost host = new servicehost(typeof(wcfservice1.service1));             host.description.behaviors.add(new wcfservice1.traceservicebehavior()); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -