BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in WSCF/WCF

 
 
Page 7725 of 20224

Accessing SOAP Headers created by WCF MessageContracts in ASMX clients.

Blogger : Kurt Claeys
All posts : All posts by Kurt Claeys
Category : WSCF/WCF
Blogged date : 2007 Oct 23

WCF and AMSX really have a good interopability. Of course ... both technologies are based on SOAP, so they should be.

Also SOAP headers are supported and the VS2005 asmx proxy generation (add webreference) supports this by adding the headers as properties of the service class. Very convenient. Once these properties are set at the service level, for every call to an WCF operation this headerdata is send to WCF service and is available during the operation. This can be used in the scenario where every method on the service needs access to information regarding the context of the client (the identification of the user for example), but you don't want to specify this in every OperationContract. In most of the cases this information will be static, so setting the properties once at the instantiation of the proxy class becomes in very handy.

On the WCF contract you have to specify a MessageContract instead of a DataContract. With a MessageContract you have more detailed control over how the fields of the contract get mapped to actual SOAPMessages, seperating header parts and body parts. Both parts can be DataContracts, so a MessageContract is a kind of wrapper around multiple DataContracts in which you specify which part is a header (and thus implicitly send to the service as a SOAPheader) and which part is the body (and must be given explicit as parameter to the methodcall).

Restriction : Only one parameter of the operation can by a MessageContract and when using a MessageContract as a parameter also the returntype has to be a MessageContract.

Defining a MessageContract with body and header :

[MessageContract] public class MyInputMessageType { [MessageHeader(Name="HeaderIn")] public HeaderIn HeaderIn; [MessageBodyMember] public OrderPayload Payload; } [DataContract] public class HeaderIn { [DataMember] public String ClientIdentification; } [DataContract] public class OrderPayload { [DataMember] public Int64 OrderID; [DataMember] public DateTime OrderDate; }

Definition of the OperationContract

[ServiceContract] interface IMyService { [OperationContract] MyReturnMessageType DoSomething(MyInputMessageType data); }

The implementation of the operation can access both parts

data.Payload.OrderDate.ToShortDateString() data.Payload.OrderID.ToString() data.HeaderIn.ClientIdentification.ToString()

The client sets header at the service level

ServiceReference.MyService service; service = new ServiceReference.MyService(); ServiceReference.HeaderIn header; header = new ServiceReference.HeaderIn(); header.ClientIdentification = "ITSME"; service.HeaderInValue = header; ServiceReference.OrderPayload orderPayload; orderPayload = new ServiceReference.OrderPayload(); orderPayload.OrderDate = System.DateTime.Now; orderPayload.OrderDateSpecified =true; orderPayload.OrderID = 1234; orderPayload.OrderIDSpecified = true; ServiceReference.OrderReturnData orderReturnData; orderReturnData = service.DoSomething(orderPayload); MessageBox.Show(orderReturnData.OrderID.ToString()); MessageBox.Show(service.HeaderOutValue.Status);


Read comments or post a reply to : Accessing SOAP Headers created by WCF MessageContracts in ASMX clients.
Page 7725 of 20224

Newest posts
 

    Email TopXML