簡體   English   中英

C#WCF-查找被調用的端點的名稱

[英]C# WCF - Find Name of endpoint that was called

如何在授權管理器中找出為我的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”。

看看這個答案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM