调用wcf

public ActionResult Index()
{
ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";

WCFTestClient wcf=new WCFTestClient();//这里在实际中替换成对应的部署对象

ViewBag.data = wcf.DoWork("某某用户");

string info= wcf.JsonData("生物","男",10000);
JavaScriptSerializer js = new JavaScriptSerializer();

List<UserModel> userList = new List<UserModel>();

///序列化或者加密处理
UserModel user=new UserModel();
userList = js.Deserialize<List<UserModel>>(info);

userList.Add(user);
return View();
}

wcf模块

1、接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace MvcClient
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IWCFTest”。
[ServiceContract]
public interface IWCFTest
{
[OperationContract]
string DoWork( string User);

[OperationContract]
string JsonData(string user,string sex,int age);
}
}

2、具体代码

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
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WCFTest”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WCFTest.svc 或 WCFTest.svc.cs,然后开始调试。
public class WCFTest : IWCFTest
{

public string DoWork(string User)
{
DateTime startTime = DateTime.Now;
string Data = string.Format("你好,{0}。欢迎使用wcf访问!", User);

string ShowInfo=string.Format("开始时间:{0},{1},结束时间:{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
// 生成json
//JavaScriptSerializer只能生成一个对象
List<UserModel> userList = new List<UserModel>();

for (int k = 0; k <= 10; 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).ToString()生成返回的会报错的,但是上述赋值却可以
return str;
}

}
}

对应的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 生产json对外的接口的更多相关文章

  1. 使用WCF对外提供接口

    本篇将通过WCF以webservices的方式对外提供接口.同时使用NUnit对webservices中的方法进行单元测试. 开发契约 contract Contract项目为类库项目,该项目下会包含 ...

  2. 开发FTP服务接口,对外提供接口服务

    注意:本文只适合小文本文件的上传下载,因为post请求是有大小限制的.默认大小是2m,虽然具体数值可以调节,但不适合做大文件的传输 最近公司有这么个需求:以后所有的项目开发中需要使用ftp服务器的地方 ...

  3. springboot+CXF开发webservice对外提供接口(转)

    文章来源:http://www.leftso.com/blog/144.html 1.项目要对外提供接口,用webservcie的方式实现 2.添加的jar包 maven: <dependenc ...

  4. WCF Ajax Json的应用

    WCF Ajax 的应用网上实际上有很多, 如: Ajax与Wcf交互-JSON jQuery调用WCF服务传递JSON对象 WCF以Json格式返回对象,客户端以JS调用显示 关于SoapUI的介绍 ...

  5. C++中模块(Dll)对外暴露接口的方式

    总结下C++中模块(Dll)对外暴露接口的方式: (1)导出API函数的方式这种方式是Windows中调用DLL接口的最基本方式,GDI32.dll, User32.dll都是用这种方式对外暴露系统A ...

  6. Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问

    Frp内网穿透搭建,家庭主机对外提供接口,支持ssh访问 1.使用场景: 需求1.家中服务器 ubuntu 主机,跑接口服务,需要对外暴漏, 需求2.同时需要在外网ssh远程 ​ 关键词: frp内网 ...

  7. WPF内嵌WCF服务对外提供接口

    要测试本帖子代码请记得管理员权限运行vs. 我写这个帖子的初衷是在我做surface小车的时候有类似的需求,感觉这个功能还挺有意思的,所以就分享给大家,网上有很多关于wcf的文章 我就不一一列举了.公 ...

  8. Java服务器对外提供接口以及Android端向服务器请求数据

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5056780.html 讲解下java服务器是如何对移动终端提供接口的,以什么数据格式提供出去,移动端又是怎么 ...

  9. JSON API免费接口

    来自:http://www.bejson.com/knownjson/webInterface/ 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuId ...

随机推荐

  1. 改变UITextField placeHolder色彩、字体

    改变UITextField placeHolder颜色.字体 我们有时需要定制化UITextField对象的风格,可以添加许多不同的重写方法,来改变文本字段的显示行为.这些方法都会返回一个CGRect ...

  2. 【Windows 8】pid为4的system进程占用80端口的解决办法

    因为Apache无法启动的原因,用netstat命令查看了一下80端口是否被占用了,如下: C:\Users\Maple>netstat -ano | findstr TCP LISTENING ...

  3. SQL server 时间日期函数、类型转换

    一.日期与时间函数 二.子查询与分页查询

  4. Git标签管理

    一般我们发布一个新版本到线上服务器时都会在版本库中打一个标签,这时就确定了某个版本将发布到线上.我们可以随时可以查看这个打标签的版本,也就 是说标签其实呢,就是版本库中一个快照.简单说标签就是指向某个 ...

  5. Windows Azure功能更新:SQL Server AlwaysOn和Notification Hub 正式商用

    一周以前Windows Azure发布了新的更新内容,主要的更新有3项: 虚拟机上的SQL Server支持AlwaysOn可用性组了 Notification Hub商用 自动缩放支持时间策略 这里 ...

  6. 【HDOJ】1930 And Now, a Remainder from Our Sponsor

    简单字符串. #include <stdio.h> #define MAXLEN 160 char buf[MAXLEN]; ], parts[]; void getpart(int x) ...

  7. (转载)浅谈javascript的分号

    (转载)http://www.blueidea.com/tech/web/2009/7261.asp javascript的分号代表语句的结束符,但由于javascript具有分号自动插入规则,所以它 ...

  8. google font和@font-face

    会使用google字体 网址: http://www.google.com/fonts/ 选择字体, quick use 引用css: <link href='http://fonts.goog ...

  9. Delphi安装/卸载OCX控件的方法

    delphi  安装卸载ocx 请参见 如下 http://blog.csdn.net/xt_chaoji/article/details/7027298 打开Delphi,关闭所有项目. 1.    ...

  10. php写excel

    $this->loadexcel();        $objPHPExcel = new PHPExcel();        $objPHPExcel->getProperties() ...