托管在Windows服务托管的WCF服务

问题描述:

我遵循此(http://msdn.microsoft.com/en-us/library/ms733069.aspx)链接并创建了服务和服务主机。 我向解决方案添加了一个webform客户端项目。为了检查我的服务是否收到请求,我在服务中添加了一个日志。 我选择了我的主机和客户端,通过设置多个启动项目同时运行。 但我有一个问题,使我的服务和客户端之间的沟通。我缺少配置中的东西?我没有看到异常(尽管我选择了CLR和JSRuntime异常,并且提供了托管调试帮助)。托管在Windows服务托管的WCF服务

这里是我的服务配置

<?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

想到几件事情。

首先(我不是100%确定,但这是基于我的经验),您无法通过Visual Studio将Windows服务作为Windows服务运行。您需要构建该项目,然后按照您链接到的页面上的指示进行安装。其次,您只需要两个配置文件,而不是三个 - Windows服务(这是服务的配置所在的位置)和一个客户端。我不确定你有什么作用(或者相信你有)服务主机配置文件。

第三,您的客户端配置在<system.serviceModel>部分有一个服务条目 - 如果您的客户端还托管服务,您只需要这些服务,但在您提出的问题中似乎不是这种情况。您应该删除<services>部分,并添加一个<client>部分,像这样:

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

请注意,我用了bindingConfiguration属性之上 - 不这样做,你的客户会使用默认basicHttpBinding(在你的情况并不重要因为你没有设置名称以外的任何其他名称,但是如果你设置了非默认值,你会想告诉客户端使用哪个绑定配置)。

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

+0

感谢您的回复。当你说“(在你的情况下,因为你没有设置除名称以外的任何内容都不重要”,你能详细说明你认为我设置名称的位置吗?它是否在服务中?另一件事情,因为我删除了标签客户端是否需要删除标签? – HXD

+0

@HXD - 对不起,我在答案的这一部分并不十分清楚,我的意思是你在配置文件中定义了一个'basicHttpBinding'配置,命名为“InboundMessage.Service.Operator”。您不会在端点中引用该绑定配置,因此不会使用它。由于您没有为“InboundMessage.Service.Operator”绑定配置指定任何值,因此无关紧要。例如,如果您指定了更大的邮件大小限制,那么这很重要。是的,你也可以删除''标签。 – Tim

+0

谢谢蒂姆我有问题在我的本地机器上安装服务。我打开另一个问题:http://*.com/questions/22416910/unable-to-install-service-using-sc-command – HXD