1

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

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

我的 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>

4

2 回答 2

5

在您的配置文件中,有:

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

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

编辑

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

再次编辑

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

<endpoint binding="mexTcpBinding" address="mex" bindingConfiguration="" contract="IMetadataExchange" />
于 2012-04-25T09:52:34.673 回答
-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();

            }
        }
于 2012-04-25T09:43:00.233 回答