wcf - Using an Attribute (IOperationBehavior) on all service methods -
i using following [trackingbehaviorattribute] custom ioperationinvoker on [operationcontract] , fine.
but seek way able add attribute on interface level ([servicecontract] level). want avoid manual process of adding attribute on every method inside service.
using system.reflection; using system.servicemodel; using system.servicemodel.description; using system.servicemodel.dispatcher; using system.servicemodel.channels; public class trackingbehaviorattribute : attribute, ioperationbehavior { #region ioperationbehavior members public void addbindingparameters(operationdescription operationdescription, bindingparametercollection bindingparameters) { } public void applyclientbehavior(operationdescription operationdescription, clientoperation clientoperation) { } public void validate(operationdescription operationdescription) { } public void applydispatchbehavior(operationdescription operationdescription, dispatchoperation dispatchoperation) { methodinfo currmethodinfo = operationdescription.syncmethod; customoperationinvoker invoker = new customoperationinvoker (dispatchoperation.invoker, currmethodinfo); dispatchoperation.invoker = invoker; invoker.oninvokeended += new eventhandler<trackingeventargs>(/* logwritermethod! */); } #endregion } }
i have tried modify attribute include iservicebehavior follows:
public class trackingbehaviorattribute : attribute, iservicebehavior, ioperationbehavior { #region iservicebehavior members public void addbindingparameters(servicedescription servicedescription, servicehostbase servicehostbase, system.collections.objectmodel.collection<serviceendpoint> endpoints, bindingparametercollection bindingparameters) { } public void applydispatchbehavior(servicedescription servicedescription, servicehostbase servicehostbase) { foreach () { //some loop add attribute service operation. } } public void validate(servicedescription servicedescription, servicehostbase servicehostbase) { } #endregion
but cant seem work. approaching wrong?
update
i have of @erniel got attribute work on service-level when applied multiple services following error when starting host:
"the value not added collection, collection contains item of same type: 'tracking.trackingservicebehaviorattribute'. collection supports 1 instance of each type."
how work around this?
the loop looking this:
public virtual void applydispatchbehavior(servicedescription servicedescription, servicehostbase servicehostbase) { foreach (serviceendpoint endpoint in servicedescription.endpoints) { foreach (operationdescription operation in endpoint.contract.operations) { // trackingbehaviorattribute code implements ioperationbehavior operation.behaviors.add(new trackingbehaviorattribute()); } } }
if need more examples carlos figueira extensibility blog place read.
Comments
Post a Comment