1. How to create a WCF service in the first step
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;
Namespace WcfService1
{
[ServiceContract (Namespace = "Valsun", Name = "Service1")]
[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
public class HelloWorldService
{
To use HTTP GET, add the [WebGet] attribute. (Default Responseformat is Webmessageformat.json)
To create an operation that returns XML,
Please add [WebGet (Responseformat=webmessageformat.xml)],
and include the following lines in the action body:
WebOperationContext.Current.OutgoingResponse.ContentType = "Text/xml";
[OperationContract]
public string HelloWorld ()
{
Add an action implementation here
Return "You are good ah good";
}
Add more actions here and mark them with [OperationContract]
[OperationContract]
Public dictionary<string, string> gettestlist (string id)
{
Define a Dictionary object
dictionary<string, string> dic = new dictionary<string, string> ();
Easy to test, directly constructs some data to simulate the data in the database
Dic. ADD ("1" + ID, "Shanghai");
Dic. ADD ("2" + ID, "Beijing");
Dic. ADD ("3" + ID, "Guangzhou");
Dic. ADD ("4" + ID, "Shenzhen");
Dic. ADD ("5" + ID, "Nanjing");
return value
return dic;
}
}
}
Second step to create a page call method
<%@ Page language="C #"inherits="system.web.mvc.viewpage<dynamic>"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title>Index</title> <script src="<%= url.content ("~/scripts/jquery-1.4.1. js")%>"type="Text/javascript"></script> "Form1"runat="Server"> <div> <br/> <br/> <input id="btnquerydictionary"Type="Button"Value="Test"onclick="btnquery ();"/> <br/> <br/>message:<p id="lblmsg"></p> </div> <script type="Text/javascript">function Btnquery () {varID ="1"; varWcfproxy =NewValsun.service1 (); Wcfproxy.gettestlist (ID, onsucceededcallback, onfailedcallback); } function Onsucceededcallback (result, UserContext, methodName) {if(MethodName = ="gettestlist") { varmsg =""; //pay attention to the way of access here!!! for(varKeyinchresult) {msg+ = Result[key]. Key +" : "+ Result[key]. Value +"\ n"; } $("#lblMsg"). HTML (msg); }} function Onfailedcallback (Error, UserContext, methodName) {alert ("Exception Information:"+ error.get_message () +"\ n"+"Exception Type:"+ error.get_exceptiontype () +"\ n"+"Stack information:"+error.get_stacktrace ()); }</script> <asp:scriptmanager id="ScriptManager1"runat="Server"> <Services> <asp:servicereference path="http://localhost:2813/HelloWorldService.svc"/> </Services> </asp:ScriptManager></form></body>View CodeReturned is the dictionary collection, very simple, I misplaced the reason is to write a wrong word, this is implemented with jquery
JavaScript calls the WCF service and processes the returned dictionary collection