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

时间:2016-07-28 12:53:13

标签: c# wcf authorization

如何在授权管理器中找到为我的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.svc System.ServiceModel.Dispatcher.PrefixEndpointAddressMessageFilter

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

在这种情况下,我想要" testConnection"被退回。

2 个答案:

答案 0 :(得分:1)

查看this回答。

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

答案 1 :(得分:1)

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