自己ホスト型の実行可能ファイルを Windows サービスとして実行しようとしています。MVC 4 ベータ Web API を使用しています。最初に、Derik Whittaker のブログを使用して基本的なコンソール アプリケーションをセットアップし、テストして良好な結果を得ました。
次に、Einar Egilsson のブログを使用して、コンソール アプリケーションと Windows サービスの両方として機能するようにしました。サービスとしてインストールされたアプリケーションは問題ありません。この基本的なテストには、サービス ログオンを独自のものを使用するように設定しました。これがないとソケットにバインドできませんでした。サービスが起動すると、予想どおりすべてのトレース ログが表示され、致命的なエラーはありません。アプリケーションは正常に実行されているようです。コンソール アプリケーションに対して同じ要求を使用してフィドラーを使用してテストすると、「HTTP/1.1 500 内部サーバー エラー」が発生します。
この同じコードを使用してサービスをオフにし、VS で F5 を使用して起動すると、アプリケーションは正常に起動し、同じ要求を処理します!? ログ エントリは、同じ実行パス内では同一です。
public partial class TestService : ServiceBase {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private HttpSelfHostServer _server;
static void Main(string[] args) {
Logger.Debug("Main Called");
var service = new TestService();
if (Environment.UserInteractive) {
Logger.Debug("Environment.UserInteractive == true");
Console.WriteLine("Press any key to stop program");
service.OnStart(args);
service.OnStop();
} else {
Logger.Debug("Environment.UserInteractive == false");
try {
Run(service);
} catch(Exception exception) {
Logger.Fatal(exception.Message, exception);
}
}
}
protected override void OnStart(string[] args) {
Logger.Debug("OnStart called");
var hostUri = string.Format("http://{0}:{1}", Environment.MachineName, ConfigurationManager.AppSettings["Service.Port"]);
Logger.Debug("URL:" + hostUri);
var selfHostConfiguration = new HttpSelfHostConfiguration(hostUri);
selfHostConfiguration.Routes.MapHttpRoute(
name: "DefaultApiRoute",
routeTemplate: "endpoints/{controller}",
defaults: null
);
Logger.Debug("Routes registered");
try {
using (_server = new HttpSelfHostServer(selfHostConfiguration)) {
Logger.Debug("Hosting at " + hostUri + "/endpoints/{controller}");
_server.OpenAsync().Wait();
if (Environment.UserInteractive) { // *** I've tried this commented out as well
Console.ReadLine();
}
Logger.Debug("End of using");
}
} catch(Exception exception) {
Logger.Fatal(exception.Message, exception);
if(exception.InnerException != null) {
Logger.Fatal(exception.InnerException.Message, exception.InnerException);
}
}
}
protected override void OnStop() {
_server.Dispose();
}
}