1

I am getting an issue:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. System.NullReferenceException: Object reference not set to an instance of an object.

I am getting this issue in some Windows 10 environments. Same code is working fine for other environments.

I am getting this error from below line response = (HttpWebResponse)request.GetResponse();

Below is detailed stack-trace:

{System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at Microsoft.SharePoint.Administration.SPServer.get_RegistryEncodedServerId()
       at Microsoft.SharePoint.Utilities.SPMonitoredScope.PushParent()
       at Microsoft.SharePoint.Utilities.SPMonitoredScope..ctor(Boolean clearCorrelationId, String name, TraceSeverity traceLevel, ISPScopedPerformanceMonitor[] monitors)
       at Microsoft.SharePoint.SPCertificateValidator.SPImmutableCertificateValidator.Validate(X509Certificate2 certificate)
       at Microsoft.SharePoint.SPCertificateValidator.Validate(X509Certificate2 certificate)
       at Microsoft.SharePoint.SPCertificateValidator.IsValid(X509Certificate2 certificate)
       at System.Net.Security.RemoteCertificateValidationCallback.Invoke(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
       at System.Net.ServerCertValidationCallback.Callback(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.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.ServerCertValidationCallback.Invoke(Object request, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
       at System.Net.Security.SecureChannel.VerifyRemoteCertificate(RemoteCertValidationCallback remoteCertValidationCallback)
       at System.Net.Security.SslState.CompleteHandshake()
       at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
       at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
       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.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
       at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.ConnectStream.WriteHeaders(Boolean async)
       --- End of inner exception stack trace ---

And code is:

     public static string SendMessage(SharePointAdapter adr, string uri, Stream postStream, string contentType)
        {
        string responseText = null;
        Stream requestStream = null;
        System.Net.WebRequest request = null;
        byte[] buffer = null;
        HttpWebResponse response = null;
        Stream receiveStream = null;
        StreamReader readStream = null;

        try
        {      
            //Create a request
            request = WebRequest.Create(uri);
            request.Timeout = 180000;
            request.Method = "POST";
            request.ContentType = contentType;
            request.Headers.Add("X-Vermeer-Content-Type", "application/x-www-form-urlencoded");
            request.Credentials = adr.Credentials.NetworkCredentials;
            HttpWebRequest httprequest = request as HttpWebRequest;
            httprequest.KeepAlive = false;
            adr.certificates.CopyCertificatesToCollection(httprequest.ClientCertificates);

            if (adr.AdapterProxy != null)
            {
                request.Proxy = adr.AdapterProxy;
            }       
            httprequest.CookieContainer = new CookieContainer();
            adr.CookieManager.AddCookiesTo(httprequest.CookieContainer);

            ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072 | (SecurityProtocolType)48;

            requestStream = request.GetRequestStream();
            int iRead = 1;
            int iBufferSize = 4096;
            buffer = new byte[iBufferSize];

            //Go to the beginning of the stream
            postStream.Seek(0, SeekOrigin.Begin);

            //Read from the post stream to the request stream
            while (iRead > 0)
            {
                iRead = postStream.Read(buffer, 0, iBufferSize);
                requestStream.Write(buffer, 0, iRead);
            }
            requestStream.Close();
            response = (HttpWebResponse)request.GetResponse();
            receiveStream = response.GetResponseStream();
            readStream = new StreamReader(receiveStream, Encoding.UTF8);
            responseText = readStream.ReadToEnd();
            response.Close();
            readStream.Close();

        }           
        finally
        {
           requestStream = null;
            request = null;
            buffer = null;
            response = null;
            receiveStream = null;
            readStream = null;
            System.GC.Collect();
        }
        return responseText;
        }

1 Answers1

0

System.NullReferenceException is not very specific. It's hard to find the problem, specially because everything on your method is inside a big try/catch block.

Use try/catch blocks just on needed blocks, avoiding big methods with only one generic try/catch. For more details, refer to https://learn.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions.

I strongly recommend usage of HttpClient instead of WebRequest. HttpClient class can be used on .Net Framework 4.5.2+ and here are some detailed explanation and reasons why to use it: Deciding between HttpClient and WebClient.

In addition, instead of manipulating manually the stream, just send it by using StreamContent:

client.PostAsync (url, new StreamContent(stream));

On your code you should get use of the IDisposable capabilities (use using) to ensure closing and disposing streams and other unmanaged resources. For instance:

using (var client = new HttpClient())
{
}

Finally, avoid manipulating GC directly, as it shall cause side effects, specially on performance. GC should only be manipulated on very specific situations. See more on https://msdn.microsoft.com/en-us/library/ms973837.aspx

This hints might help reduce future problems, improve maintainability and readability of the code (and maybe even find the System.NullReferenceException problem).

  • Thanks for your response. I am getting an exception on when we call web request means on below line. response = (HttpWebResponse)request.GetResponse(); And same code is working in other environments. It might be some issue with System environment And We don't want to touch existing functionality. – Rumit Patel May 25 '18 at 05:42