-1

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?

Kishan
  • 11
  • 2
  • [mcve] please... – mjwills Oct 11 '20 at 12:29
  • Most likely one of two issues 1) There is a credential issue so no data is being returned in the response 2) The HTTP connection is failing due to the default TLS version being used. I would use a sniffer like wireshark or fiddler and check the status in the response. A good response should be 200 OK. A bad response would be a 400/500 errors. Also compare working against non working. Check the TLS version and check headers in first request. Make sure the User Agent Header is the same in working and non working. – jdweng Oct 11 '20 at 13:42
  • @mjwills Added mimimal reproducible example. As I said It's working in my machine I'm not sure if you would get exception. – Kishan Oct 11 '20 at 15:38
  • 1
    One of the primary rules of this site is that a question without a minimal reproducible example will be closed, for the simple reason that it's almost impossible to answer such a question. – Ian Kemp Oct 11 '20 at 16:31
  • Where does version history come from? How did you deploy the code? If the two machine have different versions of Net or the Microprocessors on machine are different errors can occur. It look like version could be null. – jdweng Oct 11 '20 at 22:11
  • @jdweng. I'm sorry Maybe I should have asked even in more detail. VersionHistory is just a static class I've created and as mentioned in the code, it simply returns a json object like {"ABC": "0.1.2"}. Will Update the same in the question – Kishan Oct 12 '20 at 05:47
  • Step through code and see if GetVersion returns a value. – jdweng Oct 12 '20 at 09:24

1 Answers1

0

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.

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

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.

Kishan
  • 11
  • 2
  • I think you've already taken my comment that its a bit "sus". But as the OP and part of the SO Guidelines you should provide an [MCVE], or atleast post the error details when creating this thread. In addition I've come across a similar error in the past, and it's wasn't because I was using version 16.6x, it was because I didn't have all of the appropriate packages installed with Visual studio, when VS went to look up the media/types there stored in a the registry which wasn't available. So chances are you just need to install some IIS Components, or missing web api from VS install – johnny 5 Oct 12 '20 at 18:52
  • 1
    @Kishan we require issues to be reproducible with code posted *here*, not in an external repository, sorry. We are building a repository of questions and answers for future visitors, for the long run, and an external repository is not guaranteed to last the same amount of time. – Martijn Pieters Oct 13 '20 at 07:16