7
Answers

WCF Service Host Crashing

Michael Laterza

Michael Laterza

14y
9k
1

Hello,
I have a WCF service that uses BasicHttpBinding and is self hosted as a console app.  When I start an instance of the host from Visual Studio, or if I start it by running the .exe directly from Windows, I can run my test app and the client and service communicate perfectly.
I then have another WCF service that is used for starting the above host simply by calling the same .exe mentioned above.  When I call the above host this way, it starts and then immediately crashes.  I get the following error from the Service Trace Viewer:  No existing transport manager was found for the specified URI.  I'm not sure if this might be what is causing the host to crash, or if it is just a result of the host already not being available.
When I run the host directly there is a Console.ReadLine() statement immediately before the selfHost.Close() statement which works when you press a key to close the console app.  When the host is called from my other service by creating a new Process and running the same .exe, it crashes.  I'm not sure how using Console.ReadLine() to pause the app works when the window is not visible when calling it from the other service.  Could it somehow just be skipping the Console.ReadLine() and executing the selfHost.Close() statement because the window is not visible?
I'm really at a loss...
Also, if it will help, here is the code for the Host:
public class Program
  {
    static void Main(string[] args)
    {
      Host app = new Host();
      app.RunWCFHost();
    }
  }

  public class Host
  {
    private ServiceHost selfHost;


    /// <summary>
    /// Function creates the URI, creates the ServiceHost, adds the service endpoint, enables metadata exchange,
    /// starts the service, and once the user presses Enter, closes the service.
    /// </summary>
    public void RunWCFHost()
    {
      int availablePort = GetAvailablePort.GetPort();

      Uri baseAddress = new Uri("http://localhost:" + availablePort + "/MyServer");

      if (selfHost != null)
      {
        selfHost.Close();
        selfHost = null;
      }

      // Create the ServiceHost.
      selfHost = new ServiceHost(typeof(ServiceLibrary.MyService), baseAddress);

      try
      {
        // Configure the Binding.
        var binding = new BasicHttpBinding();
        binding.TransferMode = TransferMode.Streamed;
        
        // Add a ServiceDiscoveryBehavior.
        selfHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());

        // Add a UdpDiscoveryEndpoint.
        selfHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

        // Add a service endpoint.
        selfHost.AddServiceEndpoint(typeof(IMyServiceContract), binding, baseAddress);

        // Start the service.
        selfHost.Open();
        Console.WriteLine("MyServer Started");
        Console.WriteLine("Press <Enter> to terminate the service.");
        Console.WriteLine();
        Console.ReadLine();

        // Close the ServiceHostBase to shutdown the service.
        selfHost.Close();
      }
      catch (CommunicationException ce)
      {
        Console.WriteLine("An exception occurred: {0}", ce.Message);
        selfHost.Abort();
      }
    }
  }

Answers (7)