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 / ASP.NET / Web Services / December 2004

Tip: Looking for answers? Try searching our database.

My First Web Service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Harris - 04 Dec 2004 13:30 GMT
Hi All,

I am just creating my first web service, which will provide a phone book
lookup to one of our partner organisations.

So far, using VS.Net, I have managed to produce a service which has one
method:

<WebMethod()> _
Public Function ContactSearch(ByVal Surname As String) As String
Return "a surname"
End Function

This works Ok, and returns the following:
<?xml version="1.0" encoding="utf-8" ?>
 <string
xmlns="http://tempuri.org/myfirstwebservice/Service1">harris</string>

I would like to extend this to include complete contact details, so the
resulting XML is something like:

<?xml version="1.0"?>
   <results>
       <contact id="1">
           <firstname>Fred</firstname>
           <surname>Smith</surname>
       </contact>
       <contact id="2">
           <firstname>Jane</firstname>
           <surname>Smith</surname>
       </contact>
   </results>
</xml>

I guess my question is, how do I progmatically add the elements and
attributes?

I Googled for about an hour trying to find an example/tutorial, all the
sites I found seemed to have no more than the helloworld example.

Any advice will be much appreciated.

Regards,
Simon.
Drew Marsh - 04 Dec 2004 14:52 GMT
> I guess my question is, how do I progmatically add the elements and
> attributes?
>
> I Googled for about an hour trying to find an example/tutorial, all
> the sites I found seemed to have no more than the helloworld example.

Considering what you'd like to return, I'd recommend the "simple" approach
of writing a custom .NET class marked up with the necessary Xml Serialization
attributes and returning that from your web method. Here's what your Contact
class might look like:

<codeSnippet language="C#">
[XmlType("Contact", Namespace="uri:marsh-samples-contactSearch")]
[XmlRoot("contact")]
public sealed class Contact
{
 #region Fields

 private int id;
 private string firstName;
 private string surname;

 #endregion

 #region Type specific properties

 [XmlAttribute("id")]
 public int Id
 {
   get
   {
     return this.id;
   }

   set
   {
     this.id = value;
   }                 
 }

 [XmlElement("firstname")]
 public string FirstName
 {
   get
   {
     return this.firstName;
   }

   set
   {
     this.firstName = value;
   }
 }

 [XmlElement("surname")]
 public string Surname
 {
   get
   {
     return this.surname;
   }

   set
   {
     this.surname = value;
   }
 }

 #endregion
}
</codeSnippet>

And then your web method would change to look something like this:

<codeSnippet language="C#">
[WebMethod]
[SoapDocumentMethod(ParameterStyle=SoapParameterStyle.Bare, Use=SoapBindingUse.Literal)]
[return:XmlArray("results", Namespace="xmlns="uri:marsh-samples-contactSearch")]
[return:XmlArrayItem("contact", typeof(Contact), Namespace="xmlns="uri:marsh-samples-contactSearch")]
public Contact[] SearchContacts([XmlElement("surname", Namespace="xmlns="uri:marsh-samples-contactSearch")]
string surname)
{
 // NOTE: in the real world you'd probably be looping through a DataReader
 // or something to build these results
 Contact sampleResult1 = new Contact();
 sampleResult1.Id = 131;
 sampleResult1.FirstName = "Drew";
 sampleResult1.Surname = "Marsh";

 Contact sampleResult2 = new Contact();
 sampleResult2.Id = 123;
 sampleResult2.FirstName = "Test";
 sampleResult2.Surname = "Marsh";
       
 return new Contact[] { sampleResult1, sampleResult2 };
}
</codeSnippet>

If you use this exact code, you'll end up with a SOAP request body that looks
like this:

<surname xmlns="uri:marsh-samples-contactSearch">Marsh</surname>

And a SOAP response body that looks exactly how you described:

<results xmlns="uri:marsh-samples-contactSearch">
 <contact id="131">
   <firstname>Drew</firstname>
   <surname>Marsh</surname>
 </contact>
 <contact id="123">
   <firstname>Test</firstname>
   <surname>Marsh</surname>
 </contact>
</results>

HTH,
Dre
Simon Harris - 05 Dec 2004 19:33 GMT
Thank You Drew for your reply.

Has anyone got a VB example to hand?

>> I guess my question is, how do I progmatically add the elements and
>> attributes?
[quoted text clipped - 116 lines]
> HTH,
> Drew
Drew Marsh - 05 Dec 2004 19:55 GMT
> Thank You Drew for your reply.
>
> Has anyone got a VB example to hand?

There's nothing C# specific about those examples. It's all about using the
attributes and VB is perfectly capable of doing the same thing. In C# attributes
are declared between the []s. So if you wanted to translate the code to VB
just change those to <>s instead. If you really can't translate from the
C# syntax I'll do it for you, but it's really not that hard.

Let me know,
Dre

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.