Thomas's profileBlogsBlogLists Tools Help

Blog


    11 September

    Connection to a web service (the simplest way):

    Accessing to an existing web service in .NET is as easy as it can be:

    Remember the previous generated web service that has the following members
    • int Count();
    • string Merge(string a, string b);

    Now let the service run in the background and create a new form application. Then, In the solution explorer you select "Add new web Reference..." from the context menu.

    In the dialog, type in the url adress of the running web service (in this example it's "http://localhost:1122/WebSite2" and then you'll see the directory list, where you select "service.asmx" and then you finally need to click the button "Add Reference" and a new file "localhost" is added to your form project in the Web References section on the solution explorer.

    Now you can simply access all methods of the web services as simple as accessing to properties of an object. See a simply example here:

     
        private void button1_Click(object sender, EventArgs e)
        {
            /// Create the web service client here:
            Service service = new Service();
     
            /// Call a method form the web service.
            /// Note that this method uses SOAP to post the params to the server and for receving the result.
            string s = service.Merge("This is a Test", "123");
            MessageBox.Show(s);
        }
     


    That' all!

    So within one minute, you can access all methods of an existing web service.

    By the way, the web reference  (in this example  named  localhost)  provides  2 method access  members for each method on the web service:

    • a synchrone acess that waits until the method has executed
    • a n asynchrone acess that starts the method but doesn't wait for it to be executed.
    So despite the service.Merge() method, there is also a service.MergeAsync() method. For receiving the result of a method, an event handler can be added for each method, which looks like this:



            
    	private void button1_Click(object sender, EventArgs e)
            {
                Service service = new Service();
     
                  /// Add an event that occures when the web service has finished to process the
                  /// Merge() method:
                service.MergeCompleted += new MergeCompletedEventHandler(service_MergeCompleted);
     
                 /// now executhe the Merge method asynchronously:
                service.MergeAsync("This is a Test", "123");
            }
     
             /// <summary>
            /// Raises, when the web service's Merge() method has completed:
             /// </summary>
            void service_MergeCompleted(object sender, MergeCompletedEventArgs e)
            {
                /// Display the result of the Merge() method specified in the EventArgs:
                MessageBox.Show(e.Result);
            }
     




    05 September

    Creating a Web Client to access a previousely created Web Service

    After creating our first web service, we are no prepared to access it's members using HTML Post functionality, which goes like this:

    private void button1_Click(object sender, EventArgs e)
    {
        WebClient client = new WebClient();
        client.UseDefaultCredentials = true;
        client.Encoding = System.Text.Encoding.UTF8;
        client.Headers.Add("Authorization", "Basic wqoieuoqwenand,asdn");
        NameValueCollection values = new NameValueCollection();
        values.Add("a", "VR");
        values.Add("b", "Developers!");
        byte[] result = client.UploadValues("http://localhost:1882/WebSite1/Service.asmx/Merge", values);
        string value = System.Text.Encoding.ASCII.GetString(result);
        textBox1.Text = value;
    }


    As you see, the two params for the Merge member are specified in a NameValueCollection class, while the HttpPost is done by calling WebClient.UploadValues which returns a byte[] value, that represents an xml string that looks like followed:

    <?xml version="1.0 encoding="utf-8"?><string xmlns="http://tempuri.rog/">Merged VR with Developpers!</string>

    Of course you'll have to to some additional stuff, but this is the simple way to access a web service in .NET.

    Note that the 1882 port may differ, so you might change it. Just click on the tool tray icon of the running web service to see it's current port number.

    Creating a Web Service in .NET

    Creating a simple web service within Visual Studio 2005 is done in a view minutes:

    1. From the File Menu select New->Web Site..
    2. Choose ASP.NET Web Service (ensure the language to be set to C# or your prefered language)
    3. Now you already have a Web Service with a HelloWorld member that can be accessed with Html Post or Soap to return the "Hello World" string.
    4. Now you want to create a member, having some params, called Merge, wich finally looks like this:


    [WebService(Namespace = "http://www.tomsblog.de/", Description = "For testing purposes only")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        private int counter = 0;
        public Service ()
        {
        }
     
        [WebMethod(Description="Returns a counter value as string")]
        public string Counter()
        {
            counter++ return counter.ToString();
        } 
        [WebMethod(Description="Merges to strings to one.")]
        public string Merge(string a, string b)
        {
            return a + " and " + b;
        }
    }



    The Merge member requires 2 string params.

    Now you know how to access the web service and how to create a web client comes with the next blog...