HttpRequestMessage.CreateResponse引发StackOverflowException

问题描述

我有一个功能应用程序。我编写了一个单元测试项目(xunit)以测试我的功能应用程序代码。在单元测试方法中,我调用函数应用程序的 Run 方法。当请求为“ 获取”时,我的函数应用返回一个简单的json对象。尽管这在我的机器上正常运行,但在我的所有同事的机器上,下面的行都抛出StackOverFlowException。当我检查异常详细信息时, StackTrace 为空。

request.CreateResponse(HttpStatusCode.OK,jObject)

在调试窗口中,我看到错误为“ System.private.corlib.dll中发生未处理的异常”

功能应用程序:

     public static async Task<HttpResponseMessage> Run(
                      [HttpTrigger(AuthorizationLevel.Function,"get","post",Route = null)]HttpRequestMessage req,ILogger log)
             {
                if (req.Method.Method.Equals(HttpMethod.Get.Method))
                {
                   NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(req.RequestUri.Query);
                   operation = queryString.Get("operation");
                
                    if (operation == "GetVersion")
                    {
                       version = VersionHistory.GetVersion("ABC");// returns 
                                                               // {"ABC" : "2.1"}
                       return req.CreateResponse(HttpStatusCode.OK,version);  
                                  //Above line causes StackOverFlowException
                     }
                     else
                     {
                       return 'Error message'
                      }
                 }
             }
上面代码中的

VersionHistory 只是我正在使用的静态类。它仅返回类似于{{“ ABC”:“ 0.1.2”}}的json对象。它与.net框架版本无关。

public static JObject GetVersion(string key)
       {
            // some logic. This logic is hit only once. I'm sure this is not 
           // causing any exception
            return JObject.Parse("{\"" + key + "\":\"0.1.2\"}");
        }

单元测试:

    public async Task Run_WhenGetRequest_ShouldReturnAppropriateFunctionAppVersion()
            {
                const string QUERYSTRING_KEY = "operation";
                const string QUERYSTRING_VALUE = "GetVersion";
                const string FUNCTION_APP_NAME = "ABC";
                const string FUNCTION_APP_VERSION = "2.1";
    
                _request.Method = HttpMethod.Get;
                NameValueCollection queryList = HttpUtility.ParseQueryString(_request.RequestUri.Query);
                if (queryList.Get(QUERYSTRING_KEY) == null)
                {
                    string queryString = QueryHelpers.AddQueryString(_request.RequestUri.ToString(),QUERYSTRING_KEY,QUERYSTRING_VALUE);
                    _request.RequestUri = new System.Uri(queryString);
                }
    
                HttpResponseMessage response = await FunctionAppClassName.Run(_request,_logger);

                // Assert code
            }

我已经按照以下步骤在Fixture类中构造了请求对象并模拟了Logger实例。

    Request = new HttpRequestMessage
                {
                    Content = new StringContent("",Encoding.UTF8,"application/json"),RequestUri = new Uri("http://localhost/")
                };
    
                var services = new ServiceCollection().AddMvc().AddWebApiConventions().Services.BuildServiceProvider();
    
                Request.Properties.Add(nameof(HttpContext),new DefaultHttpContext { RequestServices = services });

有什么解决方法吗?

解决方法

最后,我们找到了问题。问题是因为我们正在从测试用例中调用Http触发功能应用程序,因此无法找到MediaType。在添加如下所示的响应类型的媒体类型后,它起作用了。

    return req.CreateResponse(HttpStatusCode.OK,jObject,"application/json");

我仍然不确定为什么仅在Visual Studio 2019 16.6.x和16.7.x中发生此问题,而在16.4.x中没有发生。如果有人可以对此发表一些看法,我们将不胜感激

谢谢那些花时间的人。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...