This tutorial is really great: (Video) Tim Heuer does an awesome job of explaining how to set up web services, so his video is highly recommended. Here is a link to his tutorial blog as well: (Tutorial Blog)
I am using Visual Studio 2008 with Silverlight enabled WCF services in this step-by-step guide:
1. Create the Project
In Visual Studio 2008 (with Silverlight installed), create a new Silverlight Application.
Right click on the website project (not the one with the XAML pages) and Add > New Item > Silverlight enabled WCF service. I called it 'SilverWCF.svc'.
Right-click on 'SilverWCF.svc' and choose 'View in Browser'. This is a very important step - and one to be repeated every time you make changes to the 'SilverWCF.svc.cs' file.
2. Add your service code
Change the DoWork() method to do something useful, like:
public string SayHello(string name)
{
return "Hello " + name;
}
3. Create the Interface
On Page.xaml, add a stackpanel inside your Grid, containing a textbox, textblock and a button to create a simple interface:
<StackPanel>
<TextBox Name="NameText"/>
<TextBlock Name="ResultText"/>
<Button Name="WCFButton" Content="Click" Click="Button_Click"/>
</StackPanel>
You can right click on "Button_Click" and choose 'Navigate to Event Handler' to generate the method.
4. Add a Service Reference
Right-click on the Silverlight project and choose 'Add Service Reference'. Click on the Discover button and name the reference. I called it 'SilverSVC'.
5. Set up the Event Handlers
The SayHelloAsync method calls the web service, passing it the value of the Text type in the NameText textbox. When the webservice completes, the SayHelloCompleted event handler puts the result string into the ResultText textblock:
private void Button_Click(object sender, RoutedEventArgs e)
{
SilverSVC.SilverWCFClient proxy = new webservice.SilverSVC.SilverWCFClient();
proxy.SayHelloCompleted += new EventHandler(proxy_SayHelloCompleted);
proxy.SayHelloAsync(NameText.Text);
}
void proxy_SayHelloCompleted(object sender, webservice.SilverSVC.SayHelloCompletedEventArgs e)
{
ResultText.Text = e.Result;
}
That's it, really - at this point you can build and run the project, type a name into the textbox, click the button and get a result from the web service :)

4 comments:
Thank you that was very helpful :D
am i i seem to get error: in the WCF, when i view in browser after addeing the sayHello method:
Compiler Error Message: CS0106: The modifier 'public' is not valid for this item
please ignore my last comment, i added the method to the wcf interface instead of the .cs *duh*!
now it works...
one severely critical thing u forgot to mention,
after creating the WCF service, need to modify web.config to :-
endpoint address="" binding="basicHttpBinding" contract="ISimpleWCF"
else it dont work and u get all kind of crazy error, think u should add that...
Post a Comment