How to Consume a Web Service in C# [Part 2 of 2]
Part One of this Articles Series can be viewed here
Introduction
In this article we will explain how you can consume a web service utilizing “.Net Web Service Studio” version 2.0. This is a free download and was written by a person or people at Microsoft… at least to the best of my knowledge and based on the “About” screen provided. It is a great little utility to help expedite the consumption of a web service.
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
Consuming a web service can be done in many ways and with various efforts. .Net Web Service Studio takes the lengthy proxy creation process and automates a lot of the steps. It does most of the work for you and can create the web service proxy as well as the client code that calls the web service proxy. You can even use this application to debug your web service along with testing that web service before you start to write any code against that web service to insure that the web service works correctly and that all of the methods you are looking for exist within that web service.
Getting Caught Up
In part one of this series we left off after we had started/initialized the web service that we created. At this point you should know the URL to the web service and have it on the clip board ready to paste it into .Net Web Service Studio. We did this so we can access that web service and consume it within the application and create our proxy. We will pickup in this article at the point of consuming that web service and then creating the client code to call that web service.
Consumption of Web Service via .Net Web Service Studio
Start the .Net Web service Studio application, once started paste the WSDL Endpoint [URL from previous example] into the edit field as in the following screen shot.
Fig. 4:
[Click to see full-size]
Once the endpoint has been entered select the “Get” button to create the proxy and consume the web service. You will be sent to the messages tab and you will see the steps being taken to consume the web service. This is a very useful screen in the sense that it will allow you to debug an issue that might arise during the consumption of the web service.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
Fig. 5
[Click to see full-size]
Once this is completed select the “Invoke” tab to access the web service. This screen allows you to invoke the web service and retrieve the message being returned from our web service. You will notice that the web service name is displayed along with the public method we created within that web service. You will also notice that it has read the web service and knows that it is a value type of string being returned. Fig. 6
Fig. 6
[Click to see full-size]
Select the “Invoke” button on the “Invoke Tab” and you will see that in the output panel the words “Hello World” are now displayed.
Fig. 7
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Request and Response objects
If you select the “Request/Response” tab you will be presented with a screen where you will be able to cut and paste the request and response objects to use in your code base and send to clients as examples of how to use your new web service.
Fig. 8:
[Click to see full-size]
Creating the Proxy
If you select the “WSDLs & Proxy” tab you will be presented with the proxy that the application created based on the consumed web service. This proxy is everything you need to paste it into your application and immediately start accessing the web service.
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.1434
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
//
// Assembly WebServiceStudio Version = 2.0.50727.1434
//
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("WebServiceStudio", "0.0.0.0")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="Service1Soap", Namespace="http://tempuri.org/")]
public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol {
///
public Service1() {
this.Url = "http://localhost:14342/Service1.asmx";
}
///
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string HelloWorld() {
object[] results = this.Invoke("HelloWorld", new object[0]);
return ((string)(results[0]));
}
///
public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState);
}
///
public string EndHelloWorld(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((string)(results[0]));
}
}
Also on this tab is the “ClientCode” section. If you select this section you will be presented with the code needed to access this web service from your code base.
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:2.0.50727.1434
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
public class MainClass {
public static void Main() {
InvokeHelloWorld();
}
public static void InvokeHelloWorld() {
Service1 service1 = new Service1();
string helloWorldResult = service1.HelloWorld();
Dumper.Dump("result", helloWorldResult);
}
}
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
As you can see this handy utility gives you another way to consume web services and does a lot of the coding for you so you can concentrate on the more important aspects of your development.
Client Application
In order to test this web service we need to create a small application that will instantiate the proxy and make the web service call so we can visually see the results from within our code base. We have attached a small application that will show you how you can literally cut and paste the code created from within the .Net Web Service Studio application right into the Visual Studio front end application you are designing to utilize the web service.
Fig. 9
[Click to see full-size]
What we have learned:
We have learned a new way to consume a web service. We have learned that this utility will save you time and money in creating and debugging web service issues and that in a few easy steps you will be well on your way to publishing your web service.
Acknowledgements:
I would like to thank Matt Harrah for seeing a great need and allowing free downloads of the .Net Web Service Studio application from his Rocket Surgery blog. Please visit this site; in my search for this application I was very impressed with what I found. I would also like to thank the creator or creators of .Net Web Service Studio 2.0 for creating a great little utility that has saved me hours of work over the years.
Attachments
|