How Do I get returned value from ASMX Service?
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]
开发者_如何学C public string GetPassword()
{
return "123";
}
}
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
- Right click on "References" in the Solution Explorer and choose "Add Service Reference..."
- Enter the address that you've located your service at into the Address box
- Click "GO"
- Choose the relevant service in the "Services" box
- Choose a namespace for the "Namespace" box
- 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();
}
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;
}
精彩评论