Call the WCF
Public ActionResult Index ()
{
Viewbag.message = "Modify this template to quickly launch your ASP. NET MVC application. ";
WcfTestClient wcf=new wcftestclient ();//This is replaced by the corresponding deployment object in the actual
Viewbag.data = WCF. DoWork ("xxx user");
String info= WCF. Jsondata ("Creature", "male", 10000);
JavaScriptSerializer js = new JavaScriptSerializer ();
list<usermodel> userlist = new list<usermodel> ();
serialization or encryption processing
Usermodel user=new Usermodel ();
userlist = js. Deserialize<list<usermodel>> (info);
Userlist.add (user);
return View ();
}
WCF modules
1. Interface
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Serialization;
Using System.ServiceModel;
Using System.Text;
Namespace Mvcclient
{
Note: Using the rename command on the Refactor menu, you can change the interface name "Iwcftest" in code and configuration files at the same time.
[ServiceContract]
public interface Iwcftest
{
[OperationContract]
String DoWork (string User);
[OperationContract]
String Jsondata (String user,string sex,int age);
}
}
2. Specific code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Remoting.Contexts;
Using System.Runtime.Serialization;
Using System.ServiceModel;
Using System.Text;
Using System.Web.Script.Serialization;
Using System.Runtime.Serialization.Json;
Namespace Mvcclient
{
Note: Using the rename command on the Refactor menu, you can change the class name "Wcftest" in Code, SVC, and configuration files at the same time.
Note: In order to start the WCF test client to test this service, select Wcftest.svc or WCFTest.svc.cs in Solution Explorer and start debugging.
public class Wcftest:iwcftest
{
public string DoWork (string User)
{
DateTime startTime = DateTime.Now;
String Data = String. Format ("Hello, {0}". Welcome to use WCF Access! ", User);
String showinfo=string. Format ("Start time: {0},{1}, end time: {2}! ", Starttime,data,datetime.now);
return showinfo;
}
public string Jsondata (string user, string sex, int age)
{
JavaScriptSerializer js = new JavaScriptSerializer ();
Using System.Runtime.Serialization.Json.JsonReaderWriterFactory
Generate JSON
JavaScriptSerializer only one object can be generated
list<usermodel> userlist = new list<usermodel> ();
for (int k = 0; k <=; k++) {
Usermodel Usermodel = new Usermodel ();
Usermodel.user = user+k;
Usermodel.sex = sex + k;
Usermodel.age = age+ k;
list<string> templist = new list<string> ();
for (int j = 0; J <=2;j++) {
Templist.add (USER+J);
}
Usermodel.temp = templist;
Userlist.add (Usermodel);
}
String str = js. Serialize (userlist);
Js. Serialize (userlist). The ToString () generates a return error, but the above assignment can
return str;
}
}
}
The corresponding model
public class Usermodel
{
public string user {get; set;}
public string Sex {get; set;}
public int Age {get; set;}
Public list<string> Temp {get; set;}
}
WCF Production JSON external interface