在没有 WCF 服务主机的情况下部署 WCF 服务主机应用

本文关键字:主机 WCF 服务 部署 应用 情况下 | 更新日期: 2023-09-27 18:33:36

我有一个由一个简单的WinForms应用程序托管的WCF服务。在Visual Studio中,该服务通过内置的WCF服务主机启动,一切正常。我现在想在我的主机应用中托管服务,但目前该服务未运行。

服务应用:

  ServiceHost host;
    private void btnStart_Click(object sender, EventArgs e)
    {
        host = new ServiceHost(typeof(RegimesService));
        host.Open();
        lblStatus.Text = "Started...";
    }

如果我在系统托盘中关闭 WCF 服务主机并单击我的开始按钮,代码将运行,但服务永远不会启动?

这是我的服务主机应用配置:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
       <service name="diiRegimesService.RegimesService">
        <endpoint address="" binding="basicHttpBinding" contract="diiRegimesService.IRegimesService"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

最后是我的服务项目App.config:

 <system.serviceModel>
    <services>
      <service name="diiRegimesService.RegimesService">
        <endpoint address="" binding="basicHttpBinding" contract="diiRegimesService.IRegimesService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="20000000"
                 maxBufferSize="20000000"
                 maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32"
               maxArrayLength="200000000"
               maxStringContentLength="200000000"/>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

当我从cmd行运行netstat -a时,端口http://localhost:8080没有侦听,所以就像服务对象从未真正初始化过一样。我确定这是我在某个地方的配置中的一个简单的错误,有什么想法吗?感谢

在没有 WCF 服务主机的情况下部署 WCF 服务主机应用

>端口 8080 是 SQL Server 的保留端口,您的服务 App.config 和主机 App.config 在其基址中具有不同的端口

好的,使用了解决方案资源管理器中的WCF配置向导,并将ServiceBehaviour绑定到主机应用程序的App.Config中的服务定义,嘿,一切都连接起来了!感谢您@Akshay乔杜里的帮助。