How to access a web service? (Have created a web reference but don't know how to use it)


  • I am trying to access a web service that I created with VS2010 and .Net Framework 3.5.  I've started my client/front-end development by choosing the Silverlight Application template, and then adding a web reference to my .Web section.  At this point, I am stumped with how to proceed, since I am seeing error messages when I try to access the webservice via a using statement.

    My Web References entry is called WebService2, and I am currently using the default namespace of SilverlightApplication1 right now.  Any hints or suggestions welcomed at this point.



    p.s.  I was able to write a program using the Console Application template, with the Web Reference set up to use WebService2 and it worked ok.
    • تم التحرير بواسطة Bob Myers 14/محرم/1434 11:31 م
    14/محرم/1434 11:20 م


  • I found out what my problem was -- I was adding in the service reference in the application.web portion of the design, instead of the application portion of the design.  Once I did this, I had access to my web service.  Thanks everyone for helping out!

    • تم وضع علامة كإجابة بواسطة Bob Myers 29/محرم/1434 07:31 م
    29/محرم/1434 07:31 م

جميع الردود

  • First Add the Service Reference in your Silverlight Project. 

    Upon adding reference you will get the proxy and against you service Method (Say, GetClient()) you will find GetClientAsync method and GetClientCompleted event. So what you need to do is - 

    Register a Handler for the GetClientCompleted. And call the Async method (GetClientAsync) . 

    Once the method call is completed your result will be available in Result property of the Args.

    Note: Since your Silverlight Hosting web is different then the Service Url you will have to add crossdomain policy file in the Service Hosting location.

    Dhananjay Tech Lead

    • تم الاقتراح كإجابة بواسطة Christian AmadoMVP 27/محرم/1434 06:33 م
    15/محرم/1434 10:12 ص
  • Thanks for the responce, but I don't quite understand how to do the steps that you're talking about.

    I've added in the Service reference, as you pointed out.  It's from this point that I don't quite follow, since I'm relatively new to Silverlight and C# programming.

    The following is in my Console Application program and it seems to work:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using ConsoleApplication3.WebService2;

    namespace ConsoleApplication3


       class Program

          static void Main(string[] args)


             Service1 webservice = new Service1();

             string srt = webservice.HelloWorld("Bob ");


             Console.WriteLine(webservice.anotherHelloWorld(4, 3));

             Console.WriteLine("Press enter to quit");





    Appreciate any help with making this more understandable by me.



    15/محرم/1434 03:44 م
  • Bob,

    Just for understanding. Your console application consumes the web service is synchronus fastion. meaning it will be waiting at the point of call until the response is received. Please note this is not the case with silverlight. It is Asynchronus in fashion meaning call will go from the client but will not wait for response instead completed event will be raised on responce to the client.

    Now good new is you don't need to write this complicated proxy which will support the asynchronuos communication. Add Service refernce by default generate async proxy at the Silverlight end.

    You just give some time in understanding the proxy. I know you are new to this, so follow the below steps.

    Consume WCF Service in Silverlight

    Right click the Silverlight application and click Add Service Reference... . Write the url of the wcf service you got above in the address box, click Go button. In the Namespace box, write WcfServiceReference and click OK. This should add a reference of the wcf service and also create ServiceReferences.ClientConfig file in the root of your Silverlight application.

    Next say your xaml is as below -

    <UserControl x:Class="DynamicDataGrid.TemplatedDG"
        d:DesignHeight="365" d:DesignWidth="528">
      <Grid x:Name="LayoutRoot" Background="White">
          <TextBlock Text="Search" Canvas.Left="10" Canvas.Top="12"></TextBlock>
          <TextBox x:Name="txtSearch" Width="75" Height="22" Canvas.Left="55" Canvas.Top="10"></TextBox>
          <Button Content="  Get Data from WCF Service  " Click="Button_Click" Canvas.Top="9" Canvas.Left="140"></Button>
          <TextBlock x:Name="lblWaiting" Canvas.Left="375" Canvas.Top="10" Text="Status: Idle"></TextBlock>
          <sdk:DataGrid x:Name="dataGrid1" Canvas.Left="10" Canvas.Top="35" Width="550" Height="300" IsReadOnly="True" ItemsSource="{Binding Mode=OneWay}" AutoGenerateColumns="True" >

    As you can see that we have specified Button_Click event handler that will fire when user clicks button after entering keyword in the textbox. Let us see the code for this event.

    private void Button_Click(object sender, RoutedEventArgs e)
                // call data web service            
                var proxy = new WcfServiceReference.Service1Client("BasicHttpBinding_IService1");
                proxy.GetCustomersCompleted += new EventHandler<SilverlightApplication.WcfServiceReference.GetCustomersCompletedEventArgs>(proxy_GetCustomerCompleted);
                // call simple web service            
                lblWaiting.Text = "Status: Waiting ...";
            void proxy_GetCustomerCompleted(object sender, WcfServiceReference.GetCustomersCompletedEventArgs e)
                System.Collections.ObjectModel.ObservableCollection<WcfServiceReference.Customer> list = e.Result;
                dataGrid1.ItemsSource = list;
                lblWaiting.Text = "Status: Done";
                if (list.Count.Equals(0))
                    lblWaiting.Text = "No records found.";
                    lblWaiting.Text = list.Count + " records found.";

    You can see that in the Button_Click event I have specified a variable named proxy and instantiating it by passing the endpointConfigurationName as parameter. endpointConfigurationName is nothing but the bindingConfiguration value of the ServiceReferences.ClientConfig file.

    Later on I have specified the event handler for GetCustomersCompleted and specified the parameter for GetCustomerAsync method as the keyword (textbox value).

    When the GetCustomersCompleted will finish, it will raise an event named proxy_GetCustomerCompleted. In this event, write the code like above. Build your Silverlight project and ensure that it is built successfully.

    Now, try to run your web application that is hosting Silverlight, you should see your browser something like above image. Try to enter some keyword and hit the button, you should expect the search results coming through Wcf service you just created and displaying in the DataGrid.

    For more detail see here.

    Dhananjay Tech Lead

    • تم وضع علامة كإجابة بواسطة Haixia_XieModerator 21/محرم/1434 09:41 ص
    • تم إلغاء علامة كإجابة بواسطة Haixia_XieModerator 22/محرم/1434 01:47 ص
    15/محرم/1434 07:56 م
  • Hi Bob Myers,

    When you add the service (in your Silverlight project) with the "Add Service Reference" dialog, click on the Advanced... button and then please uncheck the following option:

    Reuse types in referenced assemblies.


    Hope it helps.

    - Chris.

    15/محرم/1434 08:18 م
  • I tried following the steps, but I did not get the ServiceReferences.ClientConfig file generated and VS2010 did not find the type or namespace name 'WcfServiceReference' when I'm doing development with the Silverlight Application template. 

    Any help/hints/suggestions welcomed.


    16/محرم/1434 03:34 م
  • You can create a helper class that allow to connect with your service.

    This way you don't need clientconfig file.


    - Chris.

    16/محرم/1434 04:18 م
  • Since I am rather new to C# coding and trying to do this, I do not know how to register a handler and call an async method.

    If someone would not mind explaining this more to me, I would appreciate it.  I can't seem to get access to calling the service inside of my code at this point.

    thank you

    21/محرم/1434 09:28 م
  • Chris;

    sorry -- I don't know what a helper class is.  Can you give me an example?


    21/محرم/1434 09:29 م
  • Hi,

    before adding web reference have you checked that your web service is up ? Have you hosted it on IIS ?

    One good question is equivalent to ten best answers.

    24/محرم/1434 04:18 م
  • I found out what my problem was -- I was adding in the service reference in the application.web portion of the design, instead of the application portion of the design.  Once I did this, I had access to my web service.  Thanks everyone for helping out!

    • تم وضع علامة كإجابة بواسطة Bob Myers 29/محرم/1434 07:31 م
    29/محرم/1434 07:31 م