Consuming ASMX web services from Silverlight and WPF November 20, 2008
Posted by koolkatblog in ASP.NET.Tags: ASMX, Silverlight, WPF
2 comments
I have just started writing some applications in Silverlight and WFP. I have many existing ASMX web services that I still need to use. Here is how you go about it.
For Silverlight:
1. Add a Service Reference to your Silverlight project . For my example, I called it ServiceReferenceTest1
2. In your code where you want to make the call, add the following code.
(Intellisense will help).
One note: The call must be asynchronous
ServiceReferenceTest1.Service1SoapClient proxy = new ServiceReferenceTest1.Service1SoapClient();
proxy.HelloWorldCompleted +=
new EventHandler(proxy_HelloWorldCompleted);
proxy.HelloWorldAsync();
3. Provide the event for the completion of the asynchronous call.
This example just returns the string “Hello World”.
void proxy_HelloWorldCompleted(object sender,
StationCasinosPostCards.ServiceReferenceTest1.HelloWorldCompletedEventArgs e)
{
String result = e.Result.ToString();
}
Calling ASMX Web Services from Javascript using AJAX November 19, 2008
Posted by koolkatblog in ASP.NET.Tags: AJAX, web service
add a comment
This is an example of how you can call a web service from your javascript using AJAX.
1. To begin, create a new AJAX enabled website.
2. Add a class to your project. Call it Pizza.cs and add the following code:
public Pizza(string name, string desc, double cost)
{
this.m_name = name;
this.m_desc = desc;
this.m_cost = cost;
}
private string m_name;
private string m_desc;
private double m_cost; public string name { get { return this.m_name; }set { this.m_name = value; } }
public string desc { get { return this.m_desc; } set { this.m_desc = value; } }
public double cost { get { return this.m_cost; } set { this.m_cost = value; }
2. Add a web service to your project. Call it PizzaPlace.asmx and add the following 2 methods:
[WebMethod]
public int GetLocationCount(int zipCode)
{
int locations = 0;
switch (zipCode)
{
case 89128:
locations = 5;
break;
case 89118:
locations = 7;
break;
default:
break;
}
return locations;
}
[WebMethod]
public List<Pizza> GetDeals()
{
List<Pizza> pz = new List<Pizza>();
Pizza p1 = new Pizza(“Pepperoni Special”, “Large Pepperoni”, 9.99);
Pizza p2 = new Pizza(“Vegetarian”, “Large Mushroom, Onions, Peppers, Olives”, 10.99);
pz.Add(p1);
pz.Add(p2);
return pz;
}