Single file deployment concept in ASP.NET with WebAPI

Deployment contained in single file using WebAPI

  • Share

As much as this sound new to you, it actually isn't.

If you worked on any Windows From application in .NET you had an option to embedded files in your form like images, icons and even sounds and videos. This made deployment a lot easier because you only have to deploy one .exe file.

In ASP.NET this functionality was provided with WebResource.axd.

  1. Add the image to your project.
  2. In the solution explorer, click the file, and in the property window, change build action to "embedded resource."
  3. Add the following to your AssemblyInfo.cs file:
     "image/gif")]
        
    Important note here... the "MyNameSpaces" comes from the default namespace indicated in the project properties. The "Resources" comes from the fact that I happened to put the image file in the "Resources" folder I made.
  4. In your control rendering code, you'll need the following to produce a usable URL for the image:
    Page.ClientScript.GetWebResourceUrl(typeof(MyNameSpaces.MyControl), "MyNameSpaces.Resources.MyImage.gif")
        
  5. Notice I used the same naming convention here.
  6. Compile away.

This should produce a URL in your rendered control that looks something like:
/WebResource.axd?d=PhPk80h_UWEcbheb-NHNP5WshV_47UOpWqAOl1_liUFfN4cNofL74cFlQ1fvpFSf0&t=632573240669964903

With new version of .NET framework WebAPI is introduced and one of it's roles is to provide different content for actions implemented in them using HTTP not only for transport as SOAP does, but for data storing as well. Providing embedded resource to a web page from WebAPI is pretty similar to WebResource.axd approach but it is a bit easier and URL of resources are a lot more SEO friendly then form mentioned above.

Single fiel deployment using WebAPI I implemented in Umbraco plugin I worked on (https://github.com/dejanstojanovic/Umbraco-GoogleMap-Editor). The following sample code is derived from this project.

To embed file inside DLL is the same for any approach you go with

  1. Add the image to your project.
  2. In the solution explorer, click the file, and in the property window, change build action to "embedded resource."
  3. Compile

After compilation file is embedded inside DLL. Now, all we have to do is to pull it out.

Code for pulling out the resources for project mentioned above can be found at https://github.com/dejanstojanovic/Umbraco-GoogleMap-Editor/blob/master/src/Controllers/ResourceController.cs

For example purposes I will use the following method

 public HttpResponseMessage GetEmbededGif(string id)
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            MemoryStream ms = new MemoryStream();
            Common.GetResourceBitmap(id).Save(ms, ImageFormat.Gif);
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(ms.ToArray());
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/gif");
            return result;
        }
    

Now to load image on the page you will have to set src attribute to something like this /api/resources/GetEmbededGif/image.gif

I used this approach for Umbraco package, because for every new update I do, users can just download compiled DLL and replace the old one with the new one. In some of the releases I added some files, so it would be more or less a nightmare for some who uses it to read manual and copy all the files at the right location, or even have to set read permissions for it.

People like it simple, so copy/paste approach is the best solution for deployment of things like plugins and extensions.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus