Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Distributed Applications / February 2007

Tip: Looking for answers? Try searching our database.

Visual Studio WebMethod with specific SOAP format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ernesto García García - 21 Feb 2007 17:03 GMT
Hi experts,

I need a Web Service with a specific SOAP format. Its SOAP request and
response parameters must be XML elements with no nesting, for example:

<soap:Body>
   <MyMethod xmlns="http://tempuri.org/">
      <input1>input1</input1>
      <input2>input2</input2>
   </MyMethod>
</soap:Body>

However, using Visual Studio 2005, I cannot see a way to build a Web
Service like that. I put for instance:

    [WebMethod]
    public Object MyMethod(Object input1, Object input2) {
        Object output = new Object();
        output.my_string = "Hello World";
        return output;
    }

    public class Object
    {
        public string my_string;
    }

But this builds a Web Service with nested elements inside the request:
  <soap:Body>
    <MyMethod xmlns="http://tempuri.org/">
      <input1>
        <my_string>string</my_string>
      </input1>
      <input2>
        <my_string>string</my_string>
      </input2>
    </MyMethod>
  </soap:Body>

and inside the response:

  <soap:Body>
    <MyMethodResponse xmlns="http://tempuri.org/">
      <MyMethodResult>
        <my_string>string</my_string>
      </MyMethodResult>
    </MyMethodResponse>
  </soap:Body>

Do you know how can I get this done with Visual Studio?

Thank you in advance and regards,
Tito
RYoung - 27 Feb 2007 23:32 GMT
You might have better luck if you use the XSD schema designer to design the
structure of the message that leaves the service. Then use xsd.exe to
generate the class file for that schema, which you can then use as objects
in your web service code.

For example: the MyMethod response would look like this in schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="MyMethodResponse">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="input1" type="xs:string" />
       <xs:element name="input2" type="xs:string" />
     </xs:sequence>
   </xs:complexType>
 </xs:element>
</xs:schema>

All I did there was use the schema designer in Visual Studio, dragged and
dropped an element on it, added "input1" and "input2" to the element, and
left their default types as strings.

You can get as complex as you need to do with the "message" design. For
example, say MyMethod had "input1" as a part, and another part was
"Customer"

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="MyMethodResponse">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="input1" type="xs:string" />
       <xs:element name="input2" type="xs:string" />
       <xs:element name="Customer" type="CustomerType" />
     </xs:sequence>
   </xs:complexType>
 </xs:element>
 <xs:complexType name="CustomerType">
   <xs:sequence>
     <xs:element name="fname" type="xs:string" />
     <xs:element name="lname" type="xs:string" />
   </xs:sequence>
 </xs:complexType>
</xs:schema>

Again, just using the designer. This time since MyMethodResponse contains a
part named "Customer" that is of the "CustomerType", I defined the
CustomerType complexType.

Then I just copied and pasted the xml here for you to see.

Anyways, save that file as "Messages.xsd", then fire up the .NET command
prompt and run:

xsd Messages.xsd /classes

The output will be a file named Messages.cs with serializable class
definitions. Something like this:

[Serializable]
class MyMethodResponse
{
      public string input1;
      public string input2;
}

Then in your webservice:

[WebMethod]
public MyMethodResponse MyMethod(string x, string y)
{
   MyMethodResponse response = new MyMethodResponse();
   response.input1 = "hello";
   response.input2 = "world";
   return response;
}

I would suggest defining the request/response messages in schema. This will
prevent the use platform specific types, like Object, and will enforce that
your messages conform to standard schema defined types. You'll have more
control over the shape of the incoming messsage, and outgoing message.

Ron

> Hi experts,
>
[quoted text clipped - 49 lines]
> Thank you in advance and regards,
> Tito

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.