views:

23

answers:

2

Hello Experts,

Please can i use datacontracts in WCF for streaming instead of message Contract.

If yes, will it offer any performance improvement?

THanks

+1  A: 

Yes, you can... it is recommended if large amount of data to be transfered or streamed.

Check this http://blog.thekieners.com/2010/05/04/optimize-data-contracts-for-better-wcf-performance/

Sandy
+1  A: 

Answer depends on binding you use. For TCP or Named pipe transport protocols you can stream any data - including data contracts. If you want to use streaming over HTTP (supported by BasicHttpBinding) you have to some several constraints:

  • Streaming has to be allowed on binding
  • Operation has to work with Stream instance or Message contract which contains only single body element of type Stream. Data contracts are allowed only as custom message headers on Message contract.

Only valid operation definitions for streaming over HTTP are:

// StreamedResponse
[OperationContract]
Stream GetData(int id);

// StreamedRequest
[OperationContract]
int PostData(Stream data);

// Streamed
[OperationContract]
Stream WorkWithData(Stream data);

[OperationContract]
DoSomethingResponse DoSomething(DoSomethingReqest request);

[MessageContract]
public class DoSomethingRequest
{
  // Custom data and data contract allowed only as SOAP headers which are always buffered
  [MessageHeader]
  public MyDataContract CustomHeader { get; set; }

  // No other member allowed
  [MessageBodyMember]
  public Stream Data { get; set; }
}

[MessageContract]
public class DoSomethingResponse
{ ... }

If you don't follow these constraints you will end up with two cases:

  • Operation is not streamed even if you configure binding for streaming - this happens if you don't use Stream instance as message content
  • Exception - this happens if you use Stream instance + other data / data contract as message content
Ladislav Mrnka