如何在内存中托管web api?

时间:2016-11-16 22:55:47

标签: asp.net-web-api asp.net-web-api2

我有web api控制器,我想进行集成测试。所以我按照文章here来配置内存中的Web API主机。 我的集成测试和web api是同一VS解决方案中的两个不同项目。

以下是代码

Web API控制器

public class DocumentController : ApiController
{

    public DocumentController(IDomainService domainService)
    {
        _domainService = domainService;            
    }        

    [HttpPost]
    public async Task<IEnumerable<string>> Update([FromBody]IEnumerable<Document> request)
    {
        return await _domainService.Update(request).ConfigureAwait(false);
    }
}

整合测试

    [TestClass]
    public class IntegrationTests
    {
        private HttpServer _server;
        private string _url = "http://www.strathweb.com/";

        [TestInitialize]
        public void Init()
        {
            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional });
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            config.MessageHandlers.Add()
            _server = new HttpServer(config);
        }


        [TestMethod]
        public void UpdateTransformedDocuments()
        {
            var doc = new Document()
            {
              // set all properties
            }
            var client = new HttpClient(_server);
            var request = createRequest<Document>("api/document/Update", "application/json", HttpMethod.Post, doc, new JsonMediaTypeFormatter());

            using (var response = client.SendAsync(request).Result)
            {
                 // do something with response here
            }
        }        

        private HttpRequestMessage createRequest<T>(string url, string mthv, HttpMethod method, T content, MediaTypeFormatter formatter) where T : class
        {
            Create HttpRequestMessage here
        }
    }

然而我得到错误

  

StatusCode:404,ReasonPhrase:'Not Found'

&amp;我在哪里告诉HttpServer执行DocumentController?

UPDATE1 所以我通过将[TestIntialize]代码更改为打击

来修复上述错误
    [TestInitialize]
    public void Init()
    {
        var config = new HttpConfiguration();
        UnityWebApiActivator.Start();
        WebApiConfig.Register(config);
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        _server = new HttpServer(config);
    }

我现在没有404错误。但Unity无法解析DocumentController。 HttpResponse包含错误

  

尝试创建类型的控制器时发生错误   'DocumentController'。确保控制器有一个   无参数公共构造函数。

TestInitialize方法中,我调用UnityWebApiActivator.Start(),它使用Unity注册所有需要类型。

1 个答案:

答案 0 :(得分:1)

我通过设置'HttpConfiguration.DependencyResolver'来解决我的第二个问题

    [TestInitialize]
    public void Init()
    {
        var config = new HttpConfiguration();
        //UnityWebApiActivator.Start();
        config.DependencyResolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
        WebApiConfig.Register(config);
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        _server = new HttpServer(config);
    }