“The communication object, System.ServiceModel.ServiceHost, cannot be used for communication because it is in the Faulted state.”
The exception above had no any useful information about where the real problem was. The logic of my program was simple:
using(ServiceHost host = new ServiceHost(
typeof(MyService), new Uri("http://localhost:8080/MyService"))
{
host.Open();
... ...
}
When I looked inside Output of VS 2005, I saw this log information:
'System.ServiceModel.AddressAlreadyInUseException'
But why WCF did not give me that exception directly? Finally, this blog explains the reason: Why "using" is bad for your WCF service host.
------------------------------------------
ServiceHost Host = null;
try
{
Host = new ServiceHost(MySingletonService);
Host.Open();
Console.ReadKey();
Host.Close();
}
finally
{
if (Host != null)
((IDisposable)Host).Dispose();
}
The configuration exception is thrown by the “Host.Open()” line, the code jumps into the finally block and tries to dispose the host. Here the host is not null but it is in a faulty state, this means that it cannot be disposed and this raises the second exception that you usually see on your application.
The lesson learn is “do not use ‘using’ to host your WCF service”.
------------------------------------------