怎么那么内存的HttpServer知道主办哪些项目的WebAPI? [英] How does the In-Memory HttpServer know which WebAPI project to host?

查看:169
本文介绍了怎么那么内存的HttpServer知道主办哪些项目的WebAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行使用流行的内存托管战略对抗的WebAPI项目的测试。

I want to run tests against WebAPI project using a popular in-memory hosting strategy.

我的测试驻留在一个单独的项目。

My tests reside in a separate project.

下面是我的测试开始

    [TestMethod]
    public void TestMethod1()
    {
        HttpConfiguration config = new HttpConfiguration();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional});

        HttpServer server = new HttpServer(config);
        HttpMessageInvoker client = new HttpMessageInvoker(server)
    }

客户端初始化与的HttpServer,建立直接的客户端 - 服务器连接。

The client is initialized with the HttpServer, establishing the direct client-server connection.

除了提供路径配置信息,如何知道的HttpServer举办哪些项目的WebAPI?

Other than providing route config info, how does HttpServer know which WebAPI project to host?

如何在同一时间举办多的WebAPI项目?

How to host multiple WebAPI projects at the same time?

好像确实的HttpServer一些魔法来定位的WebAPI项目?

Seems HttpServer does some magic to locate WebAPI projects?

感谢

推荐答案

网页API依赖于一个名为 IAssembliesResolver 服务,让所有的组件并扫描他们找到控制器,它贯彻落实 IHttpController 接口。

Web API depends on a service called IAssembliesResolver to get all the assemblies and scans them to find controllers which implement the IHttpController interface.

现在有时网页API可能无法依赖于装配是否已经被加载到当前应用程序域还是没找到您的控制器。在这种情况下,需要确保你的程序集被加载。

Now sometimes Web API might be unable to find your controller depending on whether the assembly has been loaded into the current app domain or not. In that scenario you would need to make sure that your assembly is loaded.

看你的样品测试code,看来你是不是指任何类型的从Web API项目在这种情况下,我认为在Web API项目的组件将不会被加载。

Looking at your sample test code, it appears that you are not referring to any type from your Web API project in which case i assume the Web API project's assembly would not be loaded.

另外你似乎是在测试重新注册的路由。我会建议使用 WebApiConfig.Register(HttpConfiguration)您的Web API项目来完成所有登记的东西。这样,你将与你的Web API项目了。

Also you seem to be registering the routes again in your test. I would suggest to use the WebApiConfig.Register(HttpConfiguration) of your Web API project to do all the registration stuff. This way you would be testing with the same settings that your Web API project has.

注:


  1. 在使用内存的服务器,您的要求运行测试/响应将不会经过格式化序列化/反序列化的过程,是危险的,因为你可以将它们期间有真正的问题。所以,你需要确保采取这个照顾。很长一段时间后,我写了关于这个博客帖子。你可以检查出来的 这里

Fiddler工具是看原料请求/响应诊断任何问题非常有用的。你会失去,如果你正在做的,虽然在内存测试这种能力。

Fiddler tool is very useful in looking at the raw requests/responses to diagnose any issues. You would be loosing this ability if you are doing in-memory testing though.

这篇关于怎么那么内存的HttpServer知道主办哪些项目的WebAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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