WCF and Ajax instances

Source: Internet
Author: User

Here, WCF achieves the same Ajax effect. The Ajax WCF Service is started, that is, the page is not refreshed when the page is accessed. This example is used to facilitate the comparison between WCF and Ajax. It does not explain the principles of the WCF Service and Ajax, but simply uses the instance code.

Today, when I was writing the WCF Service Code, I suddenly got short-circuited. I wrote this article to facilitate viewing examples and familiarize myself quickly.

I. Ajax source code

1. Create a new Handler or aspx page, and create a Handler page: NowTimeHandler

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;

Namespace MyWeb
{
/// <Summary>
/// $ Codebehindclassname $ abstract description
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class NowTimeHandler: IHttpHandler
{

Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
String nowtime = DateTime. Now. ToString ("MM mm dd, yyyy, hh MM mm MM ss second fff millisecond ");
Context. Response. Write (nowtime );
}

Public bool IsReusable
{
Get
{
Return false;
}
}
}
}

2. Access the page: Default. aspx

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "MyWeb. _ Default" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script type = "text/javascript"> 4
Function getTime (){
Var xmlhttp;
If (window. XMLHttpRequest ){
Xmlhttp = new XMLHttpRequest ();
} Else {
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Xmlhttp. open ("GET", "NowTimeHandler. ashx? Date = "+ new Date (). getSeconds (), false );
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 ){
If (xmlhttp. status = 200 ){
Document. getElementById ("lblTime"). innerHTML = xmlhttp. responseText;
}
}
}
Xmlhttp. send ();
}
SetInterval ("getTime ()", 1 );
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Label id = "lblTime"> </label>
</Div>
</Form>
</Body>
</Html>

 

2. Implementing the same Ajax function in WCF

1. Create a WCF Service item that starts the Ajax service: ServiceNowTime. svc

Using System;
Using System. Linq;
Using System. Runtime. Serialization;
Using System. ServiceModel;
Using System. ServiceModel. Activation;
Using System. ServiceModel. Web;

Namespace MyWeb. MyWCF
{
[ServiceContract (Namespace = "tnj")]
[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. Allowed)]
Public class ServiceNowTime
{
// Add the [WebGet] attribute to use HTTP GET
[OperationContract]
Public void DoWork ()
{
// Add operation implementation here
Return;
}

[OperationContract]
Public string GetNowTime ()
{
// String nowtime = DateTime. Now. ToString ();
String nowtime = DateTime. Now. ToString ("MM: dd, yyyy, hh, mm: ss seconds fffff millisecond ");
Return nowtime;
}

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

[DataContract]
Public class Person
{
[DataMember]
Public int ID {get; set ;}
[DataMember]
Public string Name {get; set ;}
[DataMember]
Public int Age {get; set ;}
[DataMember]
Public string Address {get; set ;}
}
}

2. Create a page to access the WCF Service: WebForm1.aspx

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "MyWeb. MyWCF. WebForm1" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script type = "text/javascript">
Function getNowTime (){
Tnj. ServiceNowTime. GetNowTime (function (data ){
Document. getElementById ("lblNowTime"). innerHTML = data;
}, Function (data ){
Alert (data. get_message ());
});
Window. setTimeout ("getNowTime ()", 1 );
}
</Script>
</Head>
<Body onload = "getNowTime ()">
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "sm1" runat = "server">
<Services>
<Asp: ServiceReference Path = "~ /MyWCF/ServiceNowTime. svc "/>
</Services>
</Asp: ScriptManager>
<Div>
<Label id = "lblNowTime"> </label>
</Div>
</Form>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.