我正在将公司的一个web服务迁移到一个新服务器上,不幸的是,以前的开发人员在迁移生产版本之前没有办法测试这个服务的迁移。这使我们处于一种严峻的情况下,我必须制定一个备份计划,以防在我们迁移到新服务器时出错。
要理解我的计划,您必须首先了解此web服务的执行流程当前如下:
客户调用platform.
很简单,但是平台的更改已经到位,可以在交换机上进行部署,而且开发人员也不会进行迁移。因此,他们将翻转开关,让我希望迁移工作。
我有一个简单的回滚计划,在这个计划中,平台的开发人员不需要我回滚。我只是在上面的链中注入一个中间人,作为平台的web服务的管道:
客户调用platform.
这样,如果由于某种原因,迁移的web服务版本失败了,我可以回到原来的服务器上托管的版本,直到我们能够调查丢失了什么以及为什么会出错(目前我们没有办法这样做)。
现在您已经了解了这个问题,我对于编写到底层web服务的管道有一个简单的问题。我在web服务中遇到了一个返回HttpResponseMessage并期望HttpRequestMessage作为请求的方法。这是相当混乱的,因为平台通过以下URI调用此方法:
test.domain.com:端口/api/路由/方法名
我无法访问这个URI赋值下的代码(在RPG代码中),所以我不知道它们是如何传递数据的。目前,我的代码很简单:
[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;
}
}如何调用restful web服务并将请求消息传递给服务?
注意到:我理解,我提供的片段将不能工作,并且有一个错误。我不希望任何人只发送代码。参考和解释什么是需要做的,为什么是我要寻找的。
发布于 2020-01-07 16:14:47
我不确定我是否完全理解这个问题,所以很抱歉,如果这没有帮助,但是如果您的管道确实按原样转发每个请求,那么您应该能够重用传入的HttpRequestMessage,方法是将RequestUri属性更改为web服务URI,并将其与HttpClient的实例一起转发到web服务。就像这样:
[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;
}
}https://stackoverflow.com/questions/59619740
复制相似问题