AJAX-enabled WCF Service

Source: Internet
Author: User

I found WCF in Baidu encyclopedia just now:Windows Communication Foundation (WCF)Is a group of data communication application development interfaces developed by Microsoft. It is.. NET Framework. NET Framework 3.0 was introduced and used with Windows Presentation Foundation and Windows Workflow Foundation to develop three major application libraries: the next-generation Windows operating system and WinFX.

It is a set of data communication interfaces developed by Microsoft. Among them, wcf also implements Ajax services. With it, I can simplify Ajax development. Previously, when I wrote Ajax services, I needed to create an ashx handler page. to serialize the data, I also needed to deserialize the data at the front-end, which was quite troublesome.

Next, let's take a look at how to use it: in a web project, create --> Add a new item --> enable the XWCF service of AJA. Add a processing page with the suffix "svc". The approximate content is as follows:

PesrsonService. svc

[ServiceContract (Namespace = "")]
[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. Allowed)]
Public class PersonService
{
// Add the [WebGet] attribute to use HTTP GET
[OperationContract]
Public void DoWork () // This is an automatically generated example
{
// Add operation implementation here
Return;
}
[OperationContract] // The method marked with OperationContract can be called in Ajax.
Public Person GetPerson ()
{
Return new Person () {Name = "Yang fenghui", Age = 22 };
}
}
Public class Person
{
Public string Name {set; get ;}
Public int Age {set; get ;}
}

On the aspx page, add a ScriptManager control to the header, set the "service Set" attribute, and add a service. The path is the svc page just defined:

<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Services>
<Asp: ServiceReference Path = "~ /PersonService. svc "/>
</Services>
</Asp: ScriptManager>

In this way, I can call the method defined in PessonService. svc in the Javascript code of this page.

<Script language = "javascript" type = "text/javascript">
Function Button1_onclick (){
PersonService. GetPerson (function (data) {alert (data. Name );},
Function () {alert ("failed ");});
}
</Script>

After lunch, you can call the method defined above in the normal aspx through AJAX. The data can be fully read from the database for convenience of demonstration, A person class is written.

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.