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. kettle转换设置变量,校验输出新变量

    背景:有很多小的转换需要串联起来,如果前一个执行成功,后面继续接着执行,如果执行等待中,就让程序等一会再次获取数据分析,如果失败就中止,成功就进行下一个转换,以此类推.... 需求:通过job把参数传 ...

  2. SQLite进阶-11.Join

    目录 JOIN 交叉连接 - CROSS JOIN 内连接 - INNER JOIN 外连接 - OUTER JOIN JOIN JOIN 子句用于结合两个或者多个数据表的数据,基于这些表之间的共同字 ...

  3. SQL Server中bcp命令的用法以及数据批量导入导出

    原文:SQL Server中bcp命令的用法以及数据批量导入导出 1.bcp命令参数解析 bcp命令有许多参数,下面给出bcp命令参数的简要解析 用法: bcp {dbtable | query} { ...

  4. 骨牌摆放方案数n*m(状压DP)

    题意:https://www.nitacm.com/problem_show.php?pid=1378 如题. 思路: 从第一行for到最后一行,枚举每一行的所有状态,进行转移,注意答案是dp[最后一 ...

  5. composer设置autoload自己的代码

    "autoload": { "psr-4": {"": ["App/base", "App/src/contr ...

  6. MySQL如何利用索引优化ORDER BY排序语

    MySQL索引通常是被用于提高WHERE条件的数据行匹配或者执行联结操作时匹配其它表的数据行的搜索速度. MySQL也能利用索引来快速地执行ORDER BY和GROUP BY语句的排序和分组操作. 通 ...

  7. go intall的使用

    1.首先GOPATH路径指向src的上级目录 2.设置GOBIN路径指向bin目录 3.查看环境配置 4.go install 在src目录下 5.完成 6.pkg ide编译运行一下自动生成

  8. 并不对劲的P5589

    题目大意 有\(n\)(\(n\leq 10^9\))个数:\(1,2,...,n\),每次操作是随机取一个没被删除的数\(x\),并删去\(x,x^2,x^3,...\). 求期望几次删完所有数. ...

  9. spring 的工厂类

    spring 的工厂类 1. 工厂类 BeanFactory 和 ApplicationContext 的区别. ApplicationContext 是 BeanFactory 的子接口,提供了比父 ...

  10. 怎样安装 cnpm 并切换到淘宝镜像?

    如果不使用 vpn , 在国内直接使用 npm 的官方镜像会很慢,这里推荐使用淘宝 NPM 镜像.淘宝 NPM 镜像是一个完整的 npmjs.org 镜像,可以用此代替官方版本(只读). 操作方法如下 ...