1

コントローラーとすべての必要なものを備えた 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 内にあるものを呼び出そうとしています。WebApiServiceWebApiHost

前もって感謝します!

4

1 に答える 1

0

MapHttpRouteApiControllerコントローラー用で、からのみ派生します。HomeControllerから派生しControllerているため、この方法でマッピングすることはできません。

于 2013-08-19T11:11:03.907 に答える