Server Intellect

How to Create a Web Service in C#

Category: XML and Web Services

How to Create a Web Service in C#

Introduction
In this article we will explain how you can easily create a web service within Visual Studio 2005.
Web Services are utilized in the design of SOA architecture as part of the foundation for the enterprise development of that architecture. SOA means Service Oriented Architecture.
In simplistic terms and using only one example of what web services are utilized for we are designing a layer between the front end [application] and the back end [database] that can be public facing and allow users with internet/network access to retrieve information from a database.
I mention internet and network in the sense that you can have a public facing web service that can be accessed via the internet and part of your web site for example. The Network mention refers to many corporations that have web services within their intranet that will return record sets of data or documents needed to maintain their daily operation.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


There are many facets to web services and the many ways they can benefit a corporation. But before we can do any of that we have to learn how to create a web service, and that is what we will learn in this article.

What we will learn in this article:

  • How to Create a Web Service

Getting Started
We will take you through the process of creating your first Web Service using Microsoft Visual Studio 2005 and C#. This will be a Web Service that we can consume in the future and we will during our next series of web service articles called “How to Consume a Web Service”. In that article we will show you how we can consume that Web Service utilizing .Net Web Service Studio and Microsoft Visual Studio.

Source Code for the Web Service
Start Visual Studio 2005 and create new Web Service project as illustrated in Fig. 1:

Fig. 1:

1
[Click to see full-size]


When you select the “OK” button the project will be created and the following code will be inserted into the file called “Service1.asmx.cs”. This is the main entry point for the web service.

 

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}

 

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

This example code contains a “Public” method called “HelloWorld()” that returns the string “Hello World”. For simplicity purposes we will use this web service as is to show that you can create a web service and access it with ease. In future articles we can delve into the realm of modifying the web service and the nuances of doing so.
Next we select the “Start Application” button in “Debug” mode so we can run this web service and see how we can easily interact with it with no lengthy processes or third party applications required.

Fig. 2:

2

 

When the application is starting you will notice that it is initializing the “ASP.NET Development Server” and utilizing the local host service and assigning it a port. Fig. 3

Fig. 3

3


Once the web service has compiled and started you will be presented with the following Web Service screen.

 

Fig. 4:

4
[Click to see full-size]

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

This screen shows you the available public methods that are available within this web service. You will notice that the only method available in this web service is the “HelloWorld” method. This is because the method was declared as “Public” and not “Private”. If we had declared this method “Private” this same screen would be displayed but there would be no methods available for us to use externally.
If you click on the “HelloWorld” method you will be presented with the following screen. Fig. 5

Fig. 5:

5
[Click to see full-size]

 

This screen allows us to invoke the “HelloWorld” method and see what it will return. You will notice that we are inside that method within the web service and that there are sample SOAP examples for a request and a response that can be utilized to help you consume this web service.
In the next article in this series we will also show you another way of creating these objects dynamically and with great ease, which will offer even more functionality.
When you click on the “Invoke” button you will be presented with the following screen. Fig. 6

Fig. 6:

6
[Click to see full-size]


This screen displays the results returned from the “HelloWorld” web service we just created.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

What we have Learned

We have learned that we can easily create a web service that can be used on a local intranet system or can be publicly accessed via the internet. We have learned how to initialize the web service and view the request and response objects associated with this web service. We also learned that when this web service is invoked it will return the string as designed.
Ideas for future enhancements:

 

  • Security
  • Encryption
  • Passing objects and parameters to the public method

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

Leave a Comment

Comments on this Article

Post a Comment
Name:
Website:
Email:
Comments:

#1 Kraig Pakulski

Posted By: Kraig Pakulski | 8.18.2008 at 6:20 PM

Thank you for this article.

I have started my first .Net program using C# and couln't find any practical references like this that will describe how to create a web service as part of a distributed application.

#2 Julien

Posted By: Julien | 4.15.2009 at 4:23 AM

What's this disturbing advertising in the middle of your article ??

#3 swazendo

Posted By: swazendo | 8.03.2009 at 8:20 PM

I can create a web service like the one shown above in C# and use it with ASP.NET Development Server5. There are several web examples to show this. How do I place the service in my web page (it doesn't support ASP.NET) and call into it (upload the .asmx file and bin/DLL maybe? That's where the tire hits the road. I've tried SOAP and java script to call into my site.

A REAL good example would show the whole thing in ONE example (create, deploy, and call) instead of many scatterd blobs of data all over the place....

#4 free games

Posted By: free games | 11.24.2009 at 10:57 PM

What's this disturbing advertising in the middle of your article ??

#5 Designer Eyewear

Posted By: Designer Eyewear | 12.28.2009 at 4:49 PM

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

#6 american dad season 5 episode 10

Posted By: american dad season 5 episode 10 | 1.08.2010 at 8:50 AM

I can create a web service like the one shown above in C# and use it with ASP.NET Development Server5. There are several web examples to show this. How do I place the service in my web page (it doesn't support ASP.NET) and call into it (upload the .asmx file and bin/DLL maybe?

#7 Movers Manhattan

Posted By: Movers Manhattan | 1.25.2010 at 4:01 AM

I can create a web service like the one shown above in C# and use it with ASP.NET Development Server5. There are several web examples to show this.

#8 Flyer Printing

Posted By: Flyer Printing | 1.29.2010 at 5:44 AM

I must say that this is a great post. I loved reading it. You have done a great job

#9 Botox New York City

Posted By: Botox New York City | 2.10.2010 at 1:53 PM

Your blog is very nice. I m really impressed .I m waiting for your next post. Hopefully I will get it soon.

#10 moda blogu

Posted By: moda blogu | 3.07.2010 at 3:44 AM

Could you pleasee provide more details on this topic?? By the way your blog is so nice. Sincerely..

#11 642-436

Posted By: 642-436 | 3.19.2010 at 4:27 AM

wow..I'm so glad to be the first to put on a comment here...

#12 jouer aux meilleurs casinos en ligne

Posted By: jouer aux meilleurs casinos en ligne | 4.10.2010 at 5:47 AM

Hi how to use database in a web service please give a sample code with the connection string and the database is msaccess very urgent give as soon as p ossible

#13 logo design

Posted By: logo design | 4.20.2010 at 1:07 AM

Thanks for the step by step tutorial for how to initialize the web service and view the request and response objects associated with this web service.

#14 Satellite Internet

Posted By: Satellite Internet | 4.26.2010 at 5:54 PM

I have not worked with C much before, but hopefully I will be able to after reading this. Do you have any recommendations for me that could help me along the way

#15 construction games

Posted By: construction games | 4.27.2010 at 3:22 AM

well, to Create a Web Service in C# is not difficult as I thought. After reading your post, I deeply impressed by useful and creative ideas.

#16 loan interest formula

Posted By: loan interest formula | 4.27.2010 at 3:27 AM

Ideas for future enhancements:

You know you gave me more creative info and I like reading all. Specially, All ideas for future enhancements you showed at the post: Security, Encryption and Passing objects and parameters to the public method.

#17 cara meninggikan badan

Posted By: cara meninggikan badan | 5.05.2010 at 10:06 PM

There are many facets to web services and the many ways they can benefit a corporation. But before we can do any of that we have to learn how to create a web service, and that is what we will learn in this article.

#18 sonnerie portable gratuit

Posted By: sonnerie portable gratuit | 5.13.2010 at 3:55 AM

Why didn’t I find this post earlier? Keep up the good work!

#19 tinggi badan

Posted By: tinggi badan | 5.19.2010 at 12:53 AM

Private” this same screen would be displayed but there would be no methods available for us to use externally.

If you click on the “HelloWorld” method you will be presented with the following screen.

#20 wholesale laptop adapter

Posted By: wholesale laptop adapter | 5.21.2010 at 1:55 AM

e design of SOA architecture as part of the foundation for the enterprise development of that architecture. SOA means Service Oriented Architecture.

In simplistic terms and using only one example of

#21 70-272 dumps

Posted By: 70-272 dumps | 5.22.2010 at 8:06 AM

I like your blog very much.

#22 HP0-P13 dumps

Posted By: HP0-P13 dumps | 5.22.2010 at 8:07 AM

Thanks for sharing.

#23 640-801 dumps

Posted By: 640-801 dumps | 5.22.2010 at 8:07 AM

Thanks for nice sharing.

#24 air max 2009

Posted By: air max 2009 | 5.23.2010 at 5:07 AM

nike air max sale

#25 wmns air max

Posted By: wmns air max | 5.23.2010 at 5:08 AM

nike air max sale

#26 air max running shoe

Posted By: air max running shoe | 5.23.2010 at 5:11 AM

nike air max sale air max running shoes

#27 air max running shoe

Posted By: air max running shoe | 5.23.2010 at 5:11 AM

nike air max sale air max running shoes

#28 wholesale shoes

Posted By: wholesale shoes | 5.24.2010 at 3:36 AM

Spring summer pocket perfect supporting role (figure) except heart shape brooch, earring, necklace,

#29 Los Angeles Website Design

Posted By: Los Angeles Website Design | 5.26.2010 at 2:18 PM

I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

#30 dazzling

Posted By: dazzling | 6.03.2010 at 7:54 PM

Hi this is good article well done,i have been searching on this thanks.

#31 vibram five fingers

Posted By: vibram five fingers | 6.05.2010 at 9:43 AM

That great!Great article!useful That what i'm looking for!

#32 air max

Posted By: air max | 6.05.2010 at 9:45 AM

This article give me a great help!thank you very much!

#33 Trailers for Sale

Posted By: Trailers for Sale | 6.07.2010 at 3:54 AM

I really like the tips you have given. Thanks a lot for sharing. Will be referring a lot of friends about this. Keep blogging

#34 SEO UK

Posted By: SEO UK | 6.07.2010 at 1:53 PM

The Network mention refers to many corporations that have web services within their intranet that will return record sets of data or documents needed to maintain their daily operation.

#35 Guide to SEO

Posted By: Guide to SEO | 6.12.2010 at 11:18 PM

you pleasee provide more details on this topic?? By the way your blog is so nice. Sincerely..

#36 taller 4 idiots

Posted By: taller 4 idiots | 6.15.2010 at 12:26 AM

I prefer to read this kind of stuff. The quality of content is good. Thanks for taking the time to discuss this,would you mind updating your blog with more information?

#37 full truth about abs

Posted By: full truth about abs | 6.15.2010 at 12:27 AM

i realize one thing that the way you plot the matter in your post that is amazing i am appreciating that. Add me a feedback reader.

Keep it up.

#38 how to create web service

Posted By: how to create web service | 6.18.2010 at 7:55 AM

Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging

#39 cheap mbt shoes

Posted By: cheap mbt shoes | 6.18.2010 at 9:06 PM

good post!!thank you

#40 auto insurance quotes

Posted By: auto insurance quotes | 6.22.2010 at 9:23 AM

The Network mention refers to many corporations that have web services within their intranet that will return record sets of data or

documents

needed to maintain their daily operation.

#41 wireless router setups

Posted By: wireless router setups | 6.25.2010 at 9:52 AM

Very nice entry thank you.

#42 Agile Nishu Gupta

Posted By: Agile Nishu Gupta | 7.13.2010 at 6:02 AM

the over-the-network method calls are commonly implemented through the Simple Object Access Protocol (SOAP), an XML-based protocol describing how to mark up requests and responses so that they can be transferred via protocols such as HTTP. Using SOAP, applications represent and transmit data in a standardized XML-based format.

#43 kuber infotek gupta

Posted By: kuber infotek gupta | 7.13.2010 at 7:54 AM

XML Web services provide the ability for applications to communicate in a loosely coupled environment using predefined message exchanges built around standard protocols such as HTTP, XML, XSD, SOAP, and WSDL. Because the protocols and specifications are public and not platform-specific, XML Web services can be used to enable applications to communicate whether they reside on the same computer or not, even if they reside on different computing platforms or devices.

#44 TrainPro School

Posted By: TrainPro School | 7.14.2010 at 7:01 AM

the Name textbox is unavailable because specifying the location sets the project name instead. For example, Web applications and XML Web services are located on a Web server and derive their name from the virtual directory specified on that server.

#45 Free Slot Machine Games

Posted By: Free Slot Machine Games | 7.15.2010 at 7:12 AM

XML Web services provide the ability for applications to communicate in a loosely coupled environment using predefined message exchanges built around standard protocols such as HTTP, XML, XSD, SOAP, and WSDL. Because the protocols and specifications are public and not platform-specific, XML Web services can be used to enable applications to communicate whether they reside on the same computer or not, even if they reside on different computing platforms or devices.

#46 good

Posted By: good | 7.15.2010 at 9:33 PM

Wow!! really <a href="http://www.myvogue.org">Good</a>!!! I will be

back soon!!!

#47 good

Posted By: good | 7.15.2010 at 9:34 PM

[url=http://www.myvogue.org/]Chanel Vogue[/url]

#48 Free dating sites

Posted By: Free dating sites | 7.16.2010 at 10:03 AM

Good post. I am also going to write a blog post about this...

#49 how to make money online

Posted By: how to make money online | 7.16.2010 at 10:05 AM

Is it the same with 2006? Don't take any notice about the winers regarding the advertising I could not see any.

#50 tiffany co

Posted By: tiffany co | 7.21.2010 at 3:06 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#51 tiffany jewelry

Posted By: tiffany jewelry | 7.21.2010 at 3:07 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#52 tiffany jewellery

Posted By: tiffany jewellery | 7.21.2010 at 3:07 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#53 casino online

Posted By: casino online | 7.21.2010 at 8:02 AM

I wanted to create a web service in C#, but my company won’t allow be to install illegal products. So, I have to install C# express edition, and one thing led to another. C# express doesn’t provide me a WebService template.

#54 SARA

Posted By: SARA | 7.21.2010 at 4:07 PM

Protocols and specifications are public and not platform-specific, XML Web services can be used to enable applications to communicate whether they reside on the same computer or not, even if they reside on different computing platforms or devices.

#55 tiffany ring

Posted By: tiffany ring | 7.26.2010 at 7:04 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#56 tiffany bracelet

Posted By: tiffany bracelet | 7.26.2010 at 7:04 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#57 tiffany necklace

Posted By: tiffany necklace | 7.26.2010 at 7:04 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/