0

我按照本指南构建了 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的来自WindowsService project

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

这是来自 Windows 事件查看器:

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)

应用程序配置:

<?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="WCFService.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" contract="WCFService.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>
4

2 回答 2

1

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

于 2013-09-25T12:15:47.617 回答
0

更改此行

myServiceHost = new ServiceHost(typeof(Service1));

至:

myServiceHost = new ServiceHost(typeof(MyWcfService));

或者

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

据我了解,这Service1是你的。Windows Service class为了托管您的 WCF 服务,您必须输入您的 WCF 服务的类名。

于 2013-09-25T12:19:42.987 回答