Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Problem: ClickOnce Deployment via Internet May Not Always Upgrade an Application

Rate me:
Please Sign up or sign in to vote.
4.78/5 (10 votes)
30 Oct 2007CPOL4 min read 97K   440   36  
An application deployed using ClickOnce may not receive an upgrade if the browser’s proxy server has cached the deployment file. This article explains how to solve this problem using HTTP content expiration.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace CustomHttpHandlers
{
    public class ClickOnceApplicationHandler : IHttpHandler
    {
        public ClickOnceApplicationHandler()
        {
        }

        #region IHttpHandler Members

        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }

        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            Logger.Log("IHttpHandler.ProcessRequest started for " + context.Request.Path);

            context.Response.Cache.SetExpires(DateTime.Now);
            context.Response.Cache.SetValidUntilExpires(true);
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            string path = context.Server.MapPath(context.Request.Path).ToLower();
            if (File.Exists(path))
            {
                //Ensure browser handles application files correctly
                if (path.EndsWith(".application"))
                    context.Response.ContentType = "application/x-ms-application";

                byte[] outputBinaryArray;
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                outputBinaryArray = r.ReadBytes(Convert.ToInt32(fs.Length));
                r.Close();
                fs.Close();
                context.Response.BinaryWrite(outputBinaryArray);
            }
            else
            {
                throw new HttpException(404, "File not found");
            }
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions