托管WCF作为Windows服务“服务已启动并已停止”

时间:2012-04-25 09:28:32

标签: wcf self-hosting

我使用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>

2 个答案:

答案 0 :(得分:4)

在您的配置文件中,有:

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

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

编辑

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

再次编辑

为方便起见,这就是你的mex绑定的样子:

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

答案 1 :(得分:-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();

            }
        }