SelfHosted ASPNET的WebAPI用控制器类在不同的项目 [英] SelfHosted AspNet WebAPI With Controller Classes In Different Project

查看:252
本文介绍了SelfHosted ASPNET的WebAPI用控制器类在不同的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个SelfHosted ASPNET的WebAPI与Visual Studio 2012中(.NET框架4.5)。我启用了SSL的的WebAPI。当控制器在同一项目中定义它工作正常。

I have created a SelfHosted AspNet WebAPI with Visual Studio 2012 (.NET Framework 4.5). I enabled SSL for the WebAPI. It works fine when the controller is defined in the same project.

但是,当我添加其他项目的参考,包含控制器,它给了我以下错误:

But when I add a reference of another project, containing controllers, it gives me the following error:

No HTTP resource was found that matches the request URI 'https://xxx.xxx.xxx.xxx:xxxx/hellowebapi/tests/'.

我已经创建自定义类的HttpSelfHostConfiguration和的MessageHandler。

I have created custom classes for HttpSelfHostConfiguration and MessageHandler.

任何帮助解决这一问题将是一个伟大的时间,细细品味我。

Any help to resolve this problem would be a great time-savor for me.

感谢在前进。

推荐答案

您可以编写一个简单的自定义组件解析这可以确保您引用的程序集加载控制器探测工作。

You can write a simple custom assemblies resolver which makes sure that your referenced assembly is loaded for the controller probing to work.

以下是从菲利普好贴对此:结果
<一href=\"http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/\">http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

Following is a nice post from Filip regarding this:
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;
    }
}

这篇关于SelfHosted ASPNET的WebAPI用控制器类在不同的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆