将 WCF 作为 Windows 服务" Service was started and Stopped"承载



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

为了将端点添加到服务"MYService",必须指定一个非空的协定名称。 at System.ServiceModel.Description.ConfigLoader.LookupContract(String contractName, String serviceName)

我的启动功能如下

 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>

在你的配置文件中,有:

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

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

编辑

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

再次编辑

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

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

请尝试以下操作:

        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();
            }
        }

最新更新