一、客户端添加服务引用,并调用

1.使用客户端代理同步调用

static void TestTwo()
{
/*********同步访问********/
Person.PersonServiceClient client = new Person.PersonServiceClient();
string result = client.DoWork(new[] { , , });
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[] { , , });
Task<DateTime> serverTime = client.GetServerTimeAsync();
Console.WriteLine(result.Result);
Console.WriteLine(serverTime.Result); //关闭连接
client.Close();
Console.WriteLine("end");
}

客户端配置:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
<binding name="BasicHttpBinding_IPersonService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:60957/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceOne.IService1"
name="BasicHttpBinding_IService1" />
<endpoint address="http://localhost:60957/PersonService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPersonService"
contract="Person.IPersonService" name="BasicHttpBinding_IPersonService" />
</client>
</system.serviceModel>

二、接口契约和数据契约

[ServiceContract]
public interface IPersonService
{
[OperationContract]
string DoWork(int[] numbers); [OperationContract]
DateTime GetServerTime(); [OperationContract]
List<string> GetListStr(); [OperationContract]
List<Person> GetList(); [OperationContract]
Dictionary<string, int> GetDic();
} [DataContract]
public class Person
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public DateTime Birthday { get; set; }
}

三、接口实现

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", );
dic.Add("two", ); return dic;
}
public List<Person> GetList()
{
List<Person> list = new List<Person>(); list.Add(new Person()
{
ID = ,
Name = "张三",
Birthday = new DateTime(, , )
}); list.Add(new Person()
{
ID = ,
Name = "李四",
Birthday = new DateTime(, , )
});
return list;
}
public DateTime GetServerTime()
{
return DateTime.Now;
}
public List<string> GetListStr()
{
List<string> list = new List<string>();
list.Add("张三");
list.Add("王芳");
return list;
}
}

服务端使用默认配置

<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>

Wcf简单实例1的更多相关文章

  1. C# 一个WCF简单实例

    以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 复制代码 代码如下: namespace WcfDemo { // 注意: 如果更改此处的接口名称 &qu ...

  2. WCF简单实例--用Winform启动和引用

    以订票为例简单应用wcf程序,需要的朋友可以参考下 本篇转自百度文档,自己试过,确实可以用. 以订票为例简单应用wcf 新建一个wcf服务应用程序 在IService1.cs定义服务契约 namesp ...

  3. .net WCF简单实例

    最近看到网上招聘有许多都需要WCF技术的人员,我之前一直没接触过这个东西,以后工作中难免会遇到,所谓笨鸟先飞,于是我就一探究竟,便有了这边文章.由于是初学WCF没有深入研究其原理,只是写了一个demo ...

  4. 【5】.net WCF 简单实例

    1.创建WCF项目 2.系统自动生成IWcfService // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”. [ServiceContra ...

  5. WCF 学习总结1 -- 简单实例

    从VS2005推出WCF以来,WCF逐步取代了Remoting, WebService成为.NET上分布式程序的主要技术.WCF统一的模型整合了以往的 WebService.Remoting.MSMQ ...

  6. WCF分布式开发步步为赢(9):WCF服务实例激活类型编程与开发

    .Net Remoting的激活方式也有三种:SingleTon模式.SingleCall模式.客户端激活方式,WCF服务实例激活类型包括三种方式:单调服务(Call Service),会话服务(Se ...

  7. WCF小实例以及三种宿主

    WCF小实例以及三种宿主 最近一直在学习WCF相关知识,下面将通过一个小实例对所学的知识进行简单的回顾:本实例是一个简单三层操作数据库,并且也简单实现的三种宿主(控制台宿主,IIS宿主以及Window ...

  8. Hibernate(二)__简单实例入门

    首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...

  9. 最新 Eclipse IDE下的Spring框架配置及简单实例

    前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...

随机推荐

  1. centos 下搭建 php环境(2) mysql 安装

    CentOS下的MySQL 5.1安装   01 1.下载源码包 wget http://mysql.llarian.net/Downloads/MySQL-5.1/mysql-5.1.63.tar. ...

  2. 转:有事务处理的NoSQL数据库

    原文来自于:http://www.infoq.com/cn/articles/MarkLogic-NoSQL-with-Transactions Java平台在其几乎整个生命周期中,都在煞费苦心地努力 ...

  3. mysql中lock tables与unlock tables

    官网:https://dev.mysql.com/doc/refman/5.0/en/lock-tables.html LOCK TABLES tbl_name [[AS] alias] lock_t ...

  4. 转:Yii实战中8个必备常用的扩展,模块和widget

    转载自:http://www.yiiframework.com/wiki/180/yii8/ 在经过畅K网的实战后,总结一下在Yii的项目中会经常用到的组件和一些基本的使用方法,分享给大家,同时也给自 ...

  5. 玩转12款Linux开源机器人

    玩转12款Linux开源机器人 头条网2016-02-15 09:04 3DR Solo智能无人机发布于2015年中期.作为试图与大疆广受欢迎的Phantom系列无人机相抗衡的产品,它的双处理器运行L ...

  6. MapReduce执行过程源码分析(一)——Job任务的提交

    为了能使源码的执行过程与Hadoop权威指南(2.3版)中章节Shuffle and Sort的分析相对应,Hadoop的版本为0.20.2. 一般情况下我们通过Job(org.apache.hado ...

  7. COCI2014-2015CONTEST#7——POLICE

    http://www.hsin.hr/coci/archive/2014_2015/contest7_tasks.pdf [题目描述] 有N个书架,每个书架可以容纳M本书.给出了若干本书,每本书有一个 ...

  8. CSS3学习笔记(新属性)

    1. 边框(圆角边框.加阴影和用图片绘制) 新增加 border-radius  box-shadow  border-image .div1{ border:2px solid purple; bo ...

  9. 如何编写一个JSON解析器

    编写一个JSON解析器实际上就是一个函数,它的输入是一个表示JSON的字符串,输出是结构化的对应到语言本身的数据结构. 和XML相比,JSON本身结构非常简单,并且仅有几种数据类型,以Java为例,对 ...

  10. Java中Websocket使用实例解读

    介绍 现在很多网站为了实现即时通讯,所用的技术都是轮询(polling).轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发出HTTP request,然后由服务器返回最新的数据给客服端的浏览器 ...