Programming Microsoft ASP.NET 4 - Dino Esposito [461]
<%@ ServiceHost
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
Service="Samples.Services.TimeService" %>
Note that you can use simplified configuration only for service classes that implement one contract only.
The definition of the service contract for an AJAX-enabled WCF service is not different from that of any other WCF services. You use the OperationContract attribute to qualify a method as a public service method, and you use the optional WebGet and WebInvoke attributes to configure the URL template. Here’s an example:
[ServiceContract(Namespace="Samples.Services", Name="TimeService")]
public interface ITimeService
{
[OperationContract]
DateTime GetTime();
[OperationContract]
string GetTimeFormat(string format);
}
public class TimeService : ITimeService
{
public DateTime GetTime()
{
return DateTime.Now;
}
...
}
You should be sure to give meaningful values to the Namespace and Name properties of the ServiceContract attribute. The reason is that the concatenation of those values determines the name of the JavaScript proxy class used to access the WCF service. If you leave them blank, the JavaScript proxy for the preceding service will be named tempuri.org.ITimeService. Not really a nice or helpful name!
For AJAX-enabled WCF services, the data contract—namely, the agreement between the service and client that describes the data to be exchanged—is defined in the canonical way. You use an implicit contract for serialization, and deserialization is used for collections, primitive types, dates, enumerations, and the GUID; an explicit contract is required for custom complex types. In this case, you use the DataContract and DataMember attributes on class members to determine which members go into the serialization stream.
Important
The configuration of a WCF service is different if the client is a Silverlight application. In such a case, in fact, you are not allowed to use webHttpBinding and must resort to the basicHttpBinding model, which executes the method call over a SOAP 1.1 channel.
ASP.NET Web Services
The primary reason for choosing ASP.NET Web services instead of WCF as the technology for building your HTTP façade is backward compatibility. You can call ASP.NET Web services from AJAX clients as long as your Web server runs the Microsoft .NET Framework 2.0 plus AJAX Extensions 1.0. For WCF services, ASP.NET 3.5 or a newer version is required.
A Web service made to measure for an ASP.NET AJAX application is similar to any other ASP.NET Web service you might write for whatever purposes. Just one peripheral aspect, though, marks a key difference. You must use a new attribute to decorate the class of the Web service that is not allowed on regular ASP.NET Web services—the ScriptService attribute. Here’s how to use it:
namespace Samples.WebServices
{
[ScriptService]
[WebService(Namespace = "urn:aspnet4.book/")]
public class TimeService : System.Web.Services.WebService, ITimeService
{
[WebMethod]
public DateTime GetTime()
{
return DateTime.Now;
}
...
}
}
Note that the ScriptService attribute simply enables AJAX callers to connect to the service; it doesn’t prevent SOAP callers from sending their packets. As a result, an ASP.NET AJAX Web service might have a double public interface: the JSON-based interface consumed by the hosting ASP.NET AJAX application, and the classic SOAP-based interface exposed to any clients, from any platforms, that can reach the service URL.
When you write an AJAX-enabled ASP.NET Web service, you have no need for a contracted interface as with WCF services. However, extracting an interface from the service class is rarely a bad idea.
public class TimeService : System.Web.Services.WebService, ITimeService
{
[WebMethod]
public DateTime GetTime()
{
return DateTime.Now;
}
...
}
Public