WCF Production JSON external interface

Source: Internet
Author: User

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

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.