我是.net和WCF的新手。因此,如果此代码或问题看起来很愚蠢,请与我裸露。 能否请一提您的原因我无法启动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 App Client对其进行了测试。因为我需要自己不断地运行这个服务(从db发送一堆行的电子邮件)所以我试图把它放在一个带有Start和stop的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将来到哪里。
答案 0 :(得分:0)
在你的主要方法中试试这个。
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;
}
}
}
祝你好运!