82

どうやら、WCF 3.5 ではクライアント IP アドレスを簡単に取得できますが、WCF 3.0 では取得できません。誰でも方法を知っていますか?

4

3 に答える 3

152

これは 3.0 では役に立ちませんが、3.5 でクライアント IP アドレスを取得しようとしているために、この質問を見つけてイライラしている人を見ることができます。したがって、動作するはずのコードを次に示します。

using System.ServiceModel;
using System.ServiceModel.Channels;

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
    prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
于 2008-09-18T15:11:10.533 に答える
36

(a)サービスが(明らかに)Webサービスでホストされており、(b)次のようにAspNetCompatibilityモードを有効にしている限り、可能であることがわかります。

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

そして、次の方法でIPアドレスを取得できます。

HttpContext.Current.Request.UserHostAddress
于 2008-10-09T16:34:44.277 に答える
16

.NET 3.0 SP1 をターゲットにしている場合は可能です。

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;

クレジット: http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx

参照: http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.remoteendpointmessageproperty.aspx

于 2008-10-10T08:26:25.010 に答える