简体   繁体   English

调用以 HttpRequestMessage 作为参数的 Restful Web 服务?

[英]Calling a Restful Web Service that has HttpRequestMessage as a parameter?

I am in the process of migrating one of my company's web services to a new server, and unfortunately the previous developers left us no way to test the migration of this service prior to migrating the production version.我正在将我公司的一个 Web 服务迁移到新服务器,不幸的是,以前的开发人员让我们无法在迁移生产版本之前测试此服务的迁移。 This leaves us in a harsh situation where I have to formulate a backup plan in case things go wrong when we migrate to the new server.这让我们陷入了困境,我必须制定备份计划,以防我们迁移到新服务器时出现问题。

To understand my plan, you must first understand that the flow of execution for this web service is currently:要了解我的计划,您必须首先了解当前此 Web 服务的执行流程是:

  1. Customer calls platform.客户来电平台。
  2. Platform calls web service.平台调用网络服务。
  3. Web service responds to platform. Web 服务响应平台。
  4. Platform responds to customer.平台响应客户。

Simple enough, but the platform's changes are already in place for deployment at the flip of a switch and the developer will not be in house for the migration.很简单,但平台的更改已经就位,只需轻轻一按即可部署,并且开发人员不会在内部进行迁移。 Thus, they will flip the switch and leave me hoping the migration works.因此,他们会拨动开关,让我希望迁移成功。

I have a simple rollback plan in which the platform's developer won't be required for me to rollback.我有一个简单的回滚计划,其中不需要平台的开发人员回滚。 I simply inject a middle-man to the chain above which acts as a conduit to the web service for the platform:我只是在上面的链中注入一个中间人,作为平台 Web 服务的管道:

  1. Customer calls platform.客户来电平台。
  2. Platform calls conduit service.平台调用管道服务。
  3. Conduit service calls web service.管道服务调用 Web 服务。
  4. Web service responds to conduit. Web 服务响应管道。
  5. Conduit responds to platform.导管响应平台。
  6. Platform responds to customer.平台响应客户。

This way, if for some reason, the migrated version of the web service fails, I can fallback to the original version hosted on the old server until we can investigate what's missing and why it all went wrong (currently we have no way to do this).这样,如果由于某种原因,Web 服务的迁移版本失败,我可以回退到旧服务器上托管的原始版本,直到我们可以调查丢失的内容以及为什么会出错(目前我们无法做到这一点) )。


Now that you have an understanding of the issue, I have a simple issue with writing the conduit to the underlying web service.现在您已经了解了这个问题,我有一个简单的问题,就是将管道写入底层 Web 服务。 I encountered a method in the web service that returns HttpResponseMessage and expects HttpRequestMessage as a request.我在 web 服务中遇到了一个方法,它返回HttpResponseMessage并期望HttpRequestMessage作为请求。 This is rather confusing since the platform calls this method via the following URI:这相当令人困惑,因为平台通过以下 URI 调用此方法:

test.domain.com:port/api/route/methodname test.domain.com:port/api/route/methodname

I have no access to the code under this URI assignment (which is in RPG code), so I have no idea how they are passing the data over.我无法访问此 URI 分配下的代码(在 RPG 代码中),因此我不知道它们是如何传递数据的。 Currently my code is simple:目前我的代码很简单:

[Route("MethodName")]
[HttpPost]
public HttpResponseMessage MethodName(HttpRequestMessage request) {
    try {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{ServiceRoute}/api/route/methodname");
        request.Method = "GET";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        return response; // There is a type mismatch, I know.
    } catch (Exception e) {
        // Log exception.
        return null;
    }
}

How can I call a restful web service and pass on the request message to the service?如何调用 Restful Web 服务并将请求消息传递给该服务?

NOTE : I understand, the snippet I've supplied will not work and has an error.注意:我知道,我提供的代码段将不起作用并且有错误。 I DO NOT expect anyone to just hand out code.期望任何人只是分发代码。 References and explanations as to what needs to be done and why are what I'm looking for.关于需要做什么以及为什么是我正在寻找的参考和解释。

I'm not sure I totally understand the question, so apologies if this isn't helpful, but if your conduit truly just forwards each request as-is, you should be able to reuse the incoming HttpRequestMessage by changing the RequestUri property to the web service URI and forwarding it to the web service with an instance of HttpClient.我不确定我是否完全理解这个问题,所以如果这没有帮助,我很抱歉,但如果你的管道真的只是按原样转发每个请求,你应该能够通过将 RequestUri 属性更改为 Web 来重用传入的 HttpRequestMessage服务 URI 并将其转发到带有 HttpClient 实例的 Web 服务。 Something like this:像这样的东西:

[Route("MethodName")]
[HttpPost]
public async HttpResponseMessage MethodName(HttpRequestMessage request) {
        request.RequestUri = $"{ServiceRoute}/api/route/methodname";
        request.Method = HttpMethod.Get;
        request.Headers.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
        //add any additional headers, etc...
    try 
    {
        //best practice is to reuse the same HttpClient instance instead of reinstantiating per request, but this will do for an example
        var httpClient = new HttpClient();
        var response = await httpClient.SendAsync(request);
        //perform any validation or modification of the response here, or fall back to the old web service on a failure
        return response;
    } 
    catch (Exception e) 
    {
        // Log exception.
        return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM