簡體   English   中英

無法啟動主機WCF服務

[英]Cannot start host WCF service

我遵循了本指南並構建了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

這是我來自WindowsService project On 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 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)

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="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>

轉到控制面板->程序和功能-> Windows功能
Windows功能內部,檢查Microsoft .NET Framework->(對於Web HTTP)Windows Communication Foundation HTTP激活,對於TCP,MSMQ Windows Communication Foundation NonHTTP激活
我相信應該為自托管WCF啟用它們。

更改此行

myServiceHost = new ServiceHost(typeof(Service1));

至:

myServiceHost = new ServiceHost(typeof(MyWcfService));

要么

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

據我了解, Service1是您的Windows Service class 為了承載WCF服務,您必須輸入WCF服務的類名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM