【问题标题】:access Web API from windows web service从 Windows Web 服务访问 Web API
【发布时间】:2013-05-15 21:00:51
【问题描述】:

我正在开发一项 Windows 服务,以在我们客户的服务器上运行维护任务,该服务将结果返回到在我们总部运行的 Web 服务。

我遇到了 Web API 作为通过 HTTP 构建直观 Web 服务的一种新的简单替代方案。 所以问题是是否可以在我们总部的 IIS 7 中托管 Web API Web 服务,从多个远程 Windows 服务访问它以返回结果集?

【问题讨论】:

    标签: windows-services asp.net-web-api


    【解决方案1】:

    是的,有可能,asp.net 提供了specific guidance on how to do this

    原则上,创建您的 web api 项目,然后从客户端执行:

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://mywebapi.mycompany.com/");
    client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
    
    var message = new MaintenanceResult { result = resultstatus, server = "servername" };
    response = client.PostAsJsonAsync("api/maintenanceresults", message).Result;
    // now check for success, and response appropriately
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-14
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多