Windows服务启动然后立即停止(代码问题?)

时间:2013-08-09 15:42:41

标签: c# visual-studio-2010 wcf windows-services

我使用WCF制作了以下服务并按照MSDN“making a windows service”教程中的指定方式安装,但每次我开始服务时,我会弹出一个声明该服务已启动的弹出窗口停止。我想知道为什么会这样,以及如何纠正它。

服务代码:

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 System.ServiceModel.Description;
using RemoteArchiverService;
namespace ORService
{
    public partial class ORservice : ServiceBase
    {
        private ServiceHost ORAHost;
        public ORservice()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            if (ORAHost != null)
            {
                ORAHost.Close();
            }
            // Open the ServiceHostBase to start listening for commands
            ORAHost = new ServiceHost(typeof(OrionWCF));
            ORAHost.Open();
        }
        protected override void OnStop()
        {
            ORAHost.Close();//stop listening
        }
    }
    [ServiceContract(Namespace = "http://Blah.Blargh.ServiceBase")]
    public interface IRemoteArchive
    {
        //functions
        [OperationContract]
        void CollectFilesAsync(DateTime start, DateTime end);
        [OperationContract]
        void ChangeExpireCheck(int daysToKeep);
        [OperationContract]
        void UpdateActiveProfiles(Profile P, bool AddRemove);
        [OperationContract]
        void UpdateProfileList(Profile P, bool AddRemove);
    }
    partial class OrWCF : IRemoteArchive
    {
        private List<Profile> ProfileList = new List<Profile>();
        private List<Profile> ActiveProfiles = new List<Profile>();
        private int DaysToKeepData = 30;

        public void UpdateProfileList(Profile P, bool AddRemove)
        {
            ...
        }
        public void UpdateActiveProfiles(Profile P, bool AddRemove)
        {
            ...
        }
        public void ChangeExpireCheck(int daysToKeep)
        {
            ...
        }
        public void CollectFilesAsync(DateTime start, DateTime end)
        {
            ...
        }
    }
}

日志读数:

  

无法启动服务。 System.InvalidOperationException:服务   'RemoteService.OrWCF'没有应用程序   (非基础设施)端点。这可能是因为没有配置   找到了您的应用程序的文件,或者因为没有服务元素   匹配服务名称可以在配置文件中找到,或者   因为在service元素中没有定义端点。在   System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreApplicationEndpoints(ServiceDescription   描述)at   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription   description,ServiceHostBase serviceHost)at   System.ServiceModel.ServiceHostBase.InitializeRuntime()at   System.ServiceModel.ServiceHostBase.OnBeginOpen()at   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)at   System.ServiceModel.Channels.CommunicationObject.Open(时间跨度   System.ServiceModel.Channels.CommunicationObject.Open()的超时)   在C:\ Us ...中的RemoteService.ORservice.OnStart(String [] args)

1 个答案:

答案 0 :(得分:1)

启用WCF跟踪以查看有关问题的详细信息。

<configuration>
   <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="traceListener" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:\log\Traces.svclog" />
            </listeners>
         </source>
      </sources>
   </system.diagnostics>
</configuration>

有关WCF配置的详细信息,请参阅MSDN

根据新信息编辑: 它无法在App.Config文件中找到WCF服务端点配置,请参阅http://msdn.microsoft.com/en-us/library/ms733932.aspx