【问题标题】:Calling method of WCF Service that is hosted in IIS consumed in a Windows Service but the Service not starting在 Windows 服务中使用但服务未启动的 IIS 中托管的 WCF 服务的调用方法
【发布时间】:2013-02-15 10:23:59
【问题描述】:

您能否告知我为什么无法启动 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 应用客户端对其进行了测试。由于我需要不断地自行运行此服务(从 db 中为一堆行发送电子邮件),因此我试图将其放入具有启动和停止功能的 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 会来哪里。

【问题讨论】:

  • 事件查看器中的任何内容,或AppLogger 写入的任何位置?
  • @Internal Server 错误 - 如何检查事件查看器?我尝试查看应用程序日志,但什么也没有。 AppLogger 也没有写任何东西,因为它没有走那么远
  • Main 本身似乎出错了。
  • 哦,抱歉 - 错过了您的评论。在构造函数中尝试 Debugger.Break() 并从那里开始。
  • 当您说构造函数时,您的意思是在 Main() 下吗?我是 .net 和 WCF 的新手,所以仍然不了解首字母缩略词。

标签: asp.net wcf windows-services wcf-binding


【解决方案1】:

在你的 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;
        }
    }
}

祝你好运!

【讨论】:

  • 问题。我仍然应该在 BulkEmailWindowsService 命名空间中的 EmailService 类下拥有 Main。这是正确的还是我错过了什么?
  • @500 - 内部服务器错误 - 我仍然卡住了。它说由于类类型而无法启动。出于某种原因,我在 ServiceHost 的 TestBulkEmailService.BulkEmailService 中没有看到我的 Implementation 类。怎么了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
相关资源
最近更新 更多