WCF简单案例
1,定义接口层,引用System.ServiceModel
namespace Contracts
{
[ServiceContract(Name = "CalculatorService", Namespace = "http://www.test.com/")]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y); [OperationContract]
double Subtract(double x, double y); [OperationContract]
double Multiply(double x, double y); [OperationContract]
double Divide(double x, double y);
}
}
2,实现接口层,增加接口层引用
namespace Services
{
public class CalculatorService : ICalculator
{
public double Add(double x, double y)
{
return x + y;
} public double Subtract(double x, double y)
{
return x - y;
} public double Multiply(double x, double y)
{
return x * y;
} public double Divide(double x, double y)
{
return x / y;
}
}
}
3,创建wcf项目,增加配置信息
<system.serviceModel>
<services>
<service name="Services.CalculatorService" behaviorConfiguration="SingleBehavior">
<endpoint binding="basicHttpBinding" bindingConfiguration="SingleBinding" contract="Contracts.ICalculator"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SingleBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="SingleBinding" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
Service.svc中指定Service
<%@ ServiceHost Language="C#" Debug="true" Service="Services.CalculatorService" %>
4,创建服务端
namespace Client
{
class Program
{
static void Main(string[] args)
{
var c=new Client();
var testvalue= c.Add(1, 2);
Console.WriteLine(testvalue);
}
} public class Client : ClientBase<ICalculator>, ICalculator
{
public double Add(double x, double y)
{
return this.Channel.Add(x, y);
} public double Subtract(double x, double y)
{
return this.Channel.Subtract(x, y);
} public double Multiply(double x, double y)
{
return this.Channel.Multiply(x, y);
} public double Divide(double x, double y)
{
return this.Channel.Divide(x, y);
}
}
}
配置文件编写
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<client>
<endpoint address="http://wcf.test.com/Service.svc" binding="basicHttpBinding" bindingConfiguration="SingleBinding" contract="Contracts.ICalculator" name="SingleBinding" />
</client>
<bindings>
<basicHttpBinding>
<binding name="SingleBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" sendTimeout="00:10:00" receiveTimeout="00:10:00" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
WCF简单案例的更多相关文章
- Servlet请求头response应用简单案例
Servlet请求头response应用简单案例:访问AServlet重定向到BServlet,5秒后跳到CServlet,并显示图片: AServlet package cn.yzu; import ...
- winform 通过 html 与swf 交互 简单案例
在上一篇 winform 与 html 交互 简单案例 中讲了winform与html之间的简单交互,接下来的内容是在winform中以html为中转站,实现将swf嵌入winform中并实现交互. ...
- [Design Pattern] Front Controller Pattern 简单案例
Front Controller Pattern, 即前端控制器模式,用于集中化用户请求,使得所有请求都经过同一个前端控制器处理,处理内容有身份验证.权限验证.记录和追踪请求等,处理后再交由分发器把请 ...
- [Design Pattern] Observer Pattern 简单案例
Observer Pattern,即观察者模式,当存在一对多关系,例如一个对象一有变动,就要自动通知被依赖的全部对象得场景,属于行为类的设计模式. 下面是一个观察者模式的简单案例. Observer ...
- [Design Pattern] Mediator Pattern 简单案例
Meditor Pattern,即调解模式,用一个调解类类处理所有的沟通事件,使得降低多对象之间的沟通难度,属于行为类的设计模式.为了方便理解记忆,我也称其为,沟通模式. 下面是一个调解模式的简单案例 ...
- [Design Pattern] Iterator Pattern 简单案例
Iterator Pattern,即迭代时模式,按照顺序依次遍历集合内的每一个元素,而不用了解集合的底层实现,属于行为类的设计模式.为了方便理解记忆,我也会称其为遍历模式. 下面是一个迭代器模式的简单 ...
- [Design Pattern] Command Pattern 简单案例
Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order ...
- [Design Pattern] Proxy Pattern 简单案例
Proxy Pattern, 即代理模式,用一个类代表另一个类的功能,用于隐藏.解耦真正提供功能的类,属于结构类的设计模式. 下面是 代理模式的一个简单案例. Image 定义接口,RealImage ...
- [Design Pattern] Flywight Pattern 简单案例
Flywight Pattern, 即享元模式,用于减少对象的创建,降低内存的占用,属于结构类的设计模式.根据名字,我也将其会理解为 轻量模式. 下面是享元模式的一个简单案例. 享元模式,主要是重用已 ...
随机推荐
- JavaScript Array 对象方法
data.sort(function(a,b){return a.time>b.time?1:-1}); http://www.w3school.com.cn/jsref/jsref_obj_a ...
- Hibernate中HQLwhere用法和单独取出几列数据的读取方法
String hql = "select thedate,thehour,node,query_num from Cdns cdns where thehour = " +&quo ...
- FileZilla server windows 2003系统下适用的版本
最新版的FileZilla server 在windows 2003系统下已经不能用了 http://files.cnblogs.com/files/airoot/FileZilla_Server-0 ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- poj1564-Sum It Up(经典DFS)
给出一个n,k,再给出的n个数中,输出所有的可能使几个数的和等于k Sample Input 4 6 4 3 2 2 1 15 3 2 1 1400 12 50 50 50 50 50 50 25 2 ...
- calloc内存分配函数
calloc是一个C语言函数 函数名: calloc void *calloc(unsigned n,unsigned size): 功 能: 在内存的动态存储区中分配n个长度为size的连续空间,函 ...
- USB 驱动之 usb_register 函数解析
前段时间在kernel 添加了 USB to LAN 模块 AX88772B 的驱动. 根据相关添加解析一下 usb_register_driver 函数 drivers/net/usb/asix.c ...
- Java:单例模式的七种写法<转>
第一种(懒汉,线程不安全): 1 public class Singleton { 2 private static Singleton instance; 3 privat ...
- jackson2.1.4 序列化 通过给定Class映射 与抽象类的映射
//如果已知想要序列化的类型 可以使用TypeReference来进行处理 //List<MyBean> result = mapper.readValue(src, new TypeRe ...
- LINQ教程一:LINQ简介
一.为什么要使用LINQ 要理解为什么使用LINQ,先来看下面一个例子.假设有一个整数类型的数组,找到里面的偶数并进行降序排序. 在C#2.0以前,如果要实现这样的功能,我们必须使用'foreach' ...