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.