Skip to main content

Microsoft Silverlight

Answered Question Web ServiceRSS Feed

(0)

Djangoo
Djangoo

Member

Member

34 points

34 Posts

Web Service

How to access the Web service Class after, I have in my SL 2.0 project, added references to Web services? 

This is the  Page.xaml.cs:

using SilverlightApplication4.ServiceReference1;

namespace SilverlightApplication4
{
    public partial class Page : UserControl
    {
        public Page ()
        {
            InitializeComponent ();
        }

        private void Button_MouseLeftButtonDown ( object sender, MouseButtonEventArgs e )
        {
             /* If this was aspx page I would do only this:

               WebService ws = new WebService();

               ws.HelloWorld ( "some args" );
        }
    }
}

This is the  WebService.cs:

/// <summary>
/// Summary description for WebService
/// </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 WebService : System.Web.Services.WebService
{

    public WebService ()
    {

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

    [WebMethod]
    public string HelloWorld ( string x)
    {
        return x;
    }

}

Jim Mangaly
Jim Mangaly

Contributor

Contributor

2610 points

380 Posts

Answered Question

Re: Web Service

Take a look at this post by Tim Heur. It has explanation + sample for accessing data via WCF, asmx and REST. Look at the asmx example since that is your scenario.

All service calls in Silverlight are asynchronous calls. So you will see in that sample that the asmx service call is "wrapped" by WCF-related API's as shown below:

private void AsmxServiceButton_Click(object sender, RoutedEventArgs e)

{

BasicHttpBinding bind = new BasicHttpBinding(); EndpointAddress endpoint = new EndpointAddress("http://localhost:50042/CallingServices_Web/Services/SimpleAsmx.asmx");

AsmxService.SimpleAsmxSoapClient asmx = new CallingServices.AsmxService.SimpleAsmxSoapClient(bind, endpoint);

asmx.HelloWorldWithAsmxCompleted += new EventHandler<CallingServices.AsmxService.HelloWorldWithAsmxCompletedEventArgs>(asmx_HelloWorldWithAsmxCompleted);

asmx.HelloWorldWithAsmxAsync(StringToEmit.Text);

}

void asmx_HelloWorldWithAsmxCompleted(object sender, CallingServices.AsmxService.HelloWorldWithAsmxCompletedEventArgs e)

{

OutputString.Text = string.Format("Output from ASMX: {0}", e.Result.ToString());

}

I would recommend that you check out the whole sample.

Hope this helps,
Jim (http://jimmangaly.blogspot.com/)

Please MARK the replies as answers if they answered your question

http://www.identitymine.com/
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities