none
w3p crash with .net web api RRS feed

  • Question

  • We've a problem with our .net web API 2 web services hosted on IIS-7 (windows server 2008 R2). Sometimes the w3p process crashes. We can see this from the server event viewer:

    An unhandled exception occurred and the process was terminated.
    
    Application ID: /LM/W3SVC/57/ROOT
    
    Process ID: 7340
    
    Exception: System.ArgumentException
    
    Message: Value does not fall within the expected range.
    
    StackTrace:    at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at System.Web.Hosting.IIS7WorkerRequest.GetUserToken()
       at System.Web.HttpContext.get_ClientIdentityToken()
       at System.Web.HttpContext.get_ImpersonationToken()
       at System.Web.ClientImpersonationContext.Start(HttpContext context, Boolean throwOnError)
       at System.Web.ThreadContext.EnterExecutionContext()
       at System.Web.Hosting.AspNetHostExecutionContextManager.SetHostExecutionContext(HostExecutionContext hostExecutionContext)
       at System.Threading.HostExecutionContextManager.SetHostExecutionContextInternal(HostExecutionContext hostContext)
       at System.Threading.ExecutionContext.SetExecutionContext(ExecutionContext executionContext, Boolean preserveSyncCtx)
       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.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
       at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

    The call stack indicates an ASP.NET internal exception (see here) and it is related to a native Win32 API.

    The W3P crashes when we call the remote methods below (not every time). In this method, we read the requested file (image) from a nas hard disk, we scale it (see ScaleImage method) and we return it to the client.

    public async Task<HttpResponseMessage> GetPhoto(string id, ImageResolutionType resolutionType)
    {
       string fileFullPath = this.GetFileFullPath(id);
       string fileFormat = this.GetFileFormat(fileFullPath);
    
       HttpResponseMessage result = this.Request.CreateResponse();
       var allBytes = this.ScaleImage(fileFullPath, (int)resolutionType);
       result.Content = new ByteArrayContent(allBytes);
       result.Content.Headers.ContentType = new MediaTypeHeaderValue(fileFormat);
       result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"){ FileName = Path.GetFileName(fileFullPath)};
    
       return result;
    }
    private byte[] ScaleImage(string path, int maxWidth)
        {
            byte[] data = null;
    
            using (var image = Image.FromFile(path))
            {
                int width = image.Width;
                int height = image.Height;
                int newHeight = height;
                int newWidth = width;
    
                if (width > maxWidth)
                {
                    double ratio = (double)width / (double)height;
                    newWidth = maxWidth;
                    newHeight = (int)(newWidth / ratio);
                }
    
                using (var ms = new MemoryStream())
                {
                    using (var newImage = new Bitmap(newWidth, newHeight))
                    {
                        using (var graphics = Graphics.FromImage(newImage))
                            graphics.DrawImage(image, 0, 0, newWidth, newHeight);
    
                        newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        data = ms.ToArray();
                    }
                }
            }
    
            return data;
        }

    What could be the problem?

    Thanks.

    Monday, November 20, 2017 8:35 AM

All replies

  • https://forums.asp.net/1246.aspx/1?Web+API

    you can also post there too.

    Monday, November 20, 2017 11:24 AM
  • Thank's for your reply.

    I've added the request to the asp.net forum: https://forums.asp.net/p/2132070/6177999.aspx?p=True&t=636467457186890611

    Monday, November 20, 2017 11:45 AM
  • Hi sroby,

    Based on your description, it seems this issue randomly happens on specific method, it did not happen to other method, am I right?

    When the w3p crashed with one specific request for the specific file, will it work if you restart w3p with the same request? I suggest you check it whether it is related with image content.

    In addition, I suggest you refer link below to debug w3p crashes.

    #How to debug why w3wp.exe crashes randomly?

    https://serverfault.com/questions/404945/how-to-debug-why-w3wp-exe-crashes-randomly

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 21, 2017 1:54 AM
  • Thanks for your reply.

    The issue randomly happens and it does not crash with a specific file (when the w3p restart, the same request works), it is not related to the content of a specific file.

    We'll try to use the diagnostic tool in order to debug w3p crashes.

    Tuesday, November 21, 2017 11:27 AM
  • Hi sroby,

    Since your issue is random and it may not happen currently, I would suggest you add log function to GetPhoto method every line to check which line cause this issue.

    Please feel free to let us know if you have any update.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 22, 2017 1:39 AM

  • Hi Tao

    We already have logs for each method that could be called by clients, at the start and at the end of each methods. This logs has been omitted in the posted code in order to avoid redundant code. When the w3p crashes there is no log for the GetPhoto method. It seems that the request fail before to arrive to the GetPhoto method or after to have processed the request.
    • Edited by sroby79 Wednesday, November 22, 2017 8:39 AM
    Wednesday, November 22, 2017 8:38 AM
  • Hi sroby,

    Is there any findings by diagnostic tool to debug w3p crashes?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 23, 2017 1:36 AM
  • Hi Tao.

    I've added here a DebugDiag Analysis Report and the app pool settings here.

    Thursday, November 30, 2017 2:23 PM
  • Hi,

    The description in the Report is general error message. From the error message, I suggest you check below link.

    #IIS 7: value does not fall within the expected range

    https://stackoverflow.com/questions/24237018/iis-7-value-does-not-fall-within-the-expected-range

    For asp.net issue, I would suggest you keep following the thread you post in asp.net forum.

    #https://forums.asp.net/t/2132070.aspx?w3p+crash+with+net+web+api

    Regards,

    Tony


    Help each other

    Friday, December 1, 2017 2:34 AM