This post is intended for those interested in setting up a simple WCF Service in Visual Studio 2008. There are a few ways to do this, however, to show the setup and connectivity we'll use an ASP.NET Web Application.
Create the Project
Open Visual Studio 2008
Click File > New Project
Select the "Web" Project Type on the left
Select the ASP.NET Web Application template
Name it what you want and click OK
Create the Service
Right click the project and click Add New Item...
Select the WCF Service template
Name it what you want
Select Visual C# for language and click OK
Add your service method(s)
Open the C# Interface file for your service (ex. IHelloWorld.cs)
Add your method declaration with an OperationContract attribute
[OperationContract]
string SayHello(string n);
Open the C# Class file for your service (ex. HelloWorld.cs)
Add your method definition
public string SayHello(string n)
{
return "Hello " + n;
}
Build the project
You now have a WCF Service that you can connect to and a method to execute. Let's walk through connecting to the service.
Create the service proxy
Right click the project and select Add Service Reference...
Enter the service address (ex. http://localhost:[port]/[WebSiteName]/[ServiceName].svc) and click Go
Enter the Namespace for your service reference and click OK
Create a simple interface
Open the Default.aspx designer
Drag a TextBox control onto the designer (TextBox1)
Drag a LinkButton control onto the designer
Drag a Literal control onto the designer (LitResponse)
Double-click the LinkButton to create a click event handler
Connect to the service
Enter the following code for the button click handler
HelloService.HelloClient h = new HelloService.HelloClient();
this.LitResponse.Text = h.SayHello(this.TextBox1.Text);
h.Close();
...where HelloService is the service namespace I provided when setting up the proxy and Hello is my service name. The client class is created automatically when adding the service reference.