.NET

Simple WCF SOAP-REST Multi-Project Template

Download the source code for this post.

I did it again: another multi-project Visual Studio template – this time for a simple WCF service that exposes both SOAP and REST endpoints. My other REST and SOAP templates are intended as a starting point for more real-world WCF services. However, what I often need is a starting point for building a “proof-of-concept” service in order to explore various configuration options and scenarios.

To get the new template, all you have to do is fire up Visual Studio, then open up the Extensions Manager from under the Tools menu. Search for “WCF SOAP-REST” in the Online Gallery, then click Download to install the extension.

Then to use the template, simply select New Project from the File menu, click on the WCF category and select “WCF SOAP and REST Simple Service.” After clicking OK, simply press F5 to start the ASP.NET Development web server (aka Cassini), and press Ctrl+F5 to launch the client and keep the console from closing.

There are a couple of things different about this project. First, you’ll notice only three projects are created: Client, Service and Web. Both the service contract and implementation are located in the Service project. Second, the service is a simple GreetingService with a method that accepts and returns a simple string. This is usually sufficient for simple configuration scenarios. The web host exposes both SOAP and REST style endpoints but does not sport the usual REST bells and whistles, such as integration with the ASP.NET routing module.

Lastly, there is a “useFiddler” flag in the client app.config which, when set, appends a dot to the “localhost” part of the url when calling the service. This lets you easily launch Fiddler to intercept HTTP traffic to your service.

Here is code in the client project that uses SOAP and REST to communicate with the service. The SOAP client uses a ClientChannel helper class in order to properly clean up the client channel when exiting the “using” block and not throw an extra exception if the channel is faulted. The REST client uses LINQ to XML to parse the response.

class Program
{
    static void Main(string[] args)
    {
        string soapResponse = UseSoap();
        Console.WriteLine("SOAP: {0}", soapResponse);
        string restResponse = UseRest();
        Console.WriteLine("REST: {0}", restResponse);
    }
    private static string UseSoap()
    {
        var factory = new ChannelFactory<IGreetingService>("soap");
        factory.Endpoint.Address = new EndpointAddress(GetAddress(factory.Endpoint.Address.ToString()));
        using (var client = new ClientChannel<IGreetingService>(factory.CreateChannel()))
        {
            return client.Channel.Hello("Tony");
        }
    }
    private static string UseRest()
    {
        string url = ConfigurationManager.AppSettings["rest"];
        var client = new WebClient { BaseAddress = GetAddress(url) };
        string responseString = client.DownloadString("?name=Tony");
        XElement responseXml = XElement.Parse(responseString);
        return responseXml.Value;
    }
    private static string GetAddress(string address)
    {
        bool useFiddler;
        if (bool.TryParse(ConfigurationManager.AppSettings["useFiddler"], out useFiddler)
            && useFiddler)
        {
            return address.Replace("localhost", "localhost.");
        }
        return address;
    }
}

Hopefully this template will save you some time when all you want to do is put in place a simple WCF SOAP or REST service for testing and exploratory purposes. Enjoy.

Reference: Simple WCF SOAP-REST Multi-Project Template from our NCG partner Tony Sneed at the Tony Sneed’s Blog blog.

Related Articles

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button