473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

My First Web Service

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.
Nov 23 '05 #1
3 2473
Simon Harris wrote:
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=SoapParameterSty le.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

Nov 23 '05 #2
Thank You Drew for your reply.

Has anyone got a VB example to hand?

"Drew Marsh" <dr****@hotmail.no.spamming.com> wrote in message
news:98*********************@msnews.microsoft.com. ..
Simon Harris wrote:
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=SoapParameterSty le.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,
Drew

Nov 23 '05 #3
> 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

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Liza | last post by:
Hello, can you guys help me out here....? this is part of my masters degree and hence is very important to me..... my supervisor doesn't seem to be too interested in helping me and infact is...
8
by: murphy | last post by:
I'm programming a site that displays info from AWS Commerce Service 4.0. At each change of the asp.net application the first load of a page that uses the web service takes 30 seconds. Subsequent...
11
by: Jason | last post by:
Hi I have a "problem" i have got a ASP.NET application. in this application i have included logging. in the logging i have logged how many seconds it takes for this application to fully load....
2
by: markoZ | last post by:
Hi. We have developed a .NET web service. The service executes a SQL on the SQL server. The first call of the webservice takes very long but the other calls dont. Why is that and how can we...
7
by: tshad | last post by:
I have a problem with a VS 2003 project. This project was designed and works fine in VS 2003. But trying to open the project I get the following error....
6
by: DaveOnSEN | last post by:
Every time I make any .NET framework based webservice consumer I have the same problem: The first call my application makes to consume a webservice takes about 15 seconds. Subsequent calls are...
0
by: Griff | last post by:
Overview When the first call to our Web Service causes an exception, the Web Service caches that user's credentials for its life time. Details We have a Web Service which uses Windows...
6
by: alho | last post by:
The web service is called by a program running on pocket pc. When to call the web service, the first call is still ok, but for the second or later calls, it will throw "403 Forbidden" WebException....
2
by: =?Utf-8?B?SmltIE93ZW4=?= | last post by:
Hi John, Hopefully this post will find its way back to you - or perhaps be answered by someone else. As I mentioned in my last post on the earlier portion of this thread, changing the...
0
by: Rod | last post by:
This is my first WCF service and it is running on my development machine (XP Pro SP2) under IIS, I am using wsHttpBinding. (I will eventually put it onto a Windows 2003 Server running under IIS on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.