c# - 将控制台应用程序转换为 Windows 服务

标签 c# service signalr

我有一个运行 SignalR 的控制台应用程序。我正在尝试将其转换为 Windows 服务应用程序。

我只是用

替换了 Main 方法
static void Main()
{
    var servicesToRun = new ServiceBase[] 
    { 
        new MyService() 
    };
    ServiceBase.Run(servicesToRun);
}

并添加了一个简单的服务类:

namespace Services
{
    partial class MyService : ServiceBase
    {
        IDisposable SignalR { get; set; }

        public MyService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            const string url = "https://localhost:8080";
            using (SignalR = WebApp.Start<Startup>(url))
            {
                          //TODO: Add Logging
            }
        }

        protected override void OnStop()
        {
            SignalR.Dispose();
        }
          }
}

但是现在当我尝试运行安装程序时出现错误:

Exception occurred while initializing the installation: System.BadImageFormatException: Could not load file or assembly 'file:///C:\Code \MyCode\Services\bin\Debug\MyService.exe' or one of its dependencies. A n attempt was made to load a program with an incorrect format..

是否有任何简单的方法来调试此消息?或者有人知道我可能错过了什么吗?

最佳答案

您如何运行安装?我敢打赌,您正在使用 64 位安装程序注册 32 位编译应用程序,反之亦然

换句话说,运行:

%windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe <My x64 DLL/EXE>

或:

%windir%\Microsoft.NET\Framework64\v4.0.30319\installutil.exe <My x86 DLL/EXE>

关于c# - 将控制台应用程序转换为 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24044529/

相关文章:

c# - Linq - 如何按 'let' 值分组并求和另一个 'let' 值

windows - 如何编写看门狗定时器以重新启动 Windows 服务?

.net - SignalR Autofac 注入(inject)授权属性

javascript - Service Worker 不会缓存 golang 服务器在 '/' 上提供的 index.html

ios - 打开 .xcworkspace 时出错

SignalR 连接/断开集线器爆炸

c# - 从 MS Outlook 邮件中读取发件人的电子邮件地址

c# - Orleans 7.0 序列化程序无法与源自 Nuget 包的 POCO 一起使用

c# - DataGridView SelectedRows 返回 null

java - Android在Service类中使用数据库