代码之家  ›  专栏  ›  技术社区  ›  Douglas Anderson

安装Polly进行重试后,HttpResponseMessage似乎被重写

  •  0
  • Douglas Anderson  · 技术社区  · 7 年前

    public HttpResponseMessage Submit(string data)
    {
        ...do some sutff...
        return new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent("Success", System.Text.Encoding.UTF8, "text/plain")
        };
    }
    

    一切顺利。在代码库的另一个区域中,有一组方法需要重试,我们安装了Polly nuget包,并在重试中添加,在Visual Studio中本地运行时,一切看起来都很好。一旦我们部署了Azure,事情就会变糟。

    {
    "Version": {
        "_Major": 1,
        "_Minor": 1,
        "_Build": -1,
        "_Revision": -1
    },
    "Content": {
        "Headers": [
            {
                "Key": "Content-Type",
                "Value": [
                    "text/plain; charset=utf-8"
                ]
            }
        ]
    },
    "StatusCode": 200,
    "ReasonPhrase": "OK",
    "Headers": [],
    "RequestMessage": null,
        "IsSuccessStatusCode": true
    }
    

    如果我们卸载波利,一切都会恢复正常。

    我们唯一能找到的其他评论/问题是: Web API returning HttpResponseMessage object after nuget updates in Azure Web App

    2个问题:

    1. 波利身上有没有什么我在配置等方面漏掉的东西可能会导致这种情况?
    1 回复  |  直到 7 年前
        1
  •  2
  •   Joey Cai    7 年前

    正如Nkosi所说,这是ASP.NET-Cype,然后你正在混合Web API版本,并在服务器端ASP.NET核心上进行混合。 不再使用 HttpResponseMessage

    您需要使用适当的操作结果来返回所需的数据,如 .

    [HttpGet("{id}"]
    public ActionResult Submit(string data) {
    
        //...do some stuff...
    
        //returns 200 with the content and specified media type for the content
        return Content("Success", new MediaTypeHeaderValue("text/plain"));
    }    
    

    有关更多详细信息,请参阅 issue .