Creating JSON-enabled WCF services in .NET 3.5 – an even simpler approach

Onion Blog

Syndication

If some of you were reading my post yesterday and thinking "forget this – I'm going back to raw XmlHttpRequest…", fear not J

Ryan Dunn reminded me that WCF 3.5 supports an even simpler model for building script services than the approach I outlined – by setting Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" in your .svc file, you can completely remove all configuration file settings. In fact, you can reduce the entire service to a single file if you like by embedding the service class directly in the .svc file – here's a single file version of the WeatherService I posted yesterday:

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

[System.ServiceModel.ServiceContract(Namespace="http://www.pluralsight.com/ws/")]
[System.ServiceModel.Activation.AspNetCompatibilityRequirements(
RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Allowed)]
public class SimpleWeatherService
{
static System.Random _rand = new System.Random();

[System.ServiceModel.OperationContract]
public string GetForecast(string zip)
{
switch (_rand.Next(3))
{
case 0:
return "Sunny and warm";
case 1:
return "Cold and rainy";
case 2:
return "Windy with a chance of snow";
default:
return "Invalid";
}
}
}

Unfortunately, there is no wizard to generate a simple service like this in VS 2008. The best way to use this approach is to generate an Ajax-enabled WCF Service using the wizard, then delete all of the configuration goo it adds to web.config and manually add the Factory="…" attribute to your .svc file. You'll still be using the code-behind model, but you probably should be anyway since it keeps all your code in a single place in the site.

Enjoy!


Posted Feb 01 2008, 04:03 AM by fritz-onion
Filed under:

Comments

Steve wrote re: Creating JSON-enabled WCF services in .NET 3.5 – an even simpler approach
on 02-01-2008 5:20 AM
The docs say that "By default, service contracts exposed over an AJAX endpoint return data in the XML format". Does your sample return json serialized objects?
Fritz Onion wrote re: Creating JSON-enabled WCF services in .NET 3.5 – an even simpler approach
on 02-01-2008 5:28 AM
Hi Steve - yes, I have checked and it is indeed returning JSON. I know they have plans for supporting XML/SOAP calls from the client, but they're not in the current release.
-Fritz
Kor wrote re: Creating JSON-enabled WCF services in .NET 3.5 – an even simpler approach
on 03-03-2008 1:15 AM
I have tried your sample its working fine if I keep the client and service code in the same VS2008 project. But if I split client and service or I deploy the service to IIS 7 . Client could not receive the callback. Any idea, where I might be going wrong?
Regards.
mabra wrote re: Creating JSON-enabled WCF services in .NET 3.5 – an even simpler approach
on 05-01-2009 2:57 AM

Hi !

Sounds really good and simply. But - sad to say - I am new to that stuff and I get a client script error ["WeatherService" is undefined]. Any change to put a project/download onto this sample??

Thanks--mabra

Stuart wrote re: Creating JSON-enabled WCF services in .NET 3.5 – an even simpler approach
on 05-28-2009 11:59 AM

To fix the ["WeatherService" is undefined] error:

- right-click the website in your IIS manager and choose "properties". Then click the "Directory" tab, click the "Edit" button in the Anonymous access and authentication control" area. You will see that both Anonymous access and Integrated Windows Authentication are selected. Unselect one of them, then restart IIS. After that, the application should work smoothly.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?