1

我有一个 Web-Api 项目,其中包含控制器和所有必要的东西,我想将其作为 Windows 服务托管。我创建了新项目,并在那里添加了一个WindowsServiceServiceInstaller项目,所以我的解决方案如下所示:

解决方案结构

我的配置是:

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”的控制器匹配的类型。

基本上,我试图从项目中调用HomeController 位于WebApiService项目中的WebApiHost项目。

提前致谢!

4

1 回答 1

0

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

于 2013-08-19T11:11:03.907 回答