我有一个 Web-Api 项目,其中包含控制器和所有必要的东西,我想将其作为 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”的控制器匹配的类型。
基本上,我试图从项目中调用HomeController
位于WebApiService
项目中的WebApiHost
项目。
提前致谢!