I've a function app. I wrote a unit test project(xunit) to test my function app code. In unit test method I'm calling Run method of my function app. When request is 'Get' my function app returns a simple json object. While this is working fine in my machine, in all my colleague's machines following line is throwing StackOverFlowException
. When I checked the exception details, StackTrace is null.
request.CreateResponse(HttpStatusCode.OK, jObject)
In debug window I see the error as "An unhandled exception occurred in System.private.corlib.dll"
Function App:
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 in above code is just a static class I'm using. It simply returns a json object something like {{"ABC": "0.1.2"}}. It has nothing to do with .net framework version.
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\"}");
}
unit test:
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
}
I've constructed request object in a Fixture class as following and mocked Logger instance.
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 });
Any idea how to fix this?
Finally we found the issue. Problem was as we are calling Http triggered function app from test case, it was not able to find the MediaType. After adding media type in response like below it worked.
I'm still not sure why this issue occurred only in Visual Studio 2019 16.6.x and 16.7.x but not in 16.4.x. We would appreciate if someone can throw some light on this
Thank you those who gave your time.