[英]WCF Service Hosted in a Managed Windows Service
我遵循了此链接( 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,我打开了另一个问题,我在本地计算机上安装服务时遇到问题。 我打开了另一个问题: 无法使用sc命令安装服务
我想到了几件事。
首先(我不确定100%,但这是基于我的经验),您无法通过Visual Studio将Windows服务作为Windows服务运行。 您需要按照链接到的页面上的指示构建项目,然后安装它。
其次,您只需要两个配置文件,而不是三个-一个用于Windows Service(该服务用于配置),另一个用于客户端。 我不确定您在服务主机配置文件中所扮演的角色(或认为自己扮演的角色)。
第三,您的客户端配置在<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 Service,安装并启动它,然后在客户端(WinForm)应用程序中为其添加服务引用。 这将生成您所需的所有内容,您可以查看配置文件以查看所需的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.