问题
I have a problem with configuration and starting WCF service. In my application there is a method that starts service. Something like this
void Start(string protocol, string address, string port)
{
host = new ServiceHost(_myService,
new Uri(String.Format("{0}://{1}{2}/Sample", protocol, address, port)));
//...Some configuration (bindings, behaviors, etc.)
host.Open();
}
Let my computer has an IP 192.168.0.1. When I pass 'address' parameter with a value '192.168.0.2' an error occurred
"A TCP error (10049: The requested address is not valid in its context.)
occurred while listening on IP Endpoint=192.168.0.2:1234"
That's right because it's not my IP. But after that if I pass correct value (my real IP) I get the same error about IP 192.168.0.2! So I can't reconfigure and restart server without restarting application.
Why does it happen? How can I avoid such behavior?
回答1:
I can't see from your question how you are adding the correct endpoint, but I suspect you are attempting to modify the endpoint address. With WCF services, you cannot make changes to the endpoint address after calling
host.Open();
because at this point the service is (if you have no errors) up and accepting requests from clients at the specified address and port number.
You need to create a new ServiceHost object with the correct endpoint address (and dispose of the old one) if you wish to host the service at a new address.
EDIT:
After playing around with the example solution you have posted, I have found a solution to the issue. I think something is going wrong because you are using the same port number for both tries (in the example solution I downloaded you don't specify this, so the port defaulted to 808). The error you are experiencing vanishes if you change your code as follows to specify a different port number in the base address for the 2nd try:
try
{
var host2 = CreateServiceHost("localhost:5432", serviceImpl);
Console.WriteLine("#2, config: " + host2.BaseAddresses.First().ToString());
host2.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
There seems to be something buggy underneath WCF itself, probably on a socket level, where the port is somehow still unavailable after the first error with the incorrect IP.
I did a quick google and found this article where someone experienced a delay in reusing a port after closing a socket. If you always need to use the same port number, perhaps you could wait a certain amount of time for the port to become free again before trying to create the service host again.
回答2:
Looks like WCF caches a socket object in class ExclusiveTCPTransportManager.
It's seems to be good solution for me:
public static class WCFBugWorkaround
{
public static bool IsConnectionPossible(this ServiceHost host)
{
try
{
foreach (var baseAddress in host.BaseAddresses)
{
IPAddress[] ipAddresses = Dns.GetHostAddresses(baseAddress.DnsSafeHost);
IPAddress ipAddr = ipAddresses.Where(e => e.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();
if (ipAddr == null)
{
return false;
}
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
System.Net.IPEndPoint localEP = new IPEndPoint(ipAddr, baseAddress.Port);
s.Bind(localEP);
}
}
}
catch (Exception ex)
{
return false;
}
return true;
}
}
ServiceHost host = ...;
...
if (host.IsConnectionPossible())
{
host.Open();
}
Thank's to Francheska for showing me the right direction :)
来源:https://stackoverflow.com/questions/6649565/problem-with-configuring-and-starting-wcf-service