Wednesday, May 16, 2012

WCF-Message Contract Example


// Interface

    [ServiceContract]
    public interface IMessageContract
    {
        [OperationContract]
        FundDetail getFunds();       
    }


// Classes

    [MessageContract]
    [KnownType(typeof(Pricedetail))] 
    public class FundDetail
    {
        private int _FundID;
        private string _FundName;

       [MessageHeader]
        public int FundID_Master
        {
            get { return _FundID; }
            set { _FundID = value; }
        }
        [MessageHeader]
        public string FundName
        {
            get { return _FundName; }
            set { _FundName = value; }
        }
       
        [MessageHeader]
        public Pricedetail[] records;
    
    }

    [DataContract]
    public class Pricedetail
    {
        private int _PriceID;
        private decimal _Offer;


        [DataMember(Name = "PriceIDNo", Order = 1)]
        public int PriceID
        {
            get { return _PriceID; }
            set { _PriceID = value; }
        }

       [DataMember(Name = "OfferPrice", Order = 2)]
        public decimal Offer
        {
            get { return _Offer; }
            set { _Offer = value; }
        }

    }
Service
public class MessageContract : IMessageContract
    {
       
         SqlConnection Conn;
         public MessageContract()
        {
            Conn = new SqlConnection("Connection String");
        }
        public FundDetail getFunds()
        {
            List<Pricedetail> fundPriceList = new List<Pricedetail>();
            Pricedetail dc = new Pricedetail();
            Conn.Open();
            SqlCommand Cmd = new SqlCommand("Select top 3 PriceID,offer from tblPrice where FundID=" + 7 + " order by Pricedate desc ", Conn);
            SqlDataReader dr = Cmd.ExecuteReader();

            while (dr.Read())
            {
                dc.PriceID = Convert.ToInt32(dr["PriceID"].ToString());
                dc.Offer = Convert.ToDecimal(dr["offer"].ToString());
               fundPriceList.Add(dc);
            }
            dr.Close();

            FundDetail _objFund = new FundDetail();
            _objFund.FundID_Master = 7;
            _objFund.FundName = "US Global Fund";
            _objFund.records = fundPriceList.ToArray();

            return _objFund;  
        }
    }

WCFTESTCLIENT OutPut:


XML :  In this case, the entire array is serialized as one element that is, one header with multiple child elements.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:FundID_Master xmlns:h="http://tempuri.org/">7</h:FundID_Master>
    <h:FundName xmlns:h="http://tempuri.org/">US Global Fund</h:FundName>
     
    <h:Pricedetails xmlns:h="http://www.Example.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
     
     <Pricedetail xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">
        <OfferPrice>2.486</OfferPrice>
        <PriceIDNo>20980</PriceIDNo>
      </Pricedetail>
     
     <Pricedetail xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">
        <OfferPrice>2.486</OfferPrice>
        <PriceIDNo>20980</PriceIDNo>
      </Pricedetail>
     
      <Pricedetail xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">
        <OfferPrice>2.486</OfferPrice>
        <PriceIDNo>20980</PriceIDNo>
      </Pricedetail>

    </h:Pricedetails>

  </s:Header>

  <s:Body>
    <FundDetail xmlns="http://tempuri.org/" />
  </s:Body>
</s:Envelope>
Next  Step  :  Change  Funddetail  Class Member
From  =  [MessageHeader(Name = "Pricedetails", Namespace = "http://www.Example.mu")]
                 public Pricedetail[] records;

To     = [MessageBodyMember(Name = "Pricedetails", Namespace = "http://www.Example.mu")]
             public Pricedetail[] records;

Run your service in WCFTESTCLIENT  and get XML
XML :  you can see below Body part separated from header part
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Header>
    <h:FundID_Master xmlns:h="http://tempuri.org/">7</h:FundID_Master>
    <h:FundName xmlns:h="http://tempuri.org/">US Global Fund</h:FundName>
  </s:Header>

  <s:Body>
    <FundDetail xmlns="http://tempuri.org/">
      <Pricedetails xmlns="http://www.Example.com"  xmlns:a="http://schemas.datacontract.org/2004/07/MessageContractDB"  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Pricedetail>
          <a:OfferPrice>2.486</a:OfferPrice>
          <a:PriceIDNo>20980</a:PriceIDNo>
        </a:Pricedetail>
        <a:Pricedetail>
          <a:OfferPrice>2.486</a:OfferPrice>
          <a:PriceIDNo>20980</a:PriceIDNo>
        </a:Pricedetail>
        <a:Pricedetail>
          <a:OfferPrice>2.486</a:OfferPrice>
          <a:PriceIDNo>20980</a:PriceIDNo>
        </a:Pricedetail>
      </Pricedetails>
    </FundDetail>
  </s:Body>
</s:Envelope>


Next  Step  :  Change  Funddetail  Class Member
From  =    [MessageBodyMember(Name = "Pricedetails", Namespace = "http://www.Example.mu")]
               public Pricedetail[] records;
    
TO   =  [MessageHeaderArray(Name = "Pricedetails", Namespace = "http://www.Example.mu")]
         public Pricedetail[] records;

OutPut :


Run your service in WCFTESTCLIENT  and get XML  each array element is serialized independently and so that each array element has one header.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:FundID_Master xmlns:h="http://tempuri.org/">7</h:FundID_Master>
    <h:FundName xmlns:h="http://tempuri.org/">US Global Fund</h:FundName>
   
   <h:Pricedetails xmlns:h="http://www.Example.mu" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <OfferPrice xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">2.486</OfferPrice>
      <PriceIDNo xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">20980</PriceIDNo>
    </h:Pricedetails>
  
    <h:Pricedetails xmlns:h="http://www.Example.mu" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <OfferPrice xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">2.486</OfferPrice>
      <PriceIDNo xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">20980</PriceIDNo>
    </h:Pricedetails>
   
    <h:Pricedetails xmlns:h="http://www.Example.mu" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <OfferPrice xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">2.486</OfferPrice>
      <PriceIDNo xmlns="http://schemas.datacontract.org/2004/07/MessageContractDB">20980</PriceIDNo>
    </h:Pricedetails>
  </s:Header>
  <s:Body>
    <FundDetail xmlns="http://tempuri.org/" />
  </s:Body>
</s:Envelope>


No comments :

Post a Comment