WCF, a feature of .NET 3.5, has simplified the method of creating web services by providing an alternative to the Interface Contract method. The alternative method involves the application of the Service Contract Attribute to the class as well as Operation Contract Attribute to each of the methods to be exposed by the Service Component.
The following is an example Class made available via WCF declared using Service & Operation Contract attributes.
<%@ ServiceHost Language="C#" Service="TS.ToplineService" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %> using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; using TS.ModelTypes; namespace TS { [ServiceContract(Namespace = "http://www.toplinestrategies.com/wcf")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class ToplineService { [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json)] public Person GetEmployeeInfo(int empNum) { Person result = new Person(); Person employeeA = new Person(); employeeA.Id = 1; employeeA.Name = "Daniel"; employeeA.Experience.Add("MS CRM 2 Yrs"); employeeA.Experience.Add("LINQ 1 Yr"); employeeA.Experience.Add("C# 3 Yrs"); employeeA.Experience.Add("VB 2 Yrs"); Person employeeB = new Person(); employeeB.Id = 2; employeeB.Name = "Siva"; employeeB.Experience.Add("MS CRM 5 Yrs"); employeeB.Experience.Add("C# 2 Yrs"); employeeB.Experience.Add("VB 2 Yrs"); Person employeeC = new Person(); employeeC.Id = 3; employeeC.Name = "Anuj"; employeeC.Experience.Add("PMP 10 Yrs"); employeeC.Experience.Add("BA 5 Yrs"); switch (empNum) { case 0: case 1: result = employeeA; break; case 2: result = employeeB; break; case 3: default: result = employeeC; break; } return result; } } }
Classes not encapsulated within the service must include the Data Contract attribute, and fields must adopt the Data Member attribute.using System.Collections.Generic; using System.Runtime.Serialization; namespace TS.ModelTypes { [DataContract] public class Person { private List<string> _experience = new List<string>(); [DataMember] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public List<string> Experience { get { return _experience; } set { _experience = value; } } } }Below is the code to call the WCF service via JavaScript/AJAX.
<script type="text/javascript"> function onGetEmpDetail() { var empNum = $get("txtWCF").value; // Web Service Call www.toplinestrategies.com.wcf.ToplineService.GetEmployeeInfo(empNum, onSucess, onFailed); } function onSucess(result) { var experience = ""; for (var i = 0; i < result.Experience.length; i++) { experience += result.Experience[i] + "<br>"; } $get("resultsDiv").innerHTML = "Employee Detail" + "<br>" + "Name: " + result.Name + "<br>" + "Experience: " + experience; } function onFailed(err) { alert('Some error has occured: ' + err.get_message()); } </script>
The attached code is an example of WCF in action which compares the use of an update panel to JavaScript & WCF to interact with the server.
Tags: Ajax, javascript, WCF










