locked
WebService with Silverlight 2.0 RRS feed

  • Question

  • Hi,

    I'm totally new to Silverlight and WebService. At the current state I'm trying to retrieve data from Database (MSAccess) into my Silverlight page. I have manage to create a WebService project and view it in the browser. However, I cannot make use of the WebService methods I created in my Silverlight Project.

    If anyone know the solution or reference, please let me know.

    Thanks

     

    Tuesday, March 11, 2008 11:21 AM

Answers

  • Can anyone help me with this error?
     

    1.)

      [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
     

     I'm not sure why you add [ToolboxItem(false)] or etc.

    I will tell you the steps.

    • Create SL project.  (eg: SL2Test )
    • VS will ask you whether you want to add web project or not. (so, the name of ASP.NET web project will be "SL2Test_Web")
    • The structure of your solution

      Solution SL2Test (2 projects)
      ----- C:\..\SL2Test_Web
      ------SL2Test

    • Add Web Service item to your ASP.NET project
    • Update the code as below.

      using System;
      using System.Collections;
      using System.Linq;
      using System.Web;
      using System.Web.Services;
      using System.Web.Services.Protocols;
      using System.Xml.Linq;
      using System.Data.SqlClient;
      using System.Data;
      using System.Configuration;
      using System.Text;
      /// <summary>
      /// Summary description for ProductManager
      /// </summary>
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
      [System.Web.Script.Services.ScriptService]
      public class WebService1 : System.Web.Services.WebService {

          public WebService1() {

              //Uncomment the following line if using designed components
              //InitializeComponent();
          }

          [WebMethod]
          public string MyMethod() {
               return "Testing";
           }
      }


    • Add your web service to your SL project using "Add Service Reference"
    • then, you can call the web service from SL  ( note that sync-mode is not currently available )
    Friday, March 14, 2008 9:22 AM

All replies

  •  I have one sample that uses Silverlight 2 beta1 and SQL 2005. You can easily change it from SQL to Ms Access. You can download it from this link <Silverlight 2 (beta1) - Database Operations with ASP.NET Web Service in Silverlight 2>

    Please take a look that sample. Let me know if you have problem in converting SQL to MsAccess.. All you need to do is that replacing System.Data.SqlClient with System.Data.OleDb

    Tuesday, March 11, 2008 11:37 AM
  • I have construct a new project in the same solution called WebService


    namespace WebService
    {
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class Service1 : System.Web.Services.WebService
        {
            public Service1()
            {
            }

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


    and in the Page.xaml.cs:

    using ...
    using WebService;


    namespace Airline
    {
        internal enum ActionType { Selecting, Moving, RotatingScaling };

        public partial class Page : UserControl
        {
            private Service1 s;
            ........
        }
        .......
    }


    When I compile the solution, I get the following error

    The type 'System.Web.Services.WebService' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 

    Can anyone help me with this error?

    Thanks

     

    Friday, March 14, 2008 7:47 AM
  • Can anyone help me with this error?
     

    1.)

      [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
     

     I'm not sure why you add [ToolboxItem(false)] or etc.

    I will tell you the steps.

    • Create SL project.  (eg: SL2Test )
    • VS will ask you whether you want to add web project or not. (so, the name of ASP.NET web project will be "SL2Test_Web")
    • The structure of your solution

      Solution SL2Test (2 projects)
      ----- C:\..\SL2Test_Web
      ------SL2Test

    • Add Web Service item to your ASP.NET project
    • Update the code as below.

      using System;
      using System.Collections;
      using System.Linq;
      using System.Web;
      using System.Web.Services;
      using System.Web.Services.Protocols;
      using System.Xml.Linq;
      using System.Data.SqlClient;
      using System.Data;
      using System.Configuration;
      using System.Text;
      /// <summary>
      /// Summary description for ProductManager
      /// </summary>
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
      [System.Web.Script.Services.ScriptService]
      public class WebService1 : System.Web.Services.WebService {

          public WebService1() {

              //Uncomment the following line if using designed components
              //InitializeComponent();
          }

          [WebMethod]
          public string MyMethod() {
               return "Testing";
           }
      }


    • Add your web service to your SL project using "Add Service Reference"
    • then, you can call the web service from SL  ( note that sync-mode is not currently available )
    Friday, March 14, 2008 9:22 AM
  • Thank you Michael Sync 

    I finally get it to work. The problem that I had before is that I created SL project in Microsoft Blended. Therefore, the look that I have is so different from the project in reference page. Now that I follow your steps, I finally able to use WebService.

    Best Regards,
    Teerut


    Friday, March 14, 2008 1:32 PM
  • Hi Teerut,

    Cool. I'm glad to hear that your problem has been solved. :)  

    Friday, March 14, 2008 1:53 PM