I have several WCF SOAP services that programmatically expose an endpoint to which an IEndPointBehavior is attached.
var endpoint = AddServiceEndpoint(contractType,
basicBinding,
address);
_logger.Write("Adding behavior for service {0}, contract: {1}", serviceName, cd.Value.ContractType);
endpoint.Behaviors.Add(new MyBehavior());
In the behavior class, I add a message inspector to the Dispatch runtime:
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint,
EndpointDispatcher endpointDispatcher)
{
_logger.WriteError("Adding Inspector to endpoint {0}, contract {1}", endpoint.Address.ToString(), endpoint.Contract.Name);
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyInspector());
}
In the message inspector, I implemented the AfterReceiveRequest method like:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
_logger.WriteError("AfterReceiveRequest for {0} via {1}", request.Headers.Action, request.Properties.Via);
// implementation omitted...
return null;
}
Now I can see the 'Adding behavior' and 'Adding inspector' log messages for all my services, but the AfterReceiveRequest method is only called for some services.
The services for which the method is not called are running under a different web application in IIS, but I cannot find anything different that would explain why this inspector would not work. The services in which the inspector is not working are also deriving from a different base class, but I ripped out all the code from that base class and still the method is not called.
I turned on message tracing and do see that the client is calling the correct endpoint.
Does anyone have an idea why this may happen or how I can better debug this?
I finally found the problem with my code. Both WCF services have a WIF (.NET 4.5)
ClaimsAuthorizationManagerattached. The call toCheckAccesswas returningtruefor one service andfalsefor the other. The one where it was returning false, never went on to call theAfterReceiveRequestof the endpoint behavior.I extended Carlos Figueira's incredibly useful commandline tool with a WIF
ClaimsAuthenticationManagerandClaimsAuthorizationManagerand found that theirAuthenticateandCheckAccessmethods are called before theAfterReceiveRequeston theDispatchMessageInspector.As I needed an extension point that was called before the CheckAccess, I ended up using a class implementing
IInstanceContextProvider