无法启动主机WCF服务

时间:2013-09-25 12:00:14

标签: c# wcf

我遵循了本指南并构建了WCF项目:

http://msdn.microsoft.com/en-us/library/ff649818.aspx

我的客户端从本地和远程计算机成功连接,所有这些都是从Visual Studio运行wcf服务。 在服务器上安装服务并尝试启动服务(来自services.msc)服务未启动并在尝试启动服务时收到错误:

service on local computer started and stopped. some services stop automatically if they are not used by other services

这是来自Service1.cs的{​​{1}}:

WindowsService project

这是来自Windows事件查看器:

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 WcfServiceLibrary1;

namespace WindowsService1
{
    public partial class Service1: ServiceBase
    {
        internal static ServiceHost myServiceHost = null; 

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

App.config中:

The description for Event ID 0 from source Service1 cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.

If the event originated on another computer, the display information had to be saved with the event.

The following information was included with the event: 

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.CreateHttpGetEndpoints(ServiceDescription description, ServiceHostBase host, ServiceMetadataExtension mex)
   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
   at System.ServiceModel.ServiceHostBase.InitializeRuntime()
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at WindowsService1.Service1.OnStart(String[] args) in d:\WCFService\WindowsService1\Service1.cs:line 30
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

2 个答案:

答案 0 :(得分:1)

转到控制面板 - >程序和功能 - > Windows功能
内部Windows功能检查Microsoft .NET Framework - > (对于Web HTTP)Windows Communication Foundation HTTP激活, 对于TCP,MSMQ Windows Communication Foundation NonHTTP激活
我相信它们应该能够自我托管WCF。

答案 1 :(得分:0)

更改此行

myServiceHost = new ServiceHost(typeof(Service1));

为:

myServiceHost = new ServiceHost(typeof(MyWcfService));

myServiceHost = new ServiceHost(typeof(WCFService.Service1));

根据我的理解,Service1是您的Windows Service class。为了托管您的WCF服务,您必须放置您的WCF服务的类名。