托管WCF服务的Windows服务立即关闭

时间:2011-06-29 12:16:40

标签: wcf windows-services

我尝试使用Windows服务项目托管WCF库服务,我安装了该服务,但是,当我在services.msc中启动服务时,服务立即启动和关闭。在显示的消息之后:

  

本地的Servicel服务   电脑启动然后停止。   如果有些服务会自动停止   他们没有被其他服务使用   或程序。

wcf和windows服务项目的App.config文件是相同的,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WorkMateWCF.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/WorkMate1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

整个项目/解决方案可在此处下载:https://skydrive.live.com/?cid=d358d316fa2c3a37&sc=documents&uc=1&id=D358D316FA2C3A37%21135#

请您指导一下如何继续前进。谢谢。

其他信息: 以下是Windows服务项目中service1.cs文件的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using WorkMateWCF;

namespace WorkMateWinService
{
    public partial class Service1 : ServiceBase
    {
        internal static ServiceHost MyServiceHost = null;

        public Service1()
        {
            InitializeComponent();
        }

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

        protected override void OnStop()
        {
            if (MyServiceHost != null)
            {
                MyServiceHost.Close();
                MyServiceHost = null;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我发现非常令人困惑(也可能是.NET运行时)是您的Windows服务被称为Service1,而您的WCF服务被称为{{1 (没有命名空间或任何东西)。

那么这里将使用两个Service1类类型中的哪一个???

Service1

我不确定 - 我担心它会是错误的类(Windows NT服务类)。

你应该给你的东西更有意义的名字,并保持这些东西分开(通过名字)!

答案 1 :(得分:0)

遇到问题,当我查看我的事件日志时,我发现了这个:

"Service cannot be started. System.InvalidOperationException: The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address.  Either supply an http base address or set HttpGetUrl to an absolute address.
   at System.ServiceModel.Description.ServiceMetadataBehavior.EnsureGetDispatcher(ServiceHostBase host, ServiceMetadataExtension mex, Uri url, String scheme)
   at System.ServiceModel.Description.ServiceMetadataBehavior.CreateHttpGetEndpoints(ServiceDescription description, ServiceHostBase host, ServiceMetadataExtension mex)
   at System.ServiceModel.Description.ServiceMetadataBehavior.ApplyBehavior(ServiceDescription description, ServiceHostBase host)
   at System.ServiceModel.Description.ServiceMetadataBehavior.System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescript..."

然后经过彻底审查后,问题是我将HTTPSGETENABLED仅用于假,而实际上有两个,在对另一个进行更改后,应用程序开始像魅力一样工作。

我特别