Accessing a WCF service via the WebRequest component
Sometimes, it is required that our WCF service be called by some client platform which can only issue plain HTTP GET/POST requests. Or, in .NET framework-based applications, we sometimes might need to use WebRequest
components (under the
System.Net
namespace) to consume XML Web Services or WCF services.
In this recipe, we will demonstrate how to use a .NET WebRequest
component to consume an HTTP-based WCF service.
How to do it...
Here we will use a HttpWebRequest component to retrieve some simple text data from a standard WCF service application. Let's take a look at the steps to achieve this:
Set up the WCF service application.
The sample service here is a very simple one that contains one operation that accepts an input parameter and returns a string value. The following code snippet shows the service implementation.
public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}...