Tuesday, August 2, 2011

Pass xml to WCF Rest Service

In Data Contract >>Activity is Custom Object Format

  [OperationContract ( Action = "ReadyMembershipAPI/CreateActivity" )]
        [FaultContract ( typeof ( ApplicationException ) )]
        [WebInvoke ( Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "CreateActivity?requestToken={requestToken}" )]
        Guid CreateActivity ( string requestToken, Activity activity );


In Service.svc

public Guid CreateActivity ( string requestToken, FIAActivity activity )
        {
            Guid activityID=Guid.Empty;
                  //Implement Your Code ;
                    return contactHandler.CreateActivity ( organisationName, activity.ContactID, activity.ActivityType,( activity.ActivityDate), activity.Label, activity.Description );

                     }

In Activity Object

    [DataContract ( Namespace = "http://schemas.datacontract.org/2004/07/TestAPI/" )]
    public class Activity
    {

        [DataMember ( Order = 1 )]
        public string ContactID { get; set; }

        [DataMember ( Order = 2 )]
        public string ActivityId { get; set; }

        [DataMember ( Order = 3 )]
        public string ActivityType { get; set; }

        [DataMember ( Order = 4 )]
        public DateTime ActivityDate { get; set; }

        [DataMember ( Order = 5 )]
        public string Label { get; set; }


        [DataMember ( Order = 6 )]
        public string Description { get; set; }




    }


Access The Service>>>>

Host Service As

http://localhost:35798/RestServiceImpl.svc/

Then Access 

             HttpWebRequest req = null;
             HttpWebResponse res = null;
              const string url = "http://localhost:35798/RestServiceImpl.svc       /CreateActivity?requestToken=tokenabc";

                req = ( HttpWebRequest ) WebRequest.Create ( url );
                req.Method = "POST";
                req.ContentType = "application/xml; charset=utf-8";
                req.Timeout = 30000;
                req.Headers.Add ( "SOAPAction", url );

                var xmlDoc = new XmlDocument { XmlResolver = null };
                xmlDoc.Load ( Server.MapPath ( "PostData.xml" ) );
                string sXml = xmlDoc.InnerXml;
                req.ContentLength = sXml.Length;
                var sw = new StreamWriter ( req.GetRequestStream ( ) );
                sw.Write ( sXml );
                sw.Close ( );

                res = ( HttpWebResponse ) req.GetResponse ( );
                Stream responseStream = res.GetResponseStream ( );
                var streamReader = new StreamReader ( responseStream );

                //Read the response into an xml document
                var soapResonseXmlDocument = new XmlDocument ( );
                soapResonseXmlDocument.LoadXml ( streamReader.ReadToEnd ( ) );

                //return only the xml representing the response details (inner request)
                TextBox1.Text = soapResonseXmlDocument.InnerXml;//Here Retun Guid as xml


PostData.xml >>>>
 
<Activity  xmlns="http://schemas.datacontract.org/2004/07/ReadyMembershipAPI/">
  <ContactID>1A7BC156-3FAD-E011-B15A-84CDF3EB186A</ContactID>
  <ActivityId>1A7BC156-3F56-E011-B15A-84CDF56B186A</ActivityId>
  <ActivityType>TEst00</ActivityType>
  <ActivityDate>2002-09-24</ActivityDate>
  <Label>Udith</Label>
  <Description>Udith</Description>
  </Activity>

  

Refer >>http://www.codeproject.com/KB/WCF/HTTP_POST_RestServiceAPI.aspx

No comments:

Post a Comment