10

Visual Studio 2012 (.NET Framework 4.5) で SelfHosted AspNet WebAPI を作成しました。WebAPI の SSL を有効にしました。コントローラーが同じプロジェクトで定義されている場合、正常に機能します。

しかし、コントローラーを含む別のプロジェクトの参照を追加すると、次のエラーが表示されます。

リクエスト URI 'https://xxx.xxx.xxx.xxx:xxxx/hellowebapi/tests/' に一致する HTTP リソースが見つかりませんでした。

HttpSelfHostConfiguration と MessageHandler のカスタム クラスを作成しました。

この問題を解決するための助けは、私にとって非常に時間の節約になるでしょう.

事前に感謝します。

4

1 に答える 1

8

簡単なカスタム アセンブリ リゾルバーを記述して、コントローラー プローブが機能するように参照アセンブリが読み込まれるようにすることができます。

以下は、これに関する Filip の素敵な投稿です:
http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

サンプル:

class Program
{
    static HttpSelfHostServer CreateHost(string address)
    {
        // Create normal config
        HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(address);

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

        // Add a route
        config.Routes.MapHttpRoute(
          name: "default",
          routeTemplate: "api/{controller}/{id}",
          defaults: new { controller = "Home", id = RouteParameter.Optional });

        HttpSelfHostServer server = new HttpSelfHostServer(config);
        server.OpenAsync().Wait();

        Console.WriteLine("Listening on " + address);
        return server;
    }

    static void Main(string[] args)
    {
        // Create and open our host
        HttpSelfHostServer server = CreateHost("http://localhost:8080");

        Console.WriteLine("Hit ENTER to exit...");
        Console.ReadLine();
    }
}

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(@"C:\libs\controllers\ControllersLibrary.dll");

        baseAssemblies.Add(controllersAssembly);

        return assemblies;
    }
}
于 2013-06-21T04:37:50.607 に答える