Windows Communication Foundation (WCF) Firestarter Note

WCF


SOA (Service Oriented Architecture)
-It is not a product. It is an architecture and design paradigm

Problem in Procedural Programming (application were developed by continuously calling functions that housed various areas of functionality)
-No reuse outside of application
-No design analogy to real-world made for cumbersome deverlopment process


Problem in Object Oriented Architecture (Applications were built by working with various that resembled real-world counterparts, housing both data and behavior)
-No reuse outside of application
-Required lots of plumbing for manaing ancillary functionality
-good in single application

Problem in Component Oriented Programming (Objects could now be encapsulated and managed by a common abstraction layer(COM) and housed in separate libraries (Dlls))
-Ancillary functionality typically required external service management
-good in with same binary assembly system, e.g.: Windows

Problem in Service Oriented Programming
-Requires a technology

**WCF is the technology for Service Oriented Programming


*************************************************************************
Service
` -Encapsulated units of responsiblity
-Atomic
-Durable (maintain state)
-Secure
-Always leave a system in a consistent state (transaction)

**Decouple System - base on area not only delete, update, add single operation


WCF (Start from 2006 with .Net 3.0)
**Message
WCF Breakdown
-System.ServiceModel
-Data Contracts
-Service Contracts
-Services
-Service Host
-Client Proxy
-Configuration


Service Contracts: Between Client and Service
-Interface decorated with [ServiceContract]
-Operations decorated with [OperationContract]
System.ServiceModel

Data Contracts
-Must be serializable using the new [DataContract]
System.Runtime.Serialization

*DONT NOT USE Business object as Data Contract sicne service may use in different applications
*Data Contract as limit as possible since need to pass via network

Service Classes
-Implementation of service contract

Service Hosting
-IIS : Can host only HTTP services
-Self-Hosting: Can be Any application
-WAS acn host nay protocal
-uses IIS for administration
-available in Vista and Server 2008, Windows 7

Configuring Your Service
<system.serviceModel>
<services>
<service name="{service type}">
{endpoint goes here}
</service>
<services>
</system.serviceModel>

<endpoint>
-Address: url
-Binding: transport mechanism
-Contract: service contract


Client Proxy (usually naming as "XXXClient")
-Proxy Class: ClientBase<ServiceInterface>,ServiceInterface
-Channel.XXXX(xxx) : delegate the call to channel


Configuration Your Client
<system.serviceModel>
<Client>
{endpoint goes here}
<Client>
</system.serviceModel>



*************************************************************************
Binding Configuration Options
-tweak of communication between client and service

Service Behaviors
-specific to service
-Instancing
-how a service gets instantiated and how long the instance lasts
-Concurrency
-how WCF will handle locking on service instances
-Transactions
-Security
************************************************************************
Binding Configuration Options
-set characteristics of transport
-set both the client and service
-set on the endpoint itself
-potential settings
-Session
-Message
-Timeout
-Security

<bindings>
<netTcpBinding>
<binding name="binding1" additional attributes>
<additonal tagas here/>
</binding>
<binding name="binding2" additional attributes>
<additonal tagas here/>
</binding>
</netTcpBinding>
</bindings>


"Reliablility"
-Can be used with NetTcpBinding or WsHttpBinding
-Enables communication retries (automatically retry)
-Also enforces message ordering
(e.g.: from the same client, make 4 continous calls, every message call start client to server, for whatever reason, the order is not guarantee)

e.g.:
<bindings>
<netTcpBinding>
<binding name="binding1">
<reliableSession enabled="true" ordered="true"/>
</binding>
</netTcpBinding>
</bindings>


"Message Size"
-Can be used with any binding
-Increase or decrease total message size
-Default is 64KB
-Useful when returning lists(or steams)


e.g.:
<bindings>
<netTcpBinding>
<binding name="binding1"
maxReceivedMessageSize="1000000">
</binding>
</netTcpBinding>
</bindings>


"Transmission Timeout"
-Can be used with any binding
-Set the maxmum time WCF will wait for a message to be transmitted
-Throws exception when exceeded


e.g.:
<bindings>
<netTcpBinding>
<binding name="binding1"
sendTimeout="00:01:00">
</binding>
</netTcpBinding>
</bindings>


Best Practices
-Always set reliability to true
-Increase message size to about one meg to account for lists
-Default timeouts are usually fine

Behavior
-Change the way service behavior
-Do not affect transport(bindding)
-Typically unknow to client
-Two types of setting(not always both)
-Configuration
-Attribute decoration


e.g.:
Configuration
<service name="MyService" behaviorConfiguration="behavior1">
..
</service>
<behaviors>
<serviceBehaviors>
<behavior name="behavior1">
<additional tags here/>
</behavior>
</serviceBehaviors>
</behaviors>


Attribute decoration
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class MyService:IMyService
{
}


Service Debbugging
-By default, service does not pass error details to client
<behaviors>
<serviceBehaviors>
<behavior name="behavior1">
<serviceDebug IncludeExceptionDetailsInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>

OR

[ServiceBehavior(IncludeExceptionDetailsInFaults=true)]
public class MyService:IMyService
{
}

Instancing
-Controls how a service is served up
-Three different modes
-Per-call: every call get differet instance of service, constructor called every time, not memory state
-Per-Session: (Default) every proxy gets a new instance, constructor called on first operation, same proxy, same instance
-Singleton: Once instance serves all proxies for all clients,constructor called on first call


Concurrency
-Controls how a service handles locking during multiple calls
-Three different modes
-Single: (Default)service instance allows only one caller in at a time, it is lock, lock, lock
-Multiple:Calls allowed on same instance(e.g.:make two calls in two threads).
You must perform manual Locking:
-lock(variable)
-lock(this)
-[MethodImpl(MethodImplOptions.Synchronized)]

-Reentrant: Variation of single concurrency mode, remembers a caller.
-allows caller back into service: while caller is away, other calls allowed in, but when caller comes back, may wait


(related to Instancing, e.g.: per-call, then very time is new instance, it does not be affect by concurrency is single or multiple)


Best Practices
-Set to include exception details during development
-Use Per-Call for all services
-Introduce Per-Session (state handling) or  Singleton for specific scenarios
-Use Single-Concurrency for all service
-Performance increase when threads exceed cores
-Remeber synchronization locking
-Use Reentrant for callback only
-Avoid circular service calling


More binding configuration
Transactions
-transactionFlow (on the service contract level)
-NotAllowed(default)
-Allowed
-Mandatory
-OperationBehavior(on the Service Operation level)
-(TransactionScopeRequired=true), commit will automatically if not exception
-(TransactionScopeRequired=false)



e.g.:
<bindings>
<netTcpBinding>
<binding name="binding1" 
transactionFlow="true">
</binding>
</netTcpBinding>
</bindings>


[ServiceContract]
public interface IMyContract
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void UpdateContact(int id, string name);
}


public class Myservice: IMyContract
{
[OperationBehavior(TransactionScopeRequired=true)]
public void UpdateContact(int id,string name)
{
}
}


Note:
WCF not transaction coordinator is transaction mangaer. there 3 transaction coordinator in your system
Lightweight transaction coordinator(LTC)
Kernel transaction coordinator(KTC)
Distributor transaction coordinator(DTC)
**make sure DTC service open when you need distriubtor transaction










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值