When you deploy a SOAP based web service, test clients are essential to test the web service. This gets complicated when you deploy secure web services. Lot of config sections to add on both server and client side. If you had a REST endpoint, testing your service is very simple. Just open a browser and type the URL, it should execute the web method and return you some results. This helps you to test your test to all the way down like database layer.
Enough said, Let’s see how we can add SOAP and REST endpoints to a WCF web service.
- Open Visual Studio and File –> New project.
- Select WCF on “Installed Templates” section and select “WCF Service Application”.
- Let’s name our application as “SoapAndRestService”. It should create a WCF service project with bunch of files.
- Let’s rename all Visual Studio named services and variables. I am going to rename IService1, Service1, to IHelloService, HelloService.
- Let’s see code for IHelloService and HelloService. We just have two web methods that can be only accessed via SOAP clients.
- When you create a “WCF Service Application”, you won’t see any services or bindings configured in “web.config” file. But when you run your service using “WcfTestClient.exe” or accessing it via web browser, you can see, WCF “magically” displaying your service meta data and service contracts. It is because WCF configures a “basicHttpBinding” if noting was configured.
- To make it less magic and more clear, lets add the defaults things[Service, Endpoint, Behavior] to our project. This is how our web.config file looks after adding default SOAP endpoint.
- Binding determines, how an “endpoint” is consumed. To consume WCF service via browser or HTTP GET, we need an “endpoint” with “webHttpBinding” binding and “webHttp” behavior.
- So let’s add Rest endpoint with “webHttpBinding” and “webHttp” behavior. This is how “web.config” file looks after edit. You can consume the “REST” service at http://localhost:62611/HelloService.svc/Rest/
- One last thing, we have configured endpoints to access our “HelloService”, but we haven’t configured which methods can be accessed via HTTP GET.
- So let’s add “WebGet” attribute to HelloRest method in IHelloService.cs
- Now we are ready to test the web service using a browser, If you access the HelloRest method using this URL, http://localhost:62611/HelloService.svc/Rest/HelloRest/Hey . Server should respond back for your message.