Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2

With all the out-of-band technology releases we've had (ASP.NET AJAX, .NET 3.0), it's nice to reach a point where we can bring them all together.

As an example...

Create a new web site in Visual Studio 2008. This will have to be a website under IIS, unfortunately, for reasons I'll point out later. Once the web site is up, add a new item – a WCF service. The service contract can look like the following:

[ServiceContract(Namespace="http://OdeToCode.com/ws",
                 Name=
"ServerProcessInfo")]
public interface IServerProcessInfo
{
   [
OperationContract]
    
IEnumerable<ProcessInfo> GetRunningProcesses();
}

The data contract can look like so:

[DataContract]
public class ProcessInfo
{
    [
DataMember]
    
public string Name { get; set; }

    [
DataMember]
    
public long WorkingSet { get; set; }
}

Finally, the LINQish implementation:

public class ServerProcessInfo : IServerProcessInfo
{
    
public IEnumerable<ProcessInfo> GetRunningProcesses()
    {
        
return
              (
                
from p in Process.GetProcesses()
                
orderby p.WorkingSet64 descending
                 select new ProcessInfo
                 {
                     Name = p.ProcessName,
                     WorkingSet = p.WorkingSet64
                 }
              ).Take(10);
    }
}

We can entirely remove any WCF <system.serviceModel> configuration from web.config. Instead of all the XML configuration goo, we just need a Factory attribute in our .svc file:

<%@ ServiceHost Language="C#" Debug="true" Service="ServerProcessInfo"
  
...
  Factory
="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

What magic does this factory give us? Well, we can add a ServiceReference via a ScriptManager (it's nice that AJAX extensions are in the toolbox by default), and write some script:

<asp:ScriptManager ID="ScriptManager1" runat="server">
  <Services>
    <asp:ServiceReference Path="~/ServerProcessInfo.svc" />
  </Services>
</
asp:ScriptManager>
        
<script type="text/javascript">

var
ws = new odetocode.com.ws.ServerProcessInfo();
ws.GetRunningProcesses(getRunningProcessesComplete);

function getRunningProcessesComplete(result)
{      
    
for(var i = 0; i < result.length; i++)
    {
         document.write(result[i].Name,
" ", result[i].WorkingSet);
         document.write(
"<br />");

    }    
}

Voila! Zero configuration and we have JSON on the wire!

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Tue, 31 Jul 2007 03:08:10 GMT
Content-Length: 662

{"d":[{"__type":"ProcessInfo:#","Name":"devenv","WorkingSet":51011584},
{"__type":"ProcessInfo:#","Name":"w3wp","WorkingSet":44748800},
{"__type":"ProcessInfo:#","Name":"Fiddler","WorkingSet":34213888},
...
}

Note: there is a problem in Beta 2 that prevents this magic from working with WebDev.exe. The error you'll see with WebDev (aka Cassini) is:

"IIS specified authentication schemes 'Ntlm, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used."

Unfortunately, twiddling with the NTLM checkbox for WebDev doesn't help. Thus, the current need for IIS.

Print | posted @ Tuesday, July 31, 2007 2:12 AM

Comments on this entry:

Gravatar # re: Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2
by Rick Strahl at 7/31/2007 6:17 AM

Cool - I haven't had a chance to look at this yet.

Have you tried passing complex values back? The real trick is to find out what can and can't be serialized back from JSON by the service since there were fairly big limitations with PageMethods/ASMX.

One thing that has bugged me about the MS Ajax implementation is that it REQUIRED that the types originated on the server (ie. the JSON had a bunch of .NET specific payload associated with it so it wasn't truly generic.

  
Gravatar # re: Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2
by scott at 7/31/2007 11:53 AM

Haven't tried anything too complex as yet...
  
Gravatar # re: Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2
by Chris Frazier at 8/2/2007 4:50 PM

There is some batch file that you're supposed to run if you want to work with web sites/webdev.exe. Does this batch file not address this issue? I haven't run it yet, just curious.
  
Gravatar # re: Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2
by Craig Cameron at 8/7/2007 7:05 AM

Are there any estimates about when VS2008 we will be able to buy VS2008?
  
Gravatar # re: Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2
by Chris Evans at 10/28/2009 3:54 PM

Hi Scott,

I realize it's been a while since you wrote this article, but i was wondering if you had decoupled the wcf service and website. When i do it, i (apparently) don't know how to reference the js proxy. I get a Microsoft JScript myservice undefined error.

Nice site btw,

Chris
  
Gravatar # re: Configuration Free JSON with WCF and AJAX in Visual Studio 2008 Beta 2
by Scott Allen at 10/28/2009 4:46 PM

@Chris:

You might try using Fiddler or an HTTP trace tool to see how the web service responds to the /js request that generates a JavaScript proxy. That's probably where I would start.

The biggest problem with decoupling might be cross-domain security issues if "decoupled" means different web server names.

Hope that provides some help,
  
Comments have been closed on this topic.
Scott Allen
Posts - 869
Comments - 4493
Stories - 14