WCF简单Demo
WCF,光看书的原理,稍微有点枯燥,通过自己动手,会更容易理解契约声明,面向服务,分布式等概念。
1.创建WCF服务。
2.WcfService1.CS中声明新的契约。
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);//声明契约
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此添加您的服务操作
[OperationContract]
int GetSum(DateTime time);
[OperationContract]
string GetShopInfo(string address);
[OperationContract(IsOneWay=true)]
void onewaytest();
}
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
3.WcfService1.svc.cs中编写相应的方法
public int GetSum(DateTime time)
{
int BreadNum = 10;
int Milk = 5;
int hotdog = 20;
int today = BreadNum + Milk + hotdog;
return today;
}
public string GetShopInfo(string address)
{
if (address == "武汉")
{
return "武汉KFC";
}
else if (address =="上海")
{
return "上海KFC";
}
else if (address == "北京")
{
return "北京KFC";
}
else
{
return "没有该连锁店";
}
}
public void onewaytest()
{
Console.WriteLine("hello world");
}
4.启动服务测试。
5.新建winform窗体和webapplication来测试WCF的跨平台性。
a.分别添加服务引用。
b.编写代码测试
winform
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
using(
WindowsFormsApplication1.ServiceReference1.Service1Client ser=new WindowsFormsApplication1.ServiceReference1.Service1Client()){
textBox1.Text = ser.GetSum(Convert.ToDateTime(dateTimePicker1.Text)).ToString();
}
}
webapplication
protected void Button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{
using (WebApplication1.ServiceReference1.Service1Client ser = new WebApplication1.ServiceReference1.Service1Client())
{
TextBox2.Text = ser.GetShopInfo(TextBox1.Text);
}
}
else
{
TextBox2.Text = "无法检索,字符串为空";
}
}
c.最后在解决方案中记得将这三个项目设置为多项目启动。
d.反馈结果:
总结:这是一个简单的demo演示,还有很多复杂的功能,只是通过这么一个简单的demo,确实能够让我们深化理解基础概念,为以后的学习打下基础。
WCF简单Demo的更多相关文章
- 设计模式之单例模式的简单demo
/* * 设计模式之单例模式的简单demo */ class Single { /* * 创建一个本类对象. * 和get/set方法思想一样,类不能直接调用对象 * 所以用private限制权限 * ...
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- 使用Spring缓存的简单Demo
使用Spring缓存的简单Demo 1. 首先创建Maven工程,在Pom中配置 <dependency> <groupId>org.springframework</g ...
- Managed DirectX中的DirectShow应用(简单Demo及源码)
阅读目录 介绍 准备工作 环境搭建 简单Demo 显示效果 其他 Demo下载 介绍 DirectX是Microsoft开发的基于Windows平台的一组API,它是为高速的实时动画渲染.交互式音乐和 ...
- angular实现了一个简单demo,angular-weibo-favorites
前面必须说一段 帮客户做了一个过渡期的项目,唯一的要求就是速度,我只是会点儿基础的php,于是就用tp帮客户做了这个项目.最近和客户架构沟通,后期想把项目重新做一下,就用现在最流行的技术,暂时想的使用 ...
- Solr配置与简单Demo[转]
Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...
- 二维码简单Demo
二维码简单Demo 一.视图 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...
- android JNI 简单demo(2)它JNI demo 写
android JNI 简单demo(2)它JNI demo 写 一.搭建Cygwin 环境:http://blog.csdn.net/androidolblog/article/details/25 ...
- Ext简单demo示例
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
随机推荐
- HashMap 中7种遍历方式的性能分析
随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历方法讲起,然后再从性能. ...
- 通过UnityWebRequest得到的纹理转精灵
private IEnumerator GetImage(string url, Image fileImage) { UnityWebRequest WebRequest = new UnityWe ...
- Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...
- PowerMock 支持gRPC的Mock Server实现
PowerMock是一个Mock Server的实现,它同时支持HTTP与gRPC协议接口的Mock,并提供了灵活的插件功能. 这个工具面向于前后端.测试等对有接口Mock需求的开发人员,也可以作为一 ...
- java list 分页
/** * * @param list * @param pageNum * @param pageSize * @param <T> * @return 返回当前页数据 */ publi ...
- centos 8.3安装 一键安装部署gitlab
安装和配置gitlab必须的依赖包 [root@gitlabdev ~]#dnf install -y curl policycoreutils openssh-server perl 设置开机自启s ...
- MySql:mysql命令行导入导出sql文件
命令行导入 方法一:未连接数据库时方法 #导入命令示例 mysql -h ip -u userName -p dbName < sqlFilePath (结尾没有分号) -h : 数据库所在的主 ...
- The Different of Python 2.x and 3.x
为了不给Python3.0带入过多的累赘,Python 3.x没有考虑向下兼容 1,print 函数 print语句没有了,取而代之的是print()函数. Python2.7 print " ...
- 解决mount.nfs: access denied by server while mounting
在linux下进行挂载时突然出现: mount.nfs: access denied by server while mounting 第一感觉是读取文件权限不够,准备去更改一下挂载点的权限,但又考 ...
- 微信小程序支付 后台处理逻辑 (转)
<?phpnamespace app\parent\controller; use think\Request; class Wxpay{ function wechat(){ //微信配 ...