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概述的更多相关文章

  1. WCF学习之旅—WCF概述(四)

    一.WCF概述 1) 什么是WCF? Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终 ...

  2. WCF从零学习之WCF概述(一)

    WCF从零学习之WCF概述(一) 一.WCF概述 我先了解了分布式应用程序开发,所谓分布式应用程序是指应用程序分布在不同计算机上,通过网络来共同完成一项任务.通常为服务器/客户端模式. 在WCF发布之 ...

  3. 第一节 WCF概述

    主要内容: 1.什么是WCF? 2.WCF的背景介绍. 引例:(WCF用来解决什么事情) 一家汽车租赁公司决定创建一个新的应用程序,用于汽车预定 • 该租车预定应用程序的创建者知道,应用程序所实现的业 ...

  4. WCF入门一[WCF概述]

    一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...

  5. WCF学习之旅—第三个示例之五(三十一)

       上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九) WCF学习 ...

  6. WCF学习之旅—WCF第二个示例(五)

    二.WCF服务端应用程序 第一步,创建WCF服务应用程序项目 打开Visual Studio 2015,在菜单上点击文件—>新建—>项目—>WCF服务应用程序.在弹出界面的“名称”对 ...

  7. WCF学习之旅—WCF第二个示例(七)

    三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...

  8. WCF学习之旅—WCF第二个示例(六)

    第五步,创建数据服务 在“解决方案资源管理器”中,使用鼠标左键选中“SCF.WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 在“添加新项”对话框中,选择“Web”节点,然 ...

  9. MSDN Webcast 跟我一起从零开始学WCF系列课程

    系列课程 >跟我一起从零开始学WCF系列课程   跟我一起从零开始学WCF系列课程(1):WCF概述 (Level 200)   讲 师:徐长龙    课程简介:从 本堂课开始我们将开启一个新的 ...

随机推荐

  1. 一文搞懂 Java 中的枚举,写得非常好!

    知识点 概念 enum的全称为 enumeration, 是 JDK 1.5 中引入的新特性. 在Java中,被 enum关键字修饰的类型就是枚举类型.形式如下: enum Color { RED, ...

  2. PHP生成中文验证码并检测对错实例

    PHP生成中文验证码并检测对错实例,中文验证码的例子还是比较少的,今天给大家分享一下,支持自定义中文.字体.背景色等 生成验证码,注意font字体路径要对,否则显示图片不存在 session_star ...

  3. crm--rbac权限组件使用步骤

    本人的权限组件码云地址:https://gitee.com/shiguanggege/rbac 里面有文档详细介绍权限组件的使用步骤

  4. Mysql-Sqlalchemy-多表操作

    import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declar ...

  5. Ubuntu下安卓模拟器的选择

    8G内存的话,一般开个AS,再启动默认的模拟器的话,基本就有点卡了,如果再打开Idea,很容易卡死. 所以两个spingboot的后台服务只能直接命令行跑个jar包,不方便调试,webview加载的v ...

  6. C Looooops

    看了半天的同余 扩展欧几里得 练练手 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27079   A ...

  7. js循环遍历性能

    定length for循环 (有length) 不定length for循环(使用数组length) 不定length for循环(判断数组length是否存在) forEach(Array自带,对某 ...

  8. Java 单个集合去重与两个集合去重

    一.单个集合去重 描述: 去掉一个集合里重复的元素:将list集合转成hashSet集合,hashSet有自动去重的功能,再利用去重后的hashSet集合初始化一个新的list集合,此时这个list就 ...

  9. VSCode主题自定义(附详细注释及本人主题分享)

    先来一张本人自己配置的主题截图,喜欢的拿去用: 下面说说怎么自定义主题: 1.     Ctrl + ,(Ctrl键 + 逗号键):打开设置,也可以依次点击编辑器左上角 => 文件 => ...

  10. Delphi 卡通控件

    樊伟胜