File.ReadAllBytes() results in a System.OutOfMemoryException Exception

Sometimes you’ve just got to read that whole file and store it into a Byte[] and sooner or later you’ll run into a file that pushes the limits and you’ll get the wonderful System.OutOfMemoryException. Now you can make sure your Application Pool or Console application is set to run in 64bit mode which will help but what happens if you need to load over 2GB into a Byte[]? Thankfully Microsoft has finally (only took 10 years) introduced very large object support into .NET 4.5+!

(more…)

Read More

Attempt by security transparent method failed

If you are seeing the following:

Attempt by security transparent method ‘Microsoft.Web.Mvc.ScriptExtensions.Script(System.Web.Mvc.HtmlHelper, System.String)’ to access security critical type ‘System.Web.Mvc.MvcHtmlString’ failed.

or something similar chances are you’ve recently updated your project from MVC 4 to 5 and didn’t update all the references in nuget to target the new version. In my case the MVC4Futures nuget package should be removed and the Microsoft.AspNet.Mvc.Futures package should be used instead.

Read More

Setup an Application Pool so that it is always running and preloaded with Application Initialization

This works in IIS version 7/7.5 and is a native install (through features) for IIS 8+. Once setup your Application Pool will automatically restart after recycling and compile your .NET website so the first visitor does not have to sit through compilation.

Configure the IIS Application Initialization module

NOTE: Configuration Editor is found by clicking on the Server node in IIS under the Management in the Features View.

Configuration Editor Location

Read More

Auto redirect to HTTPS and add the Strict-Transport-Security header

A simple piece of code that can be added to your web.config that in IIS 7+ with URL Rewrite installed will redirect any HTTP request to HTTPS and when HTTPS add the Strict-Transport-Security header to keep future requests HTTPS.

<system.webServer>
       <rewrite>
            <rules>
                <clear />
                <rule name="Redirect to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000" />
                </rule>
            </outboundRules>
        </rewrite>
</system.webServer>

Read More

Enable “PUT” and “DELETE” verbs in IIS 7+

Is your newly created RESTful Service returning a 405 on your PUT and DELETE actions when running in IIS 7? If so you may need to disable WebDAV.

A quick way to check is to pop the following lines into a Web.config in the root of your application and try again.

<system.webServer>
 <modules>
  <remove name="WebDAVModule" />
 </modules>
 <handlers>
  <remove name="WebDAV" />
 </handlers>
</system.webServer>

UPDATE 2016-04-22: For those .NET users, if your server is still not responding to DELETE or PUT check to ensure that the ExtensionlessUrl-Integrated-4.0 handler is setup to handle the verbs by dropping these additional settings in your web.config

<system.webServer>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrl-Integrated-4.0" />
      <remove name="WebDAV" />
      <add name="ExtensionlessUrl-Integrated-4.0" path="*." 
      		verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" 
      		type="System.Web.Handlers.TransferRequestHandler" 
      		resourceType="Unspecified" requireAccess="Script" 
      		preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

Read More