WCF概述
Tips:概念性的东西仅助理解,可以略过
概述
1、SOA概述
1)、从三个问题开始
SOA是什么——面向服务架构。一种编程模式、一种架构模式。它将应用程序分成不同功能(服务)单元,再通过服务之间的接口与契约联系起来,是一种松耦合架构
SOA的优势与好处——从组织分布式应用程序上讲SOA允许用户将不同的服务单元分布在不同的计算机上,客户端业务通过组织服务单元处理相应业务。安全上SOA的服务单元声明和签名对用户公开,而服务单元的具体实现是不可见的。
SOA什么时候用——从需求讲,当应用程序或系统越来越复杂庞大的时候就需要用一个严格且可大量重用维护性高的架构,同时要满足客户群体分散环境的互联。面向对象组件式的架构难以满足要求,这时候就要用SOA.SOA方式的架构中严格定义的接口需要可靠定义,实际开发中需求可能在变化,所以服务接口开发迭代中要明确变化和未变化的部分。
2、SOA底层
SOAP 简单对象协议,一种xml规范。有消息头和消息体

WS-* 协议
WCF中利用WSHttpBinding就可以实现WS-*协议,消息的寻址,发现,调用可靠性,了解
WSDL

1.WCF的命名空间
System.ServiceModel
2.WCF的ABC
Address:地址,服务地址在哪里;
Binding:服务绑定的协议,安全设置;
Contract:契约;服务的操作约定、消息描述等
3.WCF托管
控制台、应用程序、服务、Web
4.WCF操作重载
在服务接口的操作契约加上Name属性,取不同名字即可([OperationContract(Name = "SumA")])
5.WCF信息交互
请求响应、单工通信(即不需要响应)、双工通信(通常TCP协议)
6.安全模式
安全配置主要在Bind中,根据不同的绑定设置
1.传输安全
<netTcpBinding>
<binding name="netTcpTransportBinding">
<security mode="Transport">
<Transport clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding> 2.消息安全
<wsHttpBinding>
<binding name="wsHttpMessageBinding">
<security mode="Message">
<Message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
3.
<basicHttpBinding>
<binding name="basicHttp">
<security mode="TransportWithMessageCredential">
<Transport />
<Message clientCredentialType="UserName" />
</security>
</binding>
</basicHttpBinding>
7.生成代理
svcutil.exe /out:D:/a.cs /config:D:/a.config http://localhost:8012/ComputeService.svc?wsdl
双工通信
双工通讯客户端泛型
DuplexClientBase<IConpute> 正常请求响应默认ClientBase<IConpute>
1、契约C
//设置回调
[ServiceContract(CallbackContract = typeof(ICallBack))]
public interface ICompute
{
[OperationContract(IsOneWay = true,Name = "AddDouble")]
void Add(double x, double y); [OperationContract(IsOneWay = true,Name = "AddInt")]
void Add(int x, int y);
}
//回调
[ServiceContract]
public interface ICallBack
{
[OperationContract(IsOneWay = true)]
void Show(string result);
}
2、服务端
1)service.svc.cs
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ComputeService : ICompute
{
public void Add(double x, double y)
{
var result = x + y;
ICallBack callBack = OperationContext.Current.GetCallbackChannel<ICallBack>();
callBack.Show($"Double:{x}+{y}={result}");
} public void Add(int x, int y)
{
var result = x + y;
ICallBack callBack = OperationContext.Current.GetCallbackChannel<ICallBack>();
callBack.Show($"INT:{x}+{y}={result}");
}
}
2)、服务端配置:configuration节点内
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors> <bindings>
<netTcpBinding>
<binding name="NetTcpBinding_ToolService" transferMode="Buffered" maxReceivedMessageSize="65536" portSharingEnabled="true">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Practice.Service.Web.ComputeService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ToolService" contract="Practice.Service.Contracts.ICompute" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
3)宿主IIS,Web高级设置里>> 协议 >>添加net.tcp,其他的诸如绑好端口即可
3、生成代理
双工通信继承 DuplexClientBase<T>;也可以适用svcutil.exe /out:C:/xx.cs /config:C:/xx.config
public class ComputeClient : DuplexClientBase<ICompute>, ICompute
{
public ComputeClient(System.ServiceModel.InstanceContext callbackInstance) :
base(callbackInstance)
{
} public ComputeClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName) :
base(callbackInstance, endpointConfigurationName)
{
} public ComputeClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
base(callbackInstance, endpointConfigurationName, remoteAddress)
{
} public ComputeClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(callbackInstance, endpointConfigurationName, remoteAddress)
{
} public ComputeClient(System.ServiceModel.InstanceContext callbackInstance, System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(callbackInstance, binding, remoteAddress)
{
} public void Add(double x, double y)
{
Channel.Add(x,y);
} public void Add(int x, int y)
{
Channel.Add(x,y);
}
}
4、客户端
1)配置
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="Compute_Binding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transactionProtocol="OleTransactions" maxBufferPoolSize="10485760" maxBufferSize="1310720" maxReceivedMessageSize="6553600">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://127.0.0.1:18012/ComputeService.svc" binding="netTcpBinding" bindingConfiguration="Compute_Binding" contract="Practice.Service.Contracts.ICompute" name="Compute_Endpoint"/>
</client>
</system.serviceModel>
</configuration>
2) program
class Program
{
static void Main(string[] args)
{
//InstanceContext instanceContext = new InstanceContext(new CallbackImpl());
//DuplexChannelFactory<ICompute> channelFactory =
// new DuplexChannelFactory<ICompute>(instanceContext, "Compute_Endpoint");
//var proxy = channelFactory.CreateChannel();
//using ( proxy as IDisposable)
//{ // proxy.Add(2, 1);
//} InstanceContext instanceContext = new InstanceContext(new CallbackImpl());
using (var proxy=new ComputeClient(instanceContext))
{
proxy.Add(, );
//proxy.Add(1.0,2.0);//通信关闭
}
using (var proxy = new ComputeClient(instanceContext))
{
proxy.Add(1.0,2.0);
} Console.ReadKey();
}
} [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class CallbackImpl : ICallBack
{
public void Show(string result)
{
Console.WriteLine(result);
} }
WCF概述的更多相关文章
- WCF学习之旅—WCF概述(四)
一.WCF概述 1) 什么是WCF? Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终 ...
- WCF从零学习之WCF概述(一)
WCF从零学习之WCF概述(一) 一.WCF概述 我先了解了分布式应用程序开发,所谓分布式应用程序是指应用程序分布在不同计算机上,通过网络来共同完成一项任务.通常为服务器/客户端模式. 在WCF发布之 ...
- 第一节 WCF概述
主要内容: 1.什么是WCF? 2.WCF的背景介绍. 引例:(WCF用来解决什么事情) 一家汽车租赁公司决定创建一个新的应用程序,用于汽车预定 • 该租车预定应用程序的创建者知道,应用程序所实现的业 ...
- WCF入门一[WCF概述]
一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...
- WCF学习之旅—第三个示例之五(三十一)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) WCF学习 ...
- WCF学习之旅—WCF第二个示例(五)
二.WCF服务端应用程序 第一步,创建WCF服务应用程序项目 打开Visual Studio 2015,在菜单上点击文件—>新建—>项目—>WCF服务应用程序.在弹出界面的“名称”对 ...
- WCF学习之旅—WCF第二个示例(七)
三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...
- WCF学习之旅—WCF第二个示例(六)
第五步,创建数据服务 在“解决方案资源管理器”中,使用鼠标左键选中“SCF.WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 在“添加新项”对话框中,选择“Web”节点,然 ...
- MSDN Webcast 跟我一起从零开始学WCF系列课程
系列课程 >跟我一起从零开始学WCF系列课程 跟我一起从零开始学WCF系列课程(1):WCF概述 (Level 200) 讲 师:徐长龙 课程简介:从 本堂课开始我们将开启一个新的 ...
随机推荐
- 栈与队列基本操作 Java实现
一.顺序栈 //数组实现顺序栈 public class OrderStack { private String[] elem; private int top; OrderStack(int k) ...
- # [洛谷1337] 吊打XXX/平衡点 (模拟退火)
[洛谷1337] 吊打XXX/平衡点 (模拟退火) 题意 n个重物(x,y,w),求平衡时x的位置(x,y) 分析 模拟退火基础题,基于随机数的优化算法,时间复杂度玄学,参数玄学,能不能AC看脸,当然 ...
- 如何给django admin.py配置超级管理员?注册表格?
admin.py是django给我们提供的功能非常强大的后台,况且支持拓展,,如果你要是觉得admin的后台不够牛逼你可以自己写一个!如何自己写一个后台,后面我有时间了会给大家更新!一起学习!一起进步 ...
- linux内核钩子--khook
简介 本文介绍github上的一个项目khook,一个可以在内核中增加钩子函数的框架,支持x86.项目地址在这里:https://github.com/milabs/khook 本文先简单介绍钩子函数 ...
- Java EE Servlet相关的两个包
Servlet in Java EE 在Java EE的规范API中(链接),Servlet相关联的最重要的两个Package为: 1.javax.servlet 包含了一系列接口和类,他们在一个Se ...
- Javascript——数据类型 和 注释
数据类型:JavaScript中包括如下7种数据类型:字符串.数字.布尔.数组.对象.null.undefined 字符串: 注意:字符串类型的数据需要使用单引号或双引号引起来. 数字: 注意:Jav ...
- 使用spring的test时,当配置文件不在classpath下,而在WEB-INF下
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/applicationContext.xml"})
- JSTreeShaking的webpack-deep-scope-plugin插件的应用
webpack自身实现词法分析的JSTreeShaking webpack-depp-scope-plugin插件实现作用域分析的JSTreeShaking 一.webpack词法分析的JSTreeS ...
- Java是面向对象的编程语言。它不仅吸收了C++语言的优点
Java是面向对象的编程语言.它不仅吸收了C++语言的优点,而且摒弃了C++中难于理解的多继承和指针的概念.因此,Java语言具有功能强大.使用方便的特点.Java语言作为静态面向对象的编程语言的代表 ...
- Shiro学习笔记总结,附加" 身份认证 "源码案例(一)
Shiro学习笔记总结 内容介绍: 一.Shiro介绍 二.subject认证主体 三.身份认证流程 四.Realm & JDBC reaml介绍 五.Shiro.ini配置介绍 六.源码案例 ...