Sunday, November 4, 2007

WSDualHttpBinding cross machine problem solved.

public static class WsDualProxyHelper
{
public static void SetClientBaseAddress(DuplexClientBase proxy, int port)
where T : class
{
WSDualHttpBinding binding = proxy.Endpoint.Binding as WSDualHttpBinding;
Debug.Assert(binding != null);
string strHostName = Dns.GetHostName();
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress [] addr = ipEntry.AddressList;
binding.ClientBaseAddress = new Uri("http://" + addr[0].ToString() + ":" + port + "/");
}
public static void SetClientBaseAddress(DuplexClientBase proxy)
where T : class
{
lock (typeof(WsDualProxyHelper))
{
int portNumber = FindPort();
SetClientBaseAddress(proxy, portNumber);
try
{
proxy.Open();
}
catch
{
}
}
}
internal static int FindPort()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
using (Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp))
{
socket.Bind(endPoint);
IPEndPoint local = (IPEndPoint)socket.LocalEndPoint;
return local.Port;
}
}
}

No comments: