Search code examples
c#wcf

Why doesn't the service start?


I am trying to start a self host service, so the process is this:

  • With the WCF template of VS2017 I create a library with the service and the contract.
  • I create a new WPF project that references to this library to can use the service. This WPF application will host the service.

  • I copy all the configuration from app.config of the library that is created with the VS2017 template to the app.config of the WPF application. Just I modify the URL, to avoid conflicts, the URL will be Service2 instead of Service1. This is because if I start to debug, visual studio start the service of the library and the service of the WPF application.

The problem is that the service of hosted in the WPF application is not started. Also I have tried to unload the library project to run only the WPF project, to avoid to start services that I don't want to run, but the problem is the same, the service is not started.

The app.config is this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Servicio.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/Servicio/Service2/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="Servicio.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

The code bihind of my WPF project:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        using (_host = new ServiceHost(typeof(Servicio.Service1)))
        {
            _host.Open();
        }
    }

    private ServiceHost _host;
}

Why the service is not started?


Solution

  • Well, you are creating a host, opening it and then dispose it immediately due to the using statement. So this is expected behavior.

    You should manually dispose the ServiceHost instance when the form instance is disposed.

        public MainWindow()
        {
            InitializeComponent();
    
            _host = new ServiceHost(typeof(Servicio.Service1)))
            _host.Open();
        }
    

    then you can dispose the host using _host.Dispose(); when the form is disposed.