Programing

WCF 3.0에서 클라이언트 IP 주소 얻기

lottogame 2020. 9. 23. 08:02
반응형

WCF 3.0에서 클라이언트 IP 주소 얻기


WCF 3.5에서는 클라이언트 IP 주소를 쉽게 얻을 수 있지만 WCF 3.0에서는 쉽게 얻을 수 없습니다. 누구든지 어떻게 알아?


이것은 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;

(a) 서비스가 웹 서비스에서 호스팅되고 (분명히) (b) 다음과 같이 AspNetCompatibility 모드를 활성화하는 한 가능합니다.

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

그런 다음 다음을 통해 IP 주소를 얻을 수 있습니다.

HttpContext.Current.Request.UserHostAddress

.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

참고 URL : https://stackoverflow.com/questions/93162/obtaining-client-ip-address-in-wcf-3-0

반응형