1

G'day friends (I'm American, but G'day just sounds cool)

I just installed the WCF Data Services 5.0 package along with the latest ASP.NET Web API and am having a very annoying issue.. From searching on Google, I'm definitely not the only person who's experienced this problem in general but none of the solutions I have found seem to be working.

My problem is that the host application is not accepting requests that contain the $ character and no matter what I try it won't pass off the request to my JsonpMediaTypeFormatter. Because of that I have no idea if the formatter would or wouldn't fix the actual problem.

Using fiddler, I can see that the request is being made with an "Accepts" header of "*/*" which I imagine isn't going through to my formatter and I can't add "*/*" to the MediaTypeHeaderValue because it complains that it's a range.

Please any help or suggestions would be appreciated!


Full Description of Setup

I created a sandbox solution to test with that is composed of two ASP.NET web application projects. The first project is called ClientApplication and the second is called HostApplication (I'm sure you can deduce which does what). Each of them are hosted by my local IIS in DIFFERENT web sites and IP addresses. I did as much as I could to create them as if they were completely physically detached without actually needing a server.

HostApplication

Has a simple EDMX file mapping to a simpler database with a few basic tables in it, nothing fancy. I also have my service class as follows:

public class ODataService : DataService<Data.SandboxEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        config.UseVerboseErrors = true;
        config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
        config.SetEntitySetAccessRule("Employees", EntitySetRights.All);
        config.SetEntitySetAccessRule("RandomDatas", EntitySetRights.All);
        config.SetEntitySetAccessRule("Schedules", EntitySetRights.All);
        config.SetEntitySetAccessRule("Shifts", EntitySetRights.All);
    }
}

I'm using the same code for the JsonpMediaTypeFormatter that is used in a comment by Peter Moberg on the question: JSONP with ASP.NET Web API

Finally, I registered my JsonpMediaTypeFormatter in the Global.asax file:

    protected void Application_Start(object sender, EventArgs e)
    {
        var config = GlobalConfiguration.Configuration;
        config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
    }

Client Application

To bring it all together I have a VERY simple page in the client application with the following code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Service Client Application</title>
    <script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        function makeServiceCall()
        {
            try
            {
                /*
                $.getJSON('http://10.10.1.7/ODataService.svc/Customers?$format=json&$callback=?',
                function (response)
                {
                    $.each(response.d, function (index, value)
                    {
                        var div = document.createElement('div');
                        div.innerHTML = value.ClientName;
                        $('#result').append(div);
                    })
                });
                */

                $.ajax({
                    type: "POST",
                    url: "http://10.10.1.7/ODataService.svc/Customers",
                    dataType: "json",
                    contentType: "application/json",
                    success: function (result) { alert("Winning."); },
                    error: function (result) { alert("Losing..."); }
                });
            }
            catch (err)
            {
                alert(err);
            }
        }
    </script>
</head>
<body>
    <form id="MainForm" runat="server">
        <div id="result" style="border:1px solid black;background-color:#E8E8E8;"></div>
        <button onclick="makeServiceCall();return false;">Call Service</button>
    </form>
</body>
</html>

You might notice that there is a bit of commented javascript in there.. this is because I tried two different ways of making the service call (neither of which worked) and I wanted to be sure to note that to you guys.

Thanks again! Jason

Community
  • 1
  • 1
Jason
  • 565
  • 1
  • 6
  • 18
  • A couple of initial comments: 1. OData follows typical REST conventions with HTTP verbs, meaning that if you want to pull data back you'll need to use GET rather than POST. 2. You won't need the full WCF Data Services package AND Web API - to the best of my knowledge Web API will eventually use ODataLib, but their NuGet package should pull in what you need. 3. WCF Data Services itself doesn't support $format yet; Web API probably does but I don't know for sure. It looks like the service you're trying to access is a full WCF Data Services service. 4. Try out data.js for client access. – Mark Stafford - MSFT Jun 28 '12 at 21:08

1 Answers1

2

The JSONP helper you're trying to use is designed to work with WebAPI. Your service uses WCF Data Services. Even though they perform similar things, they are not the same. And the JSONP helper won't work with WCF Data Services. The one which does work can be found here: http://archive.msdn.microsoft.com/DataServicesJSONP

Note that in WCF DS 5.0 there's been a change in behavior around JSON, the comments on the page above show how to fix the code to work with 5.0 as well.

Vitek Karas MSFT
  • 13,130
  • 1
  • 34
  • 30