Quantcast
Channel: Windows – delog
Viewing all articles
Browse latest Browse all 44

Serving static web content from a WCF service

$
0
0

This post builds upon an earlier post, Customize how objects are serialized to JSON in a WCF service. It demonstrates how static web content can be served from that service. We’ll add a new service method that will handle requests for static content.

Additional assembly references

You’ll need to use System.IO. The service method that handles static content returns a generic System.IO.Stream type to return content to the browser.

using System.IO;

Add method to service interface

Add the following method signature to the IMyService interface.

        [OperationContract]
        Stream StaticContent(string content);

Implement the service method

The implementation of the service method in the service class MyService is shown below. The service method returns a FileStream with the content of the resource requested, and sets the content type and HTTP status code.

        [WebInvoke(Method = "GET",
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "/www/{*content}")]
        public Stream StaticContent(string content)
        {
            OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
            string path = "www/" + (string.IsNullOrEmpty(content)? "index.html" : content);
            string extension = Path.GetExtension(path);
            string contentType = string.Empty;

            switch (extension)
            {
                case ".htm":
                case ".html":
                    contentType = "text/html";
                    break;
                case ".jpg":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".js":
                    contentType = "application/javascript";
                    break;
            }

            if (File.Exists(path) && !string.IsNullOrEmpty(contentType))
            {
                response.ContentType = contentType;
                response.StatusCode = System.Net.HttpStatusCode.OK;
                return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            }
            else
            {
                response.StatusCode = System.Net.HttpStatusCode.NotFound;
                return null;
            }
        }

You may want to add additional content types to the switch statement above, or implement an externally configurable mapping scheme.

Testing

Create a folder called www inside the folder where the service is started, and store your static content there. Now, when the browser requests content starting with the URL http://localhost:8003/myservice/www/, it will be served static content by the new service method, or returned a Not Found status code.

Thoughts?


Filed under: .NET, HTML, Windows

Viewing all articles
Browse latest Browse all 44

Trending Articles