Reputation: 243
My goal is to create an endpointbehavior for WCF, which adds an extra Ping()
operation to an existing contract. The EndpointBehavior
works fine, I can actually see the added Ping()
operation when I run my service and use the WCF test client. I've implemented this endpointbehavior as follows:
Configuration:
<configuration>
<system.serviceModel>
<services>
<service name="X">
<endpoint address="mex" kind="mexEndpoint" />
<endpoint address="" binding="basicHttpBinding" contract="IX"
behaviorConfiguration="ping" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ping">
<PingEndpointBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="PingEndpointBehavior"
type="Assembly.PingEndpointBehavior, Assembly, .. />
<behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
Endpoint behavior:
public class PingEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
{
...
public void ApplyDispatchBehavior(ServiceEndpoint endpoint,
EndpointDispatcher endpointDispatcher)
{
var cd = endpoint.Contract;
var inputMessage = new MessageDescription(...);
var outputMessage = new MessageDescription(...);
// Set input/output messages correctly
var od = new OperationDescription("Ping", cd);
od.Messages.Add(inputMessage);
od.Messages.Add(outputMessage);
od.Behaviors.Add(new DataContractSerializerOperationBehavior(od));
od.Behaviors.Add(new PingOperationBehavior());
endpoint.Contract.Operations.Add(od);
}
...
}
The operation behavior however, is not working. The ApplyDispatchBehavior()
method on the operationbehavior never gets called, and therefore, my own PingInvoker
is not used. This results in not getting a response when calling the added Ping()
operation to a contract.
The reason for this seems to be that the OperationBehavior
only gets added after the service has already started, resulting in the ApplyDispactchBehavior
not getting called. See code below:
Operation Behavior:
public class PingOperationBehavior : IOperationBehavior
{
...
public void ApplyDispatchBehavior(OperationDescription od, DispatchOperation do)
{
do.Invoker = new PingInvoker();
}
...
}
Invoker:
public class PingInvoker : IOperationInvoker
{
...
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
outputs = new object[0];
return Ping();
}
public static DateTime Ping()
{
return DateTime.Now;
}
...
}
Anyone have an idea how I can get the OperationBehavior
to work properly?
Note:
Upvotes: 3
Views: 2670
Reputation: 1295
I had this same problem with my custom Behavior. I was deriving from WebHttpBehavior (which inherits from IEndpointBehavior), but the ApplyDispatchBehavior was not being called.
The solution to my problem was to add the missing override keyword like: public override void ApplyDispatchBehavior(...
Upvotes: -2