コントローラーとすべての必要なものを備えた 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;
}
}
このスレッドの提案に従おうとしましたが、役に立ちませんでした - まだ取得しています:
「ホーム」という名前のコントローラーに一致するタイプが見つかりませんでした。
基本的に、プロジェクトからプロジェクトHomeController
内にあるものを呼び出そうとしています。WebApiService
WebApiHost
前もって感謝します!