简体   繁体   English

托管在Windows服务中的WCF服务

[英]WCF Service Hosted in a Managed Windows Service

I followed this( http://msdn.microsoft.com/en-us/library/ms733069.aspx ) link and created a service and and a service host. 我遵循了此链接( http://msdn.microsoft.com/en-us/library/ms733069.aspx )链接,并创建了服务和服务主机。 I added a webform client project to the solution. 我在解决方案中添加了一个Webform客户端项目。 In order to check that my service is receiving a request I added a log in the service. 为了检查我的服务是否收到请求,我在服务中添加了一个日志。 I selected my host and client to run at the same time by setting multiple start up project. 我通过设置多个启动项目来选择要同时运行的主机和客户端。 But I am having a problem making a communication between my service and client.Am i missing something in the configuration? 但是我在服务和客户端之间进行通信时遇到问题,配置中是否缺少某些内容? i don't see exception at all(even though I selected CLR and JSRuntime exception, and managed debugging assistance ). 我根本看不到异常(即使我选择了CLR和JSRuntime异常,并管理了调试帮助)。

Here is my service configuration 这是我的服务配置

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

Service Host: 服务主机:

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

a windowform Client 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>                    

EDIT: Used Tim's comment to install the service but I am having problem installing it. 编辑:使用蒂姆的评论来安装服务,但我在安装它时遇到问题。 I opened another question thanks Tim i am having problem installing the service on my local machine. 感谢Tim,我打开了另一个问题,我在本地计算机上安装服务时遇到问题。 I opened another question : Unable to install service using sc command 我打开了另一个问题: 无法使用sc命令安装服务

A few of things come to mind. 我想到了几件事。

First (I'm not 100% sure, but this is based on my experiences) you can't run a Windows Service as a Windows Service through Visual Studio. 首先(我不确定100%,但这是基于我的经验),您无法通过Visual Studio将Windows服务作为Windows服务运行。 You need to build the project and then install it, as directed on the page you linked to. 您需要按照链接到的页面上的指示构建项目,然后安装它。

Secondly, you only need two configuration files, not three - one for the Windows Service (which is where the configuration for the service goes) and one for the client. 其次,您只需要两个配置文件,而不是三个-一个用于Windows Service(该服务用于配置),另一个用于客户端。 I'm not sure what role you have (or believe you have) for the service host config file. 我不确定您在服务主机配置文件中所扮演的角色(或认为自己扮演的角色)。

Third, your client config has entries for a service in the <system.serviceModel> section - you only need those if your client is also hosting a service, which doesn't appear to be the case in the question you've asked. 第三,您的客户端配置在<system.serviceModel>部分中包含服务的条目-仅当您的客户端还托管服务时才需要这些条目,在您所询问的问题中似乎并非如此。 You should remove the <services> section and add a <client> section, like this: 您应该删除<services>部分并添加一个<client>部分,如下所示:

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

Note that I used the bindingConfiguration attribute above - without that, your client will use the default basicHttpBinding (which in your case won't matter because you didn't set anything other than the name, but if you had set non-default values you would want to tell the client which binding configuration to use). 请注意,我使用了上面的bindingConfiguration属性-否则,您的客户端将使用默认的basicHttpBinding (在您的情况下,这没有关系,因为您没有设置名称以外的任何名称,但是如果设置了非默认值,则可以想告诉客户要使用哪种绑定配置)。

In reality the simplest way (to get started) would be to build the Windows Service, install it and start it, and then add a service reference to it in your client (WinForm) application. 实际上,最简单的方法(开始使用)是构建Windows Service,安装并启动它,然后在客户端(WinForm)应用程序中为其添加服务引用。 That will generate everything you need and you can take a look at the config file to see the settings you need. 这将生成您所需的所有内容,您可以查看配置文件以查看所需的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM