0

你能告诉我为什么我无法启动我的 Windows 服务吗?一旦我开始它就会立即停止给我没有工作可做的错误。

这是代码:

 namespace BulkEmailWindowsService
 {
     public class EmailService : ServiceBase 
     {     
         public ServiceHost serviceHost = null;

         public EmailService()
         {
           // Name the Windows Service
             ServiceName = "WCFWindowsBulkEmailService";
         }

         public static void Main()
         {
             ServiceBase.Run(new EmailService()); //-------- Stops right here..
         }


         // Start the Windows service.
         protected override void OnStart(string[] args)
         {
             if (serviceHost != null)
             {
                 serviceHost.Close();
             }

             try
             {
                 Console.WriteLine("Testing 1");
                 System.Diagnostics.Debugger.Break();
                 serviceHost = new ServiceHost(typeof(TestBulkEmailService.IBulkEmailService));

                 serviceHost.Open();

                 Console.WriteLine("Testing 1");
                 string logBaseDirectory = "C:\\BulkEmailPrototype\\BulkEmailWindowsService\\BulkEmailWindowsService\\Logs\\BulkEmailWindowsService";
                 int loggingLevel = int.Parse("5");
                 int maximumLogFileSize = int.Parse("2");
                 AppLogger.TraceInfo("Initialization(): Reading configuration settings from config file...");
                 AppLogger.Init(logBaseDirectory, 0, loggingLevel, "WCFBulkEmail.log", maximumLogFileSize);
                 AppLogger.TraceInfo("Bulk Email Processing Service is starting....");

                 using (BulkEmailWindowsService.TestBulkEmailService.BulkEmailServiceClient wfc1 = new BulkEmailWindowsService.TestBulkEmailService.BulkEmailServiceClient())
                 {
                     try
                     {
                         AppLogger.TraceInfo("Database and Email Processing starting....");
                         BulkEmailDTOList result1 = new BulkEmailDTOList();
                         result1 = wfc1.GetBulkEmailInfo(1);
                         AppLogger.TraceInfo("Database and Email Processing done....");
                     }
                     catch
                     {
                         AppLogger.TraceInfo("Error in processing Database and Email....");
                     }

                 }

                 serviceHost.Close();
                 serviceHost = null;
             }
             catch (Exception ex)
             {
                 // Log the exception.
                 Console.WriteLine("Error in ONStart ");
                 AppLogger.TraceInfo("Error in OnStart of Bulk Email Processing Service....");
             }

         }

         protected override void OnStop()
         {
             if (serviceHost != null)
             {
                 serviceHost.Close();
                 serviceHost = null;
             }
         }

     }
 }

这是我的 app.config 文件:

   <system.serviceModel>
     <bindings>
       <basicHttpBinding>
         <binding name="BasicHttpBinding_IBulkEmailService" closeTimeout="00:01:00"
           openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
           allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
           maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
           messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
           useDefaultWebProxy="true">
           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
             maxBytesPerRead="4096" maxNameTableCharCount="16384" />
           <security mode="TransportCredentialOnly">
             <transport clientCredentialType="Windows" proxyCredentialType="None"
               realm="" />
             <message clientCredentialType="UserName" algorithmSuite="Default" />
           </security>
         </binding>
       </basicHttpBinding>
     </bindings>
     <client>
       <endpoint address="http://localhost/TestBulkEmailService/TestBulkEmailService.svc/BulkEmailService"
         binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IBulkEmailService"
         contract="TestBulkEmailService.IBulkEmailService" name="BasicHttpBinding_IBulkEmailService" />
     </client>
   </system.serviceModel>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
   </startup>
 </configuration>

请注意,托管在 IIS 上的 WCF 服务运行良好,我使用 Web 应用客户端对其进行了测试。因为我需要自己不断地运行这个服务(从数据库中为一堆行发送电子邮件),所以我试图把它放在一个带有启动和停止的 Windows 服务中。如果您知道任何其他更简单并且可以做同样的方法,请告诉我。

这就是我的安装程序

namespace BulkEmailWindowsService
{

  // Provide the ProjectInstaller class which allows 
  // the service to be installed by the Installutil.exe tool
  [RunInstaller(true)]
  public class ProjectInstaller : Installer
  {
     private ServiceProcessInstaller process;
     private ServiceInstaller service;

     public ProjectInstaller()
     {
        process = new ServiceProcessInstaller();
        process.Account = ServiceAccount.LocalSystem;
        service = new ServiceInstaller();
        service.ServiceName = "WCFWindowsBulkEmailService";
        Installers.Add(process);
        Installers.Add(service);
     }
  }
 }

所以这是不对的吗?我很困惑 Main 会来哪里。

4

1 回答 1

0

在你的 Main 方法中试试这个。

private static void Main()
{
    try
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new WindowsService()
        };
        ServiceBase.Run(ServicesToRun);
    }
    catch (Exception ex)
    {
        Logger.Error(ex.Message)); // if you have a logger?
    }
}

windows服务的实现应该是这样的:

public partial class WindowsService : ServiceBase
{
    internal static ServiceHost myServiceHost = null;

    public WindowsService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
        }
        myServiceHost = new ServiceHost(typeof (EmailService));
        myServiceHost.Open();
    }

    protected override void OnStop()
    {
        if (myServiceHost != null)
        {
            myServiceHost.Close();
            myServiceHost = null;
        }
    }
}

祝你好运!

于 2013-03-01T07:01:37.290 回答