简体   繁体   中英

C# WCF - Find Name of endpoint that was called

How do I find out the endpoint that was called for my WCF service within the authorisation manager?

Current Code:

 public class AuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
      Log(operationContext.EndpointDispatcher.ContractName);
      Log(operationContext.EndpointDispatcher.EndpointAddress);
      Log(operationContext.EndpointDispatcher.AddressFilter);
      //return true if the endpoint = "getDate";
     }
}

I want the endpoint that was called, but the results are currently:

MYWCFSERVICE

https://myurl.co.uk/mywcfservice.svc System.ServiceModel.Dispatcher.PrefixEndpointAddressMessageFilter

What I need is the part after the .svc eg/ https://myurl.co.uk/mywcfservice.svc/testConnection?param1=1

In this scenario I want "testConnection" to be returned.

Check out this answer.

public class AuthorizationManager : ServiceAuthorizationManager
{
    protected override bool CheckAccessCore(OperationContext operationContext)
    {
        var action = operationContext.IncomingMessageHeaders.Action;

        // Fetch the operationName based on action.
        var operationName = action.Substring(action.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);

        // Remove everything after ?
        int index = operationName.IndexOf("?");
        if (index > 0)
            operationName = operationName.Substring(0, index);

        return operationName.Equals("getDate", StringComparison.InvariantCultureIgnoreCase);
     }
}

Thanks Smoksness! Sent me in the correct direction.

I've made a function that returns the action called:

private String GetEndPointCalled(OperationContext operationContext)
    {
        string urlCalled = operationContext.RequestContext.RequestMessage.Headers.To.ToString();
        int startIndex = urlCalled.IndexOf(".svc/") + 5;
        if (urlCalled.IndexOf('?') == -1)
        {
            return urlCalled.Substring(startIndex);
        }
        else
        {
            int endIndex = urlCalled.IndexOf('?');
            int length = endIndex - startIndex;
            return urlCalled.Substring(startIndex, length);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM