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, 即享元模式,用于减少对象的创建,降低内存的占用,属于结构类的设计模式.根据名字,我也将其会理解为 轻量模式. 下面是享元模式的一个简单案例. 享元模式,主要是重用已 ...
随机推荐
- 修改IIS下默认的ASP.NET版本。
已经安装net2.0 和3.5 ,但IIS里面却只有1.1 开始→运行→CMD C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis. ...
- tomcat8源码分析-Connector初始化
谈起Tomcat的诞生,最早可以追溯到1995年.近20年来,Tomcat始终是使用最广泛的Web服务器,由于其使用Java语言开发,所以广为Java程序员所熟悉.很多人早期的J2EE项目,由程序员自 ...
- 使用HttpWebRequest调用wcf一段代码
public class HttpClass { internal static HttpWebRequest _httpWebRequest; public static void Request( ...
- SQL数据库,如何把服务器中的一张表插入到另外一个服务器的一张表中
先开启 exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Qu ...
- LINQ教程一:LINQ简介
一.为什么要使用LINQ 要理解为什么使用LINQ,先来看下面一个例子.假设有一个整数类型的数组,找到里面的偶数并进行降序排序. 在C#2.0以前,如果要实现这样的功能,我们必须使用'foreach' ...
- ruby send respond_to
http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html http://galeki.is-progr ...
- 什么是服务端渲染、客户端渲染、SPA、预渲染,看完这一篇就够了
服务端渲染(SSR) 简述: 又称为后端渲染,服务器端在返回html之前,在html特定的区域特定的符号里用数据填充,再给客户端,客户端只负责解析HTML. 鼠标右击点击查看源码时,页 ...
- SQL计算表的列数
select count(syscolumns.name) from syscolumns , sysobjects where syscolumns.id = sysobjects.id and s ...
- 表单提交 多个name相同的input
<form action="{:U('Index/test')}" method="post"> <foreach name="di ...
- 关于在Android中访问和使用到上下文变量
在监听器内部实现类中要引用上下文变量this的时候 一.采用类名.this的方法 FActivity.this 二.采用全局变量当做中间变量 1.先定义一个全局变量 private Context m ...