Sharpmap WMS in MVC 4 - Error when ParsingQueryString

Topics: SharpMap Project, SharpMap v2.0, Web Controls
Feb 8, 2014 at 1:54 PM
After hoping I had this issue licked its come back to bite when I run it from run the project on visual studio's web server or even on the local iis in a vitual directory it works fine. However once I deploy it to the IIS which is on the same machine it encounters an error:

System.NullReferenceException: Object reference not set to an instance of an object. at SharpMap.Web.Wms.ServerCapabilities.GenerateBoundingBoxElement(Envelope bbox, Int32 SRID, XmlDocument doc) at SharpMap.Web.Wms.ServerCapabilities.GenerateCapabilityNode(Map map, XmlDocument capabilities, String publicWMSUrl) at SharpMap.Web.Wms.ServerCapabilities.GetCapabilities(Map map, WmsServiceDescription serviceDescription) at SharpMap.Web.Wms.WmsServer.ParseQueryString(Map map, WmsServiceDescription description, HttpContext context) at SharpMap.Web.Wms.WmsServer.ParseQueryString(Map map, WmsServiceDescription description) at AddressBase.Controllers.WMSController.Index()

I have check the URL. Map and Capabilities are all there its when it tries to parse the query string in the WMS Controller it throws the error.

ttpRequestBase request = HttpContext.Request;
        Uri uri = request.Url;
        string url;
        string absoluteUri = uri.AbsoluteUri;
        if (uri.Query.Length > 0)
            string s = absoluteUri.Replace(uri.Query, string.Empty);
            url = s;
            url = absoluteUri;

        Image image = null;

        Map map = ((SharpMap.Map)HttpContext.Application["Map"]).Clone();
        var cap = (SharpMap.Web.Wms.Capabilities.WmsServiceDescription)HttpContext.Application["Capabilities"];
            SharpMap.Web.Wms.WmsServer.ParseQueryString(map, cap);
            image = map.GetMap();
        catch (Exception ex) { return new ContentResult() { Content = ex.ToString() }; }

        var result = new ContentResult();

        result.ContentType = "image/png";
        if (image != null)
            result.Content = image.ToString();

        return result;
Not even sure where to start looking. Like I say it works fine in development its when its running compiled from within IIS that is throwing the error.


Feb 9, 2014 at 7:22 AM
Feb 9, 2014 at 10:10 AM

Yeh I checked the map and capabilities which have been stored in the application state and they were populated and valid. I looked into the session state and moved them over to session and still no luck.
Feb 9, 2014 at 10:37 AM
SharpMap.Web.Wms.WmsServer.ParseQueryString has an override that allows you to pass the HttpContext from the controller itself thus you do not have to rely on HttpContext.Current. The signature is

ParseQueryString(Map map, Capabilities.WmsServiceDescription description, HttpContext context)

to get HttpContext from HttpContextBase check this Stackoverflow Answer basically
HttpContext context = httpContextBase.ApplicationInstance.Context;
Feb 10, 2014 at 10:00 AM
Edited Feb 10, 2014 at 10:03 AM
Yeh I have checked the session / application variables within the controller and they are valid and hold the relevant information. I have even tried rebuilding them in the specific controller and everything appears to be valid that is passed to the ParseQueryString.

Even with passing in the context its still coming up with the same.