【发布时间】: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