Tuesday, January 29, 2013

Ajax and WCF services

For this excessive we loaded a master page. I am not going to show that First I will show the WCF code. We added an Ajax enabled WCF class to the project. Here is the code for that

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;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
 // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
 // To create an operation that returns XML,
 //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
 //     and include the following line in the operation body:
 //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
 [OperationContract]
 public string Hello(string name, string birthday)
 {
        string status = "It is not your birthday.";
        string today = DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
        if (today.Equals(birthday))
        {
            status = "Happy Birthday!";
        }
  // Add your operation implementation here
        return "hello, " + name + ". " + status;
 }

 // Add more operations here and mark them with [OperationContract]
}

Here is the code for the default content page. We added a script manager and made a reference to the service. then we added HTML controls and some JavaScript

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <!--script manager with a referemce to the service-->
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/Service.svc" />
        </Services>
    </asp:ScriptManager>
    <!--Use html to gather input-->
    <p>Enter Your Name <input id="txtName" type="text" /></p>
    <p>Enter birthday Month as a digit 1 to 12 <input id="txtMonth" type="text" /></p>
    <p>Enter birthday Day<input id="txtDay" type="text" /></p> 
    <button id="btnBirthday"  onclick="getBDay();" type="button">Submit</button>
    <p id="results"></p>

    <script type="text/javascript">
        function getBDay() {
            //get the values from the html controls
            var usrname = document.getElementById('txtName').value;
            var bMonth = document.getElementById('txtMonth').value;
            var bDay = document.getElementById('txtDay').value;
            var monthDay = bMonth + "/" + bDay;
            //call the function from the service. ServiceCallBack 
            //and ServiceErrorCallbacks are function calls 
            //to deal with the returned value from
            //service.Hello and with any errors returned
            Service.Hello(usrname, monthDay, ServiceCallBack, ServiceErrorCallBack);
        }
        function ServiceCallBack(result) {
            document.getElementById('results').innerText = result;
        }
        function ServiceErrorCallBack(error) {
            alert(error.get_message);
        }
    </script>

</asp:Content>


1 comment:

  1. Steve can u also post the WebServiceExample one too ?? I falsely deleted the master page of that one. Thank you!!

    ReplyDelete