none
How to control data type of WCF message content? RRS feed

  • Question

  • First, the usual disclaimer that I am somewhat of a beginner with real-world web services, 
    so please forgive any obvious naivete.  

    I have been given a wsdl file for a web service and have run it through svcutil to get a class that effectively exposes a single solitary call at the bottom of the generated .cs file :

      public partial class sendServiceRequestRequest
      {
        [System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)]
        public object ServiceRequest;

        public sendServiceRequestRequest()
        {
        }

        public sendServiceRequestRequest(object ServiceRequest)
        {
            this.ServiceRequest = ServiceRequest;
        }
      }

      public string sendServiceRequest(object ServiceRequest)
      {
        sendServiceRequestRequest inValue = new sendServiceRequestRequest();
        inValue.ServiceRequest = ServiceRequest;
        sendServiceRequestResponse retVal = ((ServiceDomainWS)(this)).sendServiceRequest(inValue);
        return retVal.sendServiceRequestReturn;
      }

    The parameter ServiceRequest is defined as xsd:anytype in the wsdl and so gets defined as a simple object.  Actually, it is a long string of tagged data that looks something like the following (which is much simplified as I had to remove all sorts of potentially identifying information)

    <MyMsg xmlns:n="http://SomeNameSpace/this.org" xmlns:x="http://SomeNameSpace/that.org">
      <n:B>
        <n:b1>abc</b1>
        <n:b2>wert</b2>
        <n:D>
          <x:d1>qwer</x:d1>
        </n:D>
      </n:B>
      <n:C>
        <n:c1>xyz</c1>
        <n:c2>123</c2>
      </n:C>
    </n:MyMsg>

    They have given me an example of what the service expects, which is a big XML document with a root at sendServiceRequestRequest:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:reg="http://ServiceDomainWS.ws.svc">
      <soapenv:Body>
        <reg:sendServiceRequestRequest>
          <ServiceRequest>
            <MyMsg xmlns:n="http://SomeNameSpace/custom.org">
               < ......>
            </n:MyMsg>
          </ServiceRequest>
        </reg:sendServiceRequestRequest>
      </soapenv:Body>
    </soapenv:Envelope>


    However, when I create the ServiceRequest, I am basically just creating a big long string with the tagged data, which is what I send as the parameter.  When I look at it in SvcTraceViewer, I see this:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body>
        <sendServiceRequestRequest xmlns="http://ServiceDomainWS.ws.svc">
          <ServiceRequest i:type="a:string" 
                          xmlns="" 
                          xmlns:a="http://www.w3.org/2001/XMLSchema" 
                          xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <MyMsg xmlns:n="http://SomeNameSpace/custom.org">
               < ......>
            </n:MyMsg>
          </ServiceRequest>
        </sendServiceRequestRequest>
      </soapenv:Body>
    </soapenv:Envelope>    

    In terms of content, what I have created is exactly what they have specified, *except* for the i:type="string", which must be causing everything below it to be interpreted as a string.  So none of the tags are recognized for what they are supposed to be.  The receiving web service sends back a message that says basically that all my tags are invalid.

    So, my question is, how can I get that type specifier a:string out of my message and make the whole thing come across as a proper XML document?

    I am working with .Net 3.5, for what that is worth.  I can provide more detailed info, if it will help.

    Thanks.

    ------------------------------------------------------------------------

    Wednesday, February 6, 2013 7:17 PM

Answers

  • Hi,

    It seems that you are try to pass an object with unkown type, as far as I know, this is not possible in WCF because WCF requires a full compatibility with WSDL and WSDL requires transparent type definition, it will be loaded as a string and in WSDL it is defined as xs:anyType. So you need pass the type info as well in the method.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 12, 2013 12:13 PM
    Moderator

All replies

  • Hi,

    It seems that you are try to pass an object with unkown type, as far as I know, this is not possible in WCF because WCF requires a full compatibility with WSDL and WSDL requires transparent type definition, it will be loaded as a string and in WSDL it is defined as xs:anyType. So you need pass the type info as well in the method.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 12, 2013 12:13 PM
    Moderator
  • Thank you for reading and replying.

    Your answer does make sense.  The question is, how do I get that type information into the message? Or, how do I keep WCF from putting the "i:type=string" in the top level?  Nothing I try seems to get it done.  I try to cast things as object, I try modifying things in the declarations in the .cs file that svcutil generates from the .wsdl, etc, but keep hitting the same wall.  The receiving web service does not want text, it wants XML.

    In other words, how can I make my message body come across as

        <sendServiceRequestRequest xmlns="http://ServiceDomainWS.ws.svc">
          <ServiceRequest>
            <MyMsg xmlns:n="http://SomeNameSpace/custom.org">
              <n:DataABC>
                <n:Name>JOHN</n:Name>
                <n:Age>33</n:Age>
              </n:DateABC>
            <MyMsg>
          </ServiceRequest>

    instead of

        <sendServiceRequestRequest xmlns="http://ServiceDomainWS.ws.svc">
          <ServiceRequest i:type="a:string" xmlns="" 
                          xmlns:a="http://www.w3.org/2001/XMLSchema" 
                          xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <MyMsg xmlns:n="http://SomeNameSpace/custom.org">
              <n:PersonData>
                <n:Name>JOHN</n:Name>
                <n:Age>33</n:Age>
              </n:PersonData>
            <MyMsg>
          </ServiceRequest>

    Is there a way to bypass the serializer and just force the body to be an untyped string?  The message body needs to be a consistent XML document.

    Thank you,

    Ed

    Tuesday, February 12, 2013 3:28 PM
  • Hi,

    From my experience, you can just define the type as string and pass an XML which can be serialised using plain XML Serialization.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 13, 2013 9:31 AM
    Moderator
  • I have succeeded in getting past the issue of WCF inserting the type qualifier.

    When I run the wsdl file through svcutil and look at the generated code, I get a single function call with a single parameter of type "object".  This is consistent with the wsdl, where the parameter is defined a xsd:anytype.

    I went in and edited the generated file, changing "object" to "string", and now the type qualifier is no longer being added.

    I do not in any way understand this.  So much of WCF is happening behind the scenes and is out of direct control.  I think there will be other issues to be solved in this project, but I think I am past the immediate immediate problem

    Thank you for reading and offering your help, Haixia - your suggestion above got me thinking in the right direction.

    Ed

    Wednesday, February 13, 2013 9:53 PM
  • Hi,

    Thanks for your feedback.

    If you have any difficulty in future programming, we welcome you to post here again.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 14, 2013 5:03 AM
    Moderator