Tuesday, July 26, 2011

FxCop Rule CA2202 Do not dispose objects more than once

See the explanation in msdn:
http://msdn.microsoft.com/en-us/library/ms182334.aspx

Notice:

using( Stream stream = new FileStream(...))
{     
using(StreamWriter writer = new StreamWriter(stream))
  {         
  ....
  } 
}
this pattern the stream objects will be wrapped in the StreamWriter and will be disposed. In order that stream object will not be dispose more than once, we should write the following pattern:
Stream stream = null; 
try 
{     
 stream = new FileStream("file.txt", FileMode.OpenOrCreate);     
using(StreamWriter writer = new StreamWriter(stream))
  {        
      stream = null;         
      // Use the writer object...    
  }
 } 
finally 
{     
  if(stream != null)         
  stream.Dispose();
     // we dispose the stream object 
     //only it is not null 
}
We should remember this pattern.

Thursday, July 21, 2011

Linq Query Take and Skip Extension Methods

var query=(from...
where...
select new {...}).Take(3);

Select the Top 3 elements.

var query=(from...
where...
select new {...}).Skip(2).Take(3);

Select the elements from 3 to 5. (Skip first 2, then select 3)

Thursday, July 14, 2011

A WCF Binding Table (Reference, ZZ)

This is posted by Dan Rigsby, http://www.danrigsby.com/blog/index.php/2008/01/19/wcf-binding-comparision-list-and-supported-features-reference/


Name (Config Name) [Schemas]TransportMessage EncodingMessage VersionInteropSecuritySessionTx FlowDuplex
BasicHttpBinding
(basicHttpBinding)
[http, https]
HTTP/HTTPS

Text

SOAP 1.1

Basic Profile 1.1None, Transport, Message, MixedN
A binding that is suitable for communicating with WS-Basic Profile conformant Web services like ASP.NET Web services (ASMX)-based services.
WSHttpBinding
(wsHttpBinding,
webHttpBinding)
[http, https]
HTTP/HTTPSText, MTOM

SOAP 1.2, WS-A 1.0

WSNone, Transport,Message, MixedNone, Transport, Reliable SessionNo, Yes(WS-AT)N
A secure and interoperable binding that is suitable for non-duplex service contracts.
WS2007HttpBinding
(ws2007HttpBinding)
[http, https]
HTTP/HTTPSText, MTOMSOAP 1.2, WS-A 1.0WS-Security,WS-Trust,WS-SC, WS-SPNone, Transport,Message, MixedNone, Transport, Reliable SessionNo, Yes(WS-AT)N
WSDualHttpBinding
(wsDualHttpBinding)
[http]
HTTPText, MTOM

SOAP 1.2, WS-A 1.0

WSNone,MessageReliable SessionNo, Yes(WS-AT)Y
A secure and interoperable binding that is suitable for duplex service contracts or communication through SOAP intermediaries.
WSFederationHttpBinding
(wsFederationHttpBinding)
[http, https]
HTTP/HTTPSText, MTOM

SOAP 1.2, WS-A 1.0

WS-FederationNone,Message, MixedNone, Reliable SessionNo, Yes(WS-AT)N
A secure and interoperable binding that supports the WS-Federation protocol, enabling organizations that are in a federation to efficiently authenticate and authorize users.
WS2007FederationHttpBinding
(ws2007FederationHttpBinding)
[http, https]
HTTP/HTTPSText, MTOM

SOAP 1.2, WS-A 1.0

WS-FederationNone,Message, MixedNone, Reliable SessionNo, Yes(WS-AT)N
A secure and interoperable binding that derives from WS2007HttpBinding and supports federated security.
NetTcpBinding
(netTcpBinding)
[net.tcp]
TCPBinary

SOAP 1.2

.NetNone,Transport, Message, MixedTransport, Reliable SessionNo, Yes(OleTx)Y
A secure and optimized binding suitable for cross-machine communication between WCF applications.
NetPeerTcpBinding
(netPeerTcpBinding)
[net.p2p]
P2PBinary

SOAP 1.2

PeerNone,Transport, Message, MixedY
A binding that enables secure, multi-machine communication.
NetNamedPipeBinding
(netNamedPipeBinding)
[net.pipe]
Named Pipe (IPC)Binary

SOAP 1.2

.NetNone,TransportNone,TransportNo, Yes(OleTx)Y
A secure, reliable, optimized binding that is suitable for on-machine communication between WCF applications.
NetMsmqBinding
(netMsmqBinding)
[net.msmq]
MSMQBinary

SOAP 1.2

.NetNone,Transport, Message, BothNo, Yes(OleTx)N
A queued binding that is suitable for cross-machine communication between WCF applications.
MsmqIntegrationBinding
(msmqIntegrationBinding)
MSMQ*MSMQNone,TransportNo, YesN
A binding that is suitable for cross-machine communication between a WCF application and existing MSMQ applications.

*Doesn’t use a WCF message encoding – instead it lets you choose a pre-WCF serialization format
Notes: Items in bold are the defaults for features that have multiple values. “–“ = No Support
Abbreviations: WS-SC = WS-SecureConversation, WS-SP = WS-SecurityPolicy, WS-A = WS-Addressing, WS-AT = WS-AtomicTransaction, OleTx = OleTransactions

FeatureDescription
NameThe name of the binding.
Config NameThe name of the binding used in configuration such as app.config or web.config.
SchemeThe supported Uri schemes.
TransportThe supported types of message transport (similar to Providers in Remoting).
Message EncodingThe supported types of message encoding.
Message VersionThe supported message versions.
InteropNames the protocol or technology with which the binding ensures interoperation.
SecuritySpecifies how the channel is secured:

  • None: The SOAP message is not secured and the client is not authenticated.
  • Transport: Security requirements are satisfied at the transport layer.
  • Message: Security requirements are satisfied at the message layer.
  • Mixed: This security mode is known as TransportWithMessageCredentials. It handles credentials at the message level, and integrity and confidentiality requirements are satisfied by the transport layer.
  • Both: Both message level and transport level security are used. This ability is unique to the NetMsmqBinding.
SessionSpecifies whether this binding supports session contracts.
Transaction FlowSpecifies whether transactions are enabled and the transaction flow type in ().
DuplexSpecifies whether duplex contracts are supported. Note this feature requires support for Sessions in the binding.