Wcf简单实例1
2016-04-28 13:21
393 查看
一、客户端添加服务引用,并调用
1.使用客户端代理同步调用
2.使用客户端代理异步调用
客户端配置:
View Code
服务端使用默认配置
1.使用客户端代理同步调用
static void TestTwo() { /*********同步访问********/ Person.PersonServiceClient client = new Person.PersonServiceClient(); string result = client.DoWork(new[] { 1, 3, 5 }); Console.WriteLine(result); DateTime serverTime = client.GetServerTime(); Console.WriteLine(serverTime); Dictionary<string, int> dic = client.GetDic(); foreach (var item in dic) { Console.WriteLine(item.Key + ":" + item.Value); } //服务店List集合,对应客户端数组 string[] strList = client.GetListStr(); Console.WriteLine(string.Join(",", strList)); Person.Person[] list = client.GetList(); foreach (var item in list) { Console.WriteLine(item.ID + "-" + item.Name + "-" + item.Birthday); } //关闭连接 client.Close(); }
2.使用客户端代理异步调用
static void TestThree() { /******异步访问******/ Person.PersonServiceClient client = new Person.PersonServiceClient(); Task<string> result = client.DoWorkAsync(new[] { 1, 3, 5 }); Task<DateTime> serverTime = client.GetServerTimeAsync(); Console.WriteLine(result.Result); Console.WriteLine(serverTime.Result); //关闭连接 client.Close(); Console.WriteLine("end"); }
客户端配置:
public class PersonService : IPersonService { public string DoWork(int[] numbers) { return string.Join("|", numbers); } public Dictionary<string, int> GetDic() { Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("one", 1); dic.Add("two", 2); return dic; } public List<Person> GetList() { List<Person> list = new List<Person>(); list.Add(new Person() { ID = 1, Name = "张三", Birthday = new DateTime(1990, 1, 1) }); list.Add(new Person() { ID = 2, Name = "李四", Birthday = new DateTime(1991, 1, 1) }); return list; } public DateTime GetServerTime() { return DateTime.Now; } public List<string> GetListStr() { List<string> list = new List<string>(); list.Add("张三"); list.Add("王芳"); return list; } }
View Code
服务端使用默认配置
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
相关文章推荐
- 构建之法阅读笔记五
- 【Web动画】CSS3 3D 行星运转 && 浏览器渲染原理
- GraphicsMagick在centos环境的安装
- maven的setting配置
- php 随机读取一维数组的一个或多个元素
- 命令行修改root密码
- 人生感悟(一)离职
- 视图
- EOS单向N对1关联
- double由于是双精度,所以比较大小不能直接用>,<或者=,需要使用BigDecimal,具体看例子
- 解读(五):分析KeyboardFragment, 带文字和表情的评论发表面板
- 团队项目个人工作总结(4.28)
- cscope插件,项目根目录、任意子目录下打开vim自动加载cscope.out
- 北京林业大学“计蒜客”杯程序设计竞赛 网络赛 A-喝酒【模拟】
- CSS3三维相册
- Echarts的使用和学习心得体会
- 单例模式
- POJ 1742 Coins(多重背包) DP
- PAT-B 1013. 数素数
- 蛇形矩阵