簡體   English   中英

WCF operationContext返回網關而不是客戶端IP地址

[英]WCF operationContext returns gateway instead of client ip address

對開發WCF服務來說有點新。 我正在使用此代碼來獲取客戶端IP地址,

private String getRemoteIP()
{
    OperationContext context = OperationContext.Current;
    MessageProperties prop = context.IncomingMessageProperties;
    RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
    return endpoint.Address;
}

在我的本地主機中,它工作正常,但是當我在服務器上安裝我的服務進行測試時,得到的回報是客戶端的網關,對此原因的任何見解都可以?

請嘗試以下一種方法:首先啟用AspNetCompatibility模式,如下所示:

<system.serviceModel>
        <!-- this enables WCF services to access ASP.Net http context -->
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

然后您可以通過以下方式獲取IP地址:

HttpContext.Current.Request.UserHostAddress

暫無
暫無

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

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