none
Passing XML Doc as a parameter in WCF RRS feed

  • Question

  • Hello, I have tried creating a WCF service to load/read any xml from client side. I am trying to pass the XML as a parameter to the Web service.  I need to load the xml file and when I navigate to GetOperation I would like to see the XML I loaded. This is the code I have so far. Please assist me on this. Thanks!

    service.cs

    public class service: Iservice

    {

    public string GetOperation(string paramater)        {

                HttpWebRequest request = null;
               request = (HttpWebRequest)
                WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                Encoding e = Encoding.GetEncoding("iso-8859-1");
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("D:\\xml.xml");
                string rawXml = doc.OuterXml;

        Stream requestStream = request.GetRequestStream();
                StreamWriter requestWriter = new StreamWriter(requestStream, e);
                requestWriter.Write(requestText);
                requestWriter.Close();}

    Iservice.cs

    [ServiceContract]
        public interface IService
        {
            [OperationContract]
            string GetOperation(string paramater);
        }


    Tuesday, April 29, 2014 4:47 PM

All replies

  • I may be missing something, but you shouldn't need all the request stream stuff to return a value from a web service; the infrastructure (WCF in this case) handles the request/response. So you can get away with implementing it as:

    public string GetOperation(string parameter)
    {
        var doc = new XmlDocument();
        doc.LoadXml("D:\\xml.xml");
        return doc.OuterXml;
    }
    

    Tuesday, April 29, 2014 6:16 PM
  • Thank you for your response. I have tried that but that is not returning the xml document that I am loading. I have modified IService to this 

     [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Xml,
           ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetOperation")]
            string GetOperation(string parameter);

    and this is what I am getting too

    The following is an example response Xml body:

    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string>

    Tuesday, April 29, 2014 6:28 PM
  • All those attributes aren't necessary for a basic operation to work, so I'm not sure what is going on. Your original service declaration defines the minimums required (ServiceContractAttribute and OperationContracts). The "String content" makes me think that your requestText variable is xml.ToString() rather than xml.OuterXml, but that's just a guess.

    Could you paste your system.serviceModel config section? Also, try just returning some basic lorem ipsum in your implementation method to see if the basic info xfer can work?

    Tuesday, April 29, 2014 6:55 PM
  • Here is my system.servcieModel

    <system.serviceModel>
        <services>
          <service name="TestService.Service" behaviorConfiguration="Default">
            <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="TestService.IService">
         </endpoint>
          </service>
        </services>

        <behaviors>
          <endpointBehaviors>
            <behavior name="webBehavior">
              <webHttp helpEnabled="true" />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="Default">
             
              <serviceMetadata httpGetEnabled="true" />
             
              
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    Tuesday, April 29, 2014 7:14 PM
  • So I simply it as follow but still not passing the xml

    public class Service : IService { public string GetOperation(string parameter) { var doc = new XmlDocument(); doc.LoadXml("D:\\xml.xml"); return doc.OuterXml; } } [ServiceContract] public interface IService { [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetOperation")] string GetOperation(string parameter); }


    Tuesday, April 29, 2014 8:06 PM