WCF – Simple servcie
There several steps to set up a WCF service. You first add a WCF service to your project. I am using a simple project called 'SandboxWebsite'.
When you add the WCF service, it will create 3 files (I chose the name TestService):
* ITestService.cs // the interface
* TestService.cs // the service method
* TestService.svc // the service that is called (just a placeholder file)
First the interface for the service (ITestService.sc).
The interface has the 'ServiceContract' interface.
The service method has the 'OperationContract'.
You will notice the attribute 'WebGet()'. This specifies the service will be accessed via GET. (This uses the class System.ServiceModel.Web - it must be added as a reference as well. Confirm also that System.ServiceModel has also been added and is incuded in the 'Using' list).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace WCFService
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet()]
string Hello();
}
}
Then the actual service itself (TestService.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WCFService
{
public class TestService : ITestService
{
public string Hello()
{
return "Hello world";
}
}
}
The file TestService.svc contains only placholder info:
<%@ ServiceHost Language="C#" Debug="true" Service="TestService" CodeBehind="~/App_Code/TestService.cs" %>
That was the easy part.
Now for the Web.config file.
All additions go under '
The 4 additions are:
* binding
* service behavior
* endpoint behaviors
* endpoint
Our service uses 'webHttpBinding' service. Other binding types are 'wsHttpBinding' and 'mexHttpBinding' (not covered here... yet...)
The web.config settings
<system.serviceModel>
<bindings>
<!-- Minimum for WebHttp -->
<webHttpBinding>
<binding name="WCFService.MyDefaultBinding"/>
</webHttpBinding>
</bindings>
<behaviors>
<!-- serviceBehaviors -->
<serviceBehaviors>
<behavior name="WCFService.MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<!-- endpointBehaviors -->
<endpointBehaviors>
<behavior name="WCFService.MyWebBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<!-- Minimum for WebHttp -->
<service behaviorConfiguration="WCFService.MyServiceBehavior" name="WCFService.TestService">
<!-- Minimum for WebHttp -->
<endpoint address="web" behaviorConfiguration="WCFService.MyWebBehavior" binding="webHttpBinding" contract="WCFService.ITestService" bindingConfiguration="WCFService.MyDefaultBinding">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
</system.serviceModel>
How do I call it?
This service is set up to be called via GET, so it is easy. (your localhost port may be different)
http://localhost:3041/SandboxWebsite/TestService.svc/Web/Hello
This returns:
Hello world
To call this in code, it is almost as easy.
Create a HTTPWebRequest. Pass in the URL. Specify the Method as GET. Get the response stream. Read the respons stream to the end and you have it.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:3041/SandboxWebsite/TestService.svc/Web/Hello");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string s = reader.ReadToEnd();
September 11th, 2009 - 04:12
Very usefull information. Keep up the good work.
March 8th, 2010 - 21:13
Good, simple, and it works. thanks!