2016-07-28 8 views
1

如何找到在授權管理器中爲我的WCF服務調用的端點?C#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後的部分eg/ https://myurl.co.uk/mywcfservice.svc/testConnection?param1=1

在這種情況下,我想要返回「testConnection」。

回答

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

謝謝哥們,得到通過的OperationContext動作: operationContext.RequestContext.RequestMessage.Headers.To.ToString() – markthewizard1234

+0

酷。那麼你應該發佈你自己的答案來啓發未來的讀者。 :) – smoksnes

+0

我已經添加它作爲一個單獨的功能作爲答案 – markthewizard1234

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