Skip to content

Instantly share code, notes, and snippets.

@zhuqling
Last active December 19, 2015 01:19
Show Gist options
  • Save zhuqling/5875021 to your computer and use it in GitHub Desktop.
Save zhuqling/5875021 to your computer and use it in GitHub Desktop.
WCF 基础

WCF 基础

地址

地址 = Protocal :// MachineName/DomainName/IP [:Port]

  • TCP. net.tcp://localhost:8002/MySrv, net.tcp://localhost:8002/MySrv2
  • HTTP. http://localhost:8001 默认80端口
  • IPC. net.pipe://localhost/MyPip 一台机器只能打开一个命名管理
  • MSMQ. net.msmq://localhost/private/MySrv
  • Azure AppFabric sb://MyNamespace.servicebus.window.net/

契约

  • 服务契约 Service Contract

    • ServiceContract 可用于类/接口
    • OperationContract 只能用于方法上
      • Name 名称
  • 数据契约 Data Contract

    • DataMember/DataEnum
  • 错误契约 Fault Contract

  • 消息契约 Message Contract 不推荐

托管

  • IIS 5/6
  • 自托管 self-hosting

`

ServiceHost host = new ServiceHost(typeof(MySvc));

// 或者增加基地址 Uri tcpBaseAddress = new Uri("net.tcp://localhost:8001/"); Uri httpBaseAddress = new Uri("http://localhost:8002/"); ServiceHost host = new ServiceHost(typeof(MySvc), tcpBaseAddress, httpBaseAddress);

host.Open(); ... host.Close();

`

<pre> <system.serviceModel> <services name="MyNamespace.MySrv"> <host> <baseAddresses> <add baseAddress = "net.tcp://localhost:8001/" /> <add baseAddress = "http://localhost:8002/" /> </baseAddresses> <timeouts closeTimeout = "00:00:20"/> // 关闭超时 </host> </services> </system.serviceModel> </pre>

  • WAS 托管, IIS7使用WAS托管
  • Windows Server AppFabric
    • 是WAS的扩展,目的是对WCF/WF实行监控、显示、事件跟踪

绑定 Binding

  • 基本绑定 BasicHttpBinding,类ASMX
  • TCP NetTcpBinding,支持可靠性、事务性、安全性,必须使用WCF
  • IPC NetNamedPipeBinding,性能最佳,只能为同一机器
  • Web服务(WS) WSHttpBinding 基于http/https,支持可靠性、事务性、安全性(WS*标准)
  • MSMQ NetMsmqBinding
  • Web WebHttpBinding 不支持WCF,支持原始HTTP协议,支持REST/POX/JSON组合模式

终结点 EndPoint

EndPoint = Address + Contract + Binding

`

// 允许一个服务公开多个终结点
// TCP协议时允许使用相同基地址(端口相同),服务名称不同

// 绑定配置 ... 可以共用TrasationTCP绑定配置 // 按绑定分组 // 新增的绑定 // 默认绑定(仅一个),适用于所有netTcpBinding

`

// Programming add endpoint // 当地址为空字符串时,使用基地址 // 也可使用相对地址 MySvr host.AddServiceEndpoint(typeof(IMyContract), wsBinding, "http://localhost:8000/MySvr"); host.Open();

Binding tcpBinding = new NetTcpBinding(); // 也可传入配置内的名称,基于配置基础进行修改 tcpBinding.TransactionFlow = true; host.AddServiceEndpoint(typeof(), tcpBinding, "net.tcp://");

协议映射

<pre> <system.serviceModel> <protocalMapping> <add scheme = "http" binding = "wsHttpBinding" bindingConfiguration="..." /> </protocalMapping> </system.serviceModel> </pre>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment