wcfservicebehavior

WCF Service Behavior to add an operation to my services


I'd like to create a service behavior which adds an extra service operation to my service. This way I can add the behavior to any of my services (existing and future) and my service will all be able to perform this extra service operation. (e.g. ping or returns some other service information)

I'm not sure what to do here to achieve what I want, What I've tried to do so far is to implement a service behavior which tries to modify the service description. Should I add an endpoint and then specify a new contract on that?


public class MyOperationBehavior : IServiceBehavior
{
   void IServiceBehavior.ApplyDispatchBehavior(
                    ServiceDescription desc, ServiceHostBase host)
    {
       ServiceEndpointCollection sec = svcDesc.Endpoints;
       ServiceEndpoint se = new ServiceEndpoint()
       {
            se.Address = "DoMyOperation";
            se.Binding = "basicHttpBinding";
            se.Contract = MyCustomContract;           
       }

    }
}


Solution

  • I've found what I was looking for in this article on Dynamically adding methods to a WCF service without having to add it to each service contract.

    But I also believe using WS-Discovery is a much better way of achieving what I want: exposing information about a service without having to touch the service contracts