Görünüşe göre WCF 3.5'te kolayca bir istemci IP adresi elde edebilirsiniz, ancak WCF 3.0'da elde edemezsiniz. Nasıl olduğunu bilen var mı?
Yanıtlar:
Bu 3.0'da size yardımcı olmuyor, ancak insanların bu soruyu bulduğunu ve 3.5'te istemci IP adresini almaya çalıştıkları için hayal kırıklığına uğradığını görebiliyorum. İşte çalışması gereken bazı kodlar:
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) hizmetiniz bir Web Hizmetinde barındırıldığı (açıkça) ve (b) AspNetCompatibility modunu etkinleştirdiğiniz sürece şunları yapabilirsiniz:
<system.serviceModel>
<!-- this enables WCF services to access ASP.Net http context -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...
</system.serviceModel>
Ve sonra IP adresini şu şekilde alabilirsiniz:
HttpContext.Current.Request.UserHostAddress
HttpContext.Current.Request.UserHostAddress
.NET 3.0 SP1'i hedefliyorsanız bunu yapabilirsiniz.
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
Krediler: http://blogs.msdn.com/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx