Blogger :
Pluralsight Blogs
All posts :
All posts by Pluralsight Blogs
Category :
WSCF/WCF
Blogged date : 2008 Feb 01
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!