views:

64

answers:

2

I have an ASMX service. I want to receive a response from it. My code is below:

public class UserService : System.Web.Services.WebService
{
    [WebMethod]
    public string GetPassword()
    {
        return "123";
    }
}
+3  A: 

If you mean, "how do I connect to this web service?" you'll need to create a Visual Studio project (I'm assuming VS2k8 here), be it Console Application, Windows Forms, or pretty much any other

  1. Right click on "References" in the Solution Explorer and choose "Add Service Reference..."
  2. Enter the address that you've located your service at into the Address box
  3. Click "GO"
  4. Choose the relevant service in the "Services" box
  5. Choose a namespace for the "Namespace" box
  6. Hit OK

Visual Studio will now generate a service proxy for you. If you chose your namespace as, for example "MyNamespace", then in Visual Studio you can add in your code:

using (var client = new MyNamespace.UserService())
{
    var result = client.GetPassword();
}
Rob
But I use Silverlight :) ...client.GetPasswordAsync()... and it function dont return a value
Boheanh Josiggle
Well, that's because you're calling the Async version of the method. You'll need to add an event handler to the `client.GetPasswordCompleted` method as the result will be in `e.Result` as asynchronous methods don't (1) return their result directly and (2) block the execution of your code until they're completed.
Rob
No.I dont found e.Result propetry :( I take a screen shot http://img205.imageshack.us/img205/4241/screenss.png Image of My IDE
Boheanh Josiggle
See if it'll let you change `System.ComponentModel.AsyncCompletedEventArgs` to `GetPasswordCompletedEventArgs`, or similar. It would also be useful if you pasted in the code you're currently using to call `client.GetPasswordAsync()`
Rob
GetPasswordCompletedEventArgs not found my code :(
Boheanh Josiggle
Thanks for all replies. I found GetPasswordCompletedEventArgs class :) I found ! Again thanks. ..
Boheanh Josiggle
In that case, how about accepting this answer then? :)
Rob
+1  A: 

I hope you want to wire up an ASMX service to your Silverlight application. If that's the case, you can take a look at this blog.

Though I've used a WCF service in my blog, wiring up a sevice to a Silverlight application is all one and the same.

Follow the steps in the blog to add the ASMX service as a ServiceReference.

Try this code on the Client Side

private void Connect2Service()
{
  ServiceReference.UserServiceClient client = new ServiceReference.UserServiceClient();
  client.GetPasswordCompleted += 
             new EventHandler<GetPasswordCompletedEventArgs>(client_GetPasswordCompleted);
  client.GetPasswordAsync();
}

private void client_GetPasswordCompleted(object sender, GetPasswordCompletedEventArgs e)
{
    // Textblock will show the output. In your case "123"
    textblock.Text = e.Result;
}
Aswin Ramakrishnan
Very Thanks. It working...
Boheanh Josiggle
Please mark it as answer, so that it helps others too. Thank you
Aswin Ramakrishnan