【问题标题】:WCF Service Hosted in a Managed Windows Service托管在托管 Windows 服务中的 WCF 服务
【发布时间】:2014-03-15 11:09:00
【问题描述】:

我按照这个(http://msdn.microsoft.com/en-us/library/ms733069.aspx) 链接创建了一个服务和一个服务主机。 我在解决方案中添加了一个 webform 客户端项目。为了检查我的服务是否正在接收请求,我在服务中添加了一个日志。 我通过设置多个启动项目选择了我的主机和客户端同时运行。 但是我在我的服务和客户端之间进行通信时遇到问题。我在配置中遗漏了什么吗?我根本看不到异常(即使我选择了 CLR 和 JSRuntime 异常,并管理调试帮助)。

这是我的服务配置

    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
        <client/>
        <behaviors>
          <serviceBehaviors>
            <behavior name="meta">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="InboundMessage.Service.Operator" behaviorConfiguration="meta" >
            <endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator" name="basic"/>
            <endpoint address="mex" binding="mexHttpBinding"  contract="IMetadataExchange" />
            <!--<endpoint address="" binding="wsHttpBinding" contract="IMetadataExchange" name="Ws"  />-->
            <host>
              <baseAddresses>
                <add baseAddress = "http://IP/InboundMessage.Service/"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
          <basicHttpBinding>
            <binding name="InboundMessage.Service.Operator"/>
          </basicHttpBinding>
        </bindings>
      </system.serviceModel>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
    </configuration>

服务主机:

     <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="meta">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <diagnostics performanceCounters="ServiceOnly" />
        <services>
          <service name="InboundMessage.Service.Operator" behaviorConfiguration="meta">
            <endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator" name="basic"/>
            <endpoint address="mex" binding="mexHttpBinding"  contract="IMetadataExchange" />
            <!--<endpoint address="" binding="wsHttpBinding" contract="IMetadataExchange" />-->
          </service>
        </services>
      </system.serviceModel>
      <system.web>
        <compilation
            debug="true" >
        </compilation>
      </system.web>
      <system.webServer>
        <directoryBrowse enabled="true"/>
      </system.webServer>
    </configuration>

一个windowform客户端配置:

        <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
      </configSections>

      <system.web>
        <compilation debug="true"></compilation>
      </system.web>
      <system.serviceModel>
        <services>
          <service  behaviorConfiguration="meta" name="InboundMessage.Service.Operator">
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://IP/InboundMessage.Service/"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
         <basicHttpBinding>    
            <binding name="InboundMessage.Service.Operator"/> 
          </basicHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="meta">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>     
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>                    

编辑: 使用 Tim 的评论来安装该服务,但我在安装它时遇到问题。 我打开了另一个问题,感谢蒂姆,我在本地机器上安装服务时遇到问题。我又开了一个问题:Unable to install service using sc command

【问题讨论】:

    标签: c# .net wcf app-config


    【解决方案1】:

    我想到了一些事情。

    首先(我不能 100% 确定,但这是基于我的经验)您不能通过 Visual Studio 将 Windows 服务作为 Windows 服务运行。您需要按照链接到的页面上的说明构建项目,然后安装它。

    其次,您只需要两个配置文件,而不是三个 - 一个用于 Windows 服务(这是服务的配置所在),一个用于客户端。我不确定您对服务主机配置文件拥有(或相信您拥有)什么角色。

    第三,您的客户端配置在 &lt;system.serviceModel&gt; 部分中有服务条目 - 只有当您的客户端也托管服务时,您才需要这些条目,而您提出的问题似乎并非如此。您应该删除 &lt;services&gt; 部分并添加 &lt;client&gt; 部分,如下所示:

    <client>
      <endpoint address="http://IP/InboundMessage.Service" 
                binding="basicHttpBinding" 
                bindingConfiguration="InboundMessage.Service.Operator" 
                contract="InboundMessage.Service.IOperator" />
    </client>
    

    请注意,我使用了上面的 bindingConfiguration 属性 - 没有它,您的客户端将使用默认的 basicHttpBinding(在您的情况下这无关紧要,因为您没有设置名称以外的任何内容,但是如果您设置了非默认值,您希望告诉客户端使用哪个绑定配置)。

    实际上(开始)最简单的方法是构建 Windows 服务,安装并启动它,然后在您的客户端 (WinForm) 应用程序中添加对它的服务引用。这将生成您需要的所有内容,您可以查看配置文件以查看您需要的设置。

    【讨论】:

    • 感谢您的回复。当您说“(在您的情况下这无关紧要,因为您没有设置除名称以外的任何内容,”您能否详细说明您认为我设置名称的位置?它在服务中吗?自从我删除后的另一件事 来自客户端的标签我是否也需要删除 标签?
    • @HXD - 抱歉,我对答案的那部分不是很清楚。我的意思是你在配置文件中定义了一个basicHttpBinding 配置,名为“InboundMessage.Service.Operator”。您没有在端点中引用该绑定配置,因此不会使用它。由于您没有为“InboundMessage.Service.Operator”绑定配置指定任何值,因此没关系。例如,如果您指定了更大的消息大小限制,那么这很重要。是的,您也可以删除 &lt;behaviors&gt; 标记。
    • 感谢蒂姆,我在本地计算机上安装服务时遇到问题。我打开了另一个问题:*.com/questions/22416910/…
    最近更新 更多