【问题标题】:Hosting WCF as Windows service " Service was started and Stopped"将 WCF 托管为 Windows 服务“服务已启动和停止”
【发布时间】:2012-04-25 09:28:32
【问题描述】:

我使用 net.tcp 将 WCF 作为 Windows 服务托管。在启动服务时安装Windows服务后,我得到该服务已启动和停止。

错误说为了向服务“MYService”添加端点,必须指定一个非空的合同名称。 在 System.ServiceModel.Description.ConfigLoader.LookupContract(String contractName, String serviceName)

我的OnStart函数如下

 protected override void OnStart(string[] args)
        {
            try
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                }
                myServiceHost = new ServiceHost(typeof(MYservice));
                myServiceHost.Open();

            }
            catch (Exception ex)
            {
                log.Error("ONStart", ex);
                throw;
            }

        }

配置文件如下:

<serviceHostingEnvironment minFreeMemoryPercentageToActivateService="10" />
<services>
  <service behaviorConfiguration="myServiceBehavior"
    name="myNamespace.myTestService">
    <endpoint binding="netTcpBinding" bindingConfiguration="netTCPBindingConfig" contract="myNamespace.ServiceInterface.ImyTestService" />
    <endpoint binding="mexTcpBinding" bindingConfiguration="" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://10.1.3.69:8523/TestService" />
      </baseAddresses>
      <timeouts closeTimeout="10:00:10" openTimeout="10:01:00" />
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="myServiceBehavior">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

【问题讨论】:

  • 错误在您的配置文件中。此行为特定于服务启动期间发生某些异常的情况。发布您的配置。

标签: wcf self-hosting


【解决方案1】:

在你的配置文件中,有:

<endpoint binding="netTcpBinding" bindingConfiguration="netTCPBindingConfig" contract="myNamespace.ServiceInterface.ISomeService" /> `

您必须指定MYService 实现的接口,而不是ISomeService

编辑

此外,mex 绑定必须指定一个合同,即contract="IMetadataExchange"

再次编辑

为方便起见,您的 mex 绑定应如下所示:

<endpoint binding="mexTcpBinding" address="mex" bindingConfiguration="" contract="IMetadataExchange" />

【讨论】:

  • 当然我已经指定了服务实现的接口名称
  • 好的,在我将合同添加到 mex 端点后,我收到以下错误 System.InvalidOperationException: A binding instance has been associated to listen URI 'net.tcp://10.1.3.69:8523/TestService '。如果两个端点想要共享同一个 ListenUri,它们也必须共享同一个绑定对象实例。两个冲突的端点要么在 AddServiceEndpoint() 调用中指定,要么在配置文件中指定,要么在 AddServiceEndpoint() 和 config 的组合中指定。
  • @xaria 不要忘记将 address="mex" 添加到 mex 端点
  • 非常感谢......就是这样...... muah
【解决方案2】:

请试试这个:

        protected override void OnStart(string[] args)
        {
            try
            {
                myServiceHost = new ServiceHost(typeof(MYservice));
                myServiceHost.Open();
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                log.Error("ONStart", ex); throw;
            }
            finally
            {
                myServiceHost.Close();

            }
        }

【讨论】:

  • 当然,这只会在服务启动后立即关闭。关闭需要在关机时完成。
猜你喜欢
  • 2011-06-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多