简体   繁体   English

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

[英]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? 如何在授权管理器中找出为我的WCF服务调用的端点?

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 MYWCFSERVICE

https://myurl.co.uk/mywcfservice.svc System.ServiceModel.Dispatcher.PrefixEndpointAddressMessageFilter 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 我需要的是.svc之后的部分,例如/ https://myurl.co.uk/mywcfservice.svc/testConnection?param1=1

In this scenario I want "testConnection" to be returned. 在这种情况下,我希望返回“ testConnection”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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