Azure and Response is not available in this context

by ingvar 17. december 2010 10:29

Application_Start and 'Response is not available in this context'

I have experienced the the HttpException 'Response is not available in this context' a lot when working with Azure. It appens if you try to do Azure stuff like accessing the blob in the Application_Start method.

After some time I found out that it was because HttpContext.Current.Response was accessible. So I though, why do Azure needs access to the Response object? I knew that it was possible to access the blob storage from a console app. Though I'm not sure about this, but I think that Azure uses the Response object for logging. In any case, if you need to access the blob in your web app or do any other Azuer stuff AND want to do this even though there is not a Response object, like in the Application_Start method. You simply set HttpContext.Current to null, but in a smart way!

Here is my code for Application_Start and below is the code for the smart way of setting HttpContext.Current to null:

protected void Application_Start(object sender, EventArgs e)
{
    CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
    {
        configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
    });

    using (new AzureContext())
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("BlobConnectionString");

        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
        container.CreateIfNotExist();
        container.FetchAttributes();
    }
}

 

And the code for AzureContext:

public class AzureContext : IDisposable
{
    HttpContext _oldHttpContext;
    bool _restoreOldHttpContext = false;


    public AzureContext(bool forceSettingContextToNull = false)
    {
        if (forceSettingContextToNull)
        {
            _oldHttpContext = HttpContext.Current;
            HttpContext.Current = null;
            _restoreOldHttpContext = true;
        }
        else
        {
            try
            {
                HttpResponse response = HttpContext.Current.Response;
            }
            catch (HttpException)
            {
                _oldHttpContext = HttpContext.Current;
                HttpContext.Current = null;
                _restoreOldHttpContext = true;
            }
        }
    }


    public void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_restoreOldHttpContext)
            {
                HttpContext.Current = _oldHttpContext;
            }
        }
    }


    public void Dispose()
    {
        Dispose(true);
    }


    ~AzureContext()
    {
        Dispose(false);
    }
}

Tags:

.NET | Azure | C#

Comments (5) -

Marcus Jansson
Marcus Jansson Sweden
10-03-2011 16:40:44 #

Thanks for the tips.
Could not understand why I got this strange exception in Application_Start

duy
duy United States
08-07-2011 11:05:17 #

good post!
can we still do that and not create the AzureContext class ???

ingvar
ingvar Denmark
04-10-2011 14:25:27 #

What do you mean?

Pauli Østerø
Pauli Østerø Denmark
26-12-2011 18:19:37 #

Looks like there is a more "official" way of executing code in a Null Context now, supported by Microsoft

msdn.microsoft.com/.../...ullcontext(v=vs.99).aspx

Ben Foster
Ben Foster United Kingdom
18-07-2012 23:58:08 #

Ingvar, I came across the same problem and *eventually* found the cause. I blogged about it at ben.onfabrik.com/.../azure-virtual-machines-do-not-come-with-a-sysadmin.

About the author

Martin Ingvar Kofoed Jensen

Architect and Senior Developer at Composite on the open source project Composite C1 - C#/4.0, LINQ, Azure, Parallel and much more!

Follow me on Twitter

Read more about me here.

Read press and buzz about my work and me here.

Stack Overflow

Month List