Using MapPath with WCF Service, Setting aspNetCompatibilityEnabled to true

Posted by Peter Kellner on August 18, 2008 · 1 min read
Ad: Learn Modern JavaScript on YouTube (3 Hours & Free)

I'm just starting out using WCF in an application for the first time.  I'm using Visual Studio 2008 sp1 and created an asp.net web site from the standard create project wizard.  I then created a simple AJAX--enabled WCF Service as follows:

image

I then added some code to the service that looks like the following (using MapPath):

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class TrackData

{
    // Add [WebGet] attribute to use HTTP GET
    [OperationContract]

    public List<TrackDataList> RetrieveTrackDataByLap(int excerciseId, int lapNumberToShow)

    {
        var listTrackData = new List<TrackDataList>();

        string fileName = HttpContext.Current.Server.MapPath("~/App_Data/sample.tcl");

        Activity activity = GarminUtils.ConvertTCS(fileName);

I call it with JavaScript as follows:

/// <reference path="MapLive.aspx" />

/// <reference path="../VEJS/VeJavaScriptIntellisenseHelper.js" />

function pageLoad() {

    TrackData.RetrieveTrackDataByLap(0, 0, OnRetrieveTrackDataByLapComplete);

}

function OnRetrieveTrackDataByLapComplete(TrackDataList) {

    alert('OnRetrieveTrackDataByLapComplete called');

}

And, I'm surprised to find that I get a JavaScript error saying MapPath is undefined.  So, after a little digging I discover that I need to set aspNetCompatibilityEnabled to true in my web.config as follows:

xx

By default, it is set to false.  Hope this Helps!