Search code examples
c#.netsrcprimitive

How to fix: Object must be an array of primitives. Parameter name: src


I'm getting loads of these errors reported when a user tries to download a pdf from my web application:

    System.ArgumentException: Object must be an array of primitives. 
Parameter name: src 
at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count) 
at System.Web.StaticFileHandler.ProcessRangeRequest(HttpContext context, String physicalPath, Int64 fileLength, String rangeHeader, String etag, DateTime lastModified) 
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context) 
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 

I've already had a look at http://support.microsoft.com/kb/2026272 and tried point 3 (the other suggestions are not really feasible solutions in this case) but it doesn't fix the issue.

Another suggestion I saw elsewhere was to add the following to the Application_PostRequestHandlerExecute method in the Global.asax:

HttpApplication app = (HttpApplication)sender;

if(app.Context.Request.RawUrl.Contains(".pdf"))
    app.Response.AppendHeader("Accept-Ranges", "none");

But again this doesn't seem to resolve the issue.

Any thoughts appreciated...


Solution

  • The problem is caused by the .Net framework itself. It appears that there is a common problem where the StaticFileHandler's call to Buffer.BlockCopy doesn't properly handle PDF files. The only solution I have found elsewhere, and which can think of myself, is to write your own file handler which does not use Buffer.BlockCopy.

    Here is a link to an implementation of an http handler to fix this exact situation: http://forums.asp.net/t/1471292.aspx/1

    You can also follow Microsoft's guide to creating an http handler and adjust the sample to be specific to PDF files: http://msdn.microsoft.com/en-us/library/ms972953.aspx