我是.net和WCF的新手。所以如果這個代碼或者這個問題看起來很傻,請和我一起裸照。 您能否請您指教爲什麼我無法啓動Windows服務。 一旦我開始它立即停止給我的錯誤,沒有工作要做。在Windows服務中使用的IIS中承載的WCF服務的調用方法,但服務未啓動
下面是代碼:
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客戶端。由於我需要不斷自行運行此服務(從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);
}
}
}
因此,這是不正確的?我很困惑主要會在哪裏。
什麼都可以在事件查看器或AppLogger中寫入? – 2013-03-01 01:39:18
@內部服務器錯誤 - 如何檢查事件查看器?我試着看應用程序日誌,但沒有任何東西。 AppLogger也沒有寫任何東西,因爲它不會那麼遠 – Ditty 2013-03-01 01:41:02
它似乎在Main本身出錯。 – Ditty 2013-03-01 01:41:42