首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >托管WCF服务的Windows服务立即关闭。

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

Stack Overflow用户
提问于 2011-06-29 12:16:39
回答 2查看 3.5K关注 0票数 0

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

本地计算机上的服务启动后停止。一些服务如果没有被其他服务或程序使用,就会自动停止。

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

代码语言:javascript
复制
<?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文件中的代码。

代码语言:javascript
复制
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;
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-29 21:20:02

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

代码语言:javascript
复制
"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,只有一个错误,事实上有两个,在为另一个做了改变后,应用程序开始像魅力一样工作。

我特别

票数 0
EN

Stack Overflow用户

发布于 2011-06-29 12:35:37

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

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

代码语言:javascript
复制
MyServiceHost = new ServiceHost(typeof(Service1));

我不确定--恐怕是错误的类( Windows类)。

你应该给你的东西更有意义的名字,并把这些东西分开(也用名字)!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6520490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档