C# 作为Windows服务的自托管ASP.NET Web Api,在解决方案中的不同项目中具有控制器

C# 作为Windows服务的自托管ASP.NET Web Api,在解决方案中的不同项目中具有控制器,c#,routing,asp.net-web-api,self-hosting,C#,Routing,Asp.net Web Api,Self Hosting,我有一个带有控制器和所有必要的东西的WebAPI项目,我想作为Windows服务托管它。 我创建了新项目,并在其中添加了WindowsService和ServiceInstaller项,因此我的解决方案如下所示: 我的配置是: private HttpSelfHostServer _server; private readonly HttpSelfHostConfiguration _config; public const string ServiceAddress = "ht

我有一个带有控制器和所有必要的东西的WebAPI项目,我想作为Windows服务托管它。 我创建了新项目,并在其中添加了WindowsService和ServiceInstaller项,因此我的解决方案如下所示:

我的配置是:

private HttpSelfHostServer _server;
    private readonly HttpSelfHostConfiguration _config;
    public const string ServiceAddress = "http://localhost:333";

    public WebApiService()
    {
        InitializeComponent();

        _config = new HttpSelfHostConfiguration(ServiceAddress);

        // Set our own assembly resolver where we add the assemblies we need
        CustomAssembliesResolver assemblyResolver = new CustomAssembliesResolver();
        _config.Services.Replace(typeof(IAssembliesResolver), assemblyResolver);

        _config.Routes.MapHttpRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = RouteParameter.Optional });
    }

public class CustomAssembliesResolver : DefaultAssembliesResolver
    {
        public override ICollection<Assembly> GetAssemblies()
        {
            ICollection<Assembly> baseAssemblies = base.GetAssemblies();

            List<Assembly> assemblies = new List<Assembly>(baseAssemblies);

            var controllersAssembly = Assembly.LoadFrom(@"D:\Regula\WebApiService\WebApiService\bin\WebApiService.dll");

            baseAssemblies.Add(controllersAssembly);

            return assemblies;
        }
    }
我试着跟随,但没有帮助我-我仍然得到:

找不到与名为“Home”的控制器匹配的类型

基本上,我想打电话给家庭控制器 位于WebApiHost项目的WebApiService项目中


提前谢谢

MapHttpRoute用于控制器,仅从ApicController派生。HomeController是从Controller派生的,因此不能以这种方式映射。

CustomAssembliesResolver的代码似乎相关。它是从我在文章中指定的线程复制/粘贴的,只是更改了目标程序集路径。