Подтвердить что ты не робот

Получение IP-адреса клиента в WCF 3.0

Очевидно, вы можете легко получить IP-адрес клиента в WCF 3.5, но не в WCF 3.0. Кто-нибудь знает как?

4b9b3361

Ответ 1

Оказывается, вы можете, если (а) ваша служба размещается в веб-службе (очевидно) и (б) вы активируете режим AspNetCompatibility следующим образом:

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

И затем вы можете получить IP-адрес:

HttpContext.Current.Request.UserHostAddress

Ответ 2

Это не поможет вам в версии 3.0, но я могу просто увидеть, как люди находят этот вопрос и расстраиваются, потому что пытаются получить IP-адрес клиента в 3.5. Итак, вот какой код, который должен работать:

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;

Ответ 3

Вы можете настроить таргетинг на .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