How to get OperationDescription from OperationContext in AfterReceiveRequest in WCF?

In my implementation of IDispatchMessageInspector on AfterReceiveRequest I want to check if OperationBehavior is applied to the current operation being called. So do I need to access the OperationDescription of the operation that is about to be called? Is there any direct way instead of comparing the action of the current operation with everyone in DispatchRuntime?

Thank..

+3
source share
2 answers

I had the same problem and solved below.

OperationContext ctx = OperationContext.Current;
ServiceDescription hostDesc = ctx.Host.Description;
ServiceEndpoint endpoint = hostDesc.Endpoints.Find(ctx.IncomingMessageHeaders.To);
string operationName = ctx.IncomingMessageHeaders.Action.Replace(
  endpoint.Contract.Namespace + endpoint.Contract.Name + "/", "");
OperationDescription operation =
endpoint.Contract.Operations.Find(operationName);

This solution is offered on the msdn forum.

+6
source

Noel Bj Kim's suggested answer works, except for this line, is problematic:

ServiceEndpoint endpoint = hostDesc.Endpoints.Find(ctx.IncomingMessageHeaders.To);

, WCF IIS Uri, , - "localhost", (, "pc1.domain1.net" ), "localhost".

, , , , . http://localhost/Service1 http://pc1.domain.net/Service1.

/// <summary>
/// Find a service endpoint by partially matching the uri, but only against the Scheme and PathAndQuery elements.
/// This avoids issues with IIS hosting, where the actual Uri stored in the ServiceEndpoint may not exactly
/// match the one used in configuration.
/// </summary>
/// <param name="uri">Uri to match against</param>
/// <returns>A matching ServiceEndpoint, or null if no match was found.</returns>
private ServiceEndpoint FindServiceEndpointBySchemeAndQuery(Uri uri)
{
    foreach (var endpoint in OperationContext.Current.Host.Description.Endpoints)
    {
        if (endpoint.Address.Uri.Scheme == uri.Scheme
            && endpoint.Address.Uri.PathAndQuery == uri.PathAndQuery)
        {
            return endpoint;
        }
    }
    return null;
}

:

ServiceEndpoint endpoint = FindServiceEndpointBySchemeAndQuery(ctx.IncomingMessageHeaders.To);
+1

Source: https://habr.com/ru/post/1737453/


All Articles