skip to Main Content

I have a web application (ASP.NET 4.7.2, C#). I am using HttpClient to do a POST request to webservice.
When it comes to the point to execute this response = await client.SendAsync(request);

it throws
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

I wrapped it around a try/catch but the exception doesn’t get cought.

The exact same code works perfectly on WinForms App and on a Console project (!!).

Here is the exception that i get

   at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
   at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
   at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(Object state)
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.AwaitTaskContinuation.<>c.<ThrowAsyncIfNecessary>b__18_0(Object s)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</StackTrace><ExceptionString>System.NullReferenceException: Δεν έχει οριστεί αναφορά αντικειμένου at μια παρουσία αντικειμένου.
   at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
   at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
   at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
   at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
   at System.Web.LegacyAspNetSynchronizationContext.Post(SendOrPostCallback callback, Object state)
   at System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.PostAction(Object state)
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task&amp;amp; currentTask)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.AwaitTaskContinuation.&amp;lt;&amp;gt;c.&amp;lt;ThrowAsyncIfNecessary&amp;gt;b__18_0(Object s)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

And here is the stripped down code

public async Task<bool> SendPostRequestAsync()
{
    string endpoint = "endpointname";
    url = endpointUrl;
    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // adding Request headers for authentication
    client.DefaultRequestHeaders.Add(...);
    client.DefaultRequestHeaders.Add(...);
    var uri = url + endpoint + "?" + queryString;

    HttpResponseMessage response = new HttpResponseMessage();

    byte[] byteData = Encoding.UTF8.GetBytes(bodyxml);
    using (var content = new ByteArrayContent(byteData))
    {
        try
        {
            Uri myUri = new Uri(uri, UriKind.Absolute);
            HttpRequestMessage request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = myUri,
                Content = content
            };

            content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
            
            //this is to check if there nothing is null for the action
            bool iAmOk = ((client != null) && (response != null) && (url != null) && (request != null) && (bodyxml != null));
            if (!iAmOk) { return false; }
            
            try
            {
               response = await client.SendAsync(request);
               //here throws the exception
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                //it never goes here
            }
            var contents = response.Content.ReadAsStringAsync().Result;
            
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // the call is successfull
                return true;
            }
            else
            {
                _log.Error("errordata");
                //the call is not successfull
                return false;
            }
        }
        catch (Exception exx)
        {
            //it doesn't go neither here
            _log.Error("exceptiondata");
            return false;
        }
   }
}

I am using

  • Microsoft Visual Studio Community 2019 Version 16.11.29
  • ASP.NET and Web Tools 2019 16.11.116.46506
  • ASP.NET Web Frameworks and Tools 2019 16.11.116.46506
  • .Net Framework 4.7.2

Is it a problem with the type of the application? Or with some libraries ??
(found this post that looks similar but does not have any answer yet.
Any input would be appreciated!

2

Answers


  1. One potential issue in your code is that you are initializing the HttpResponseMessage variable response before making the asynchronous call, and then later trying to read from it. However, since the exception is thrown during the SendAsync call, it’s possible that the response is still null at that point.

    You can try to initialize response inside the try block, just before making the asynchronous call. This ensures that it’s not null when you try to access it:

    Eg.

    //...
    
    HttpResponseMessage response = null; // Initialize to null here
    
    byte[] byteData = Encoding.UTF8.GetBytes(bodyxml);
    using (var content = new ByteArrayContent(byteData))
    {
         try
         {
            //...
    
            // Initialize response here
            response = await client.SendAsync(request);
    
            //...
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }
        //...
    }
    
    Login or Signup to reply.
  2. Since the exception comes from System.Threading namespace I’d assume there is some issue with async/awaits. Also, each framework has different thread management (synchronization context to be more precise), it’s not strange that code works properly in CLI and WinForms, while fails on ASP.NET

    In your code, there is a code smell, which is very suspicious:

    var contents = response.Content.ReadAsStringAsync().Result;
    

    This is a synchronous wait in asynchronous method – a big no-no. Depending on internal implementation of this method and how Synchronization Context works in ASP.NET it might cause deadlocks or other failures. Please, in async methods use await call:

    var contents = await response.Content.ReadAsStringAsync();
    

    I hope this will fix the issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search