Reputation: 157
I am using the following code in my service
public class HeartBeat : IHeartBeat
{
public string GetData()
{
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
return "IP address is: " + ip;
}
}
I noticed that if my web config is:
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
I can get the IP address successfully. However, if i use dual http binding like this:
<protocolMapping>
<add binding="wsDualHttpBinding" scheme="http" />
</protocolMapping>
I am getting a null return. Is there any other way to get IP address of client in wsDualHttpBinding? Thank you in advance
Upvotes: 0
Views: 1873
Reputation: 157
Finally figured it out using @Neel
public class HeartBeat : IHeartBeat
{
public string GetData()
{
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint =
prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
EndpointAddress test = OperationContext.Current.Channel.RemoteAddress;
IPHostEntry ipHostEntry = Dns.GetHostEntry(System.ServiceModel.OperationContext.Current.Channel.RemoteAddress.Uri.Host);
foreach (IPAddress ip2 in ipHostEntry.AddressList)
{
if (ip2.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
//System.Diagnostics.Debug.WriteLine("LocalIPadress: " + ip);
return ip2.ToString();
}
}
return "IP address is: " + System.ServiceModel.OperationContext.Current.Channel.RemoteAddress.Uri.Host;
}
}
Upvotes: 0
Reputation: 4588
This happens because RemoteEndpointMessageProperty.Address
property appears empty in case of wsDualHttpBinding
.
The RemoteEndpointMessageProperty
uses the HttpApplication.Request.UserHostAddress
to return the IP.
however, the HttpContext
is not available with WSDualHttpBinding
, causing a "Request is not available in the context" exception.
You can try to access host property like below for dual channel.
if (string.IsNullOrEmpty(endpoint.Address))
{
string clientIpOrName = System.ServiceModel.OperationContext.Current.Channel.RemoteAddress.Uri.Host;
}
Upvotes: 1