C#WCF-查找被调用的终结点的名称

本文关键字:结点 C#WCF- 调用 查找 | 更新日期: 2023-09-27 17:59:07

如何在授权管理器中找到为WCF服务调用的端点?

当前代码:

 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";
     }
}

我想要被调用的端点,但目前的结果是:

MYWCFSERVICE

https://myurl.co.uk/mywcfservice.svcSystem.ServiceModel.Dispatcher.PrefixEndpointAddressMessageFilter

我需要的是.svc后面的部分https://myurl.co.uk/mywcfservice.svc/testConnection?param1=1

在这个场景中,我希望返回"testConnection"。

C#WCF-查找被调用的终结点的名称

看看这个答案。

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);
     }
}

感谢Smoksness!把我送到了正确的方向。

我做了一个函数,返回名为:的操作

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);
        }
    }