WCF REST Client built on top of WCF SOAP Service Reference

On Server Side:

  • Define IService1 with OperationContract and WebInvoke and WebGet tags
  • Add an SVC file – SoapService1 – with NotImplementedException implementation of IService1
  • Add an SVC file – RestService1 – with implementation of IService1, in the View Markup – Set:
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" 
    

On Client Side:

  • Add Service Reference to SoapService1

  • Add reference to System.ServiceModel.Web

  • Add the following class

    public class HttpClientBase<T>
    {
        public T Channel { get; private set; }
    
        private string EndpointConfigurationName { get; set; }
    
        private string RemoteAddress;
        private EndpointAddress EndpointAddress;
        private Binding Binding;
    
        public HttpClientBase()
        {
            ChannelFactory<T> cf = new ChannelFactory<T>(new WebHttpBinding(), new EndpointAddress("http://localhost:32903/Service1.svc/"));
            cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
            Channel = cf.CreateChannel();
        }
    
        public HttpClientBase(string endpointConfigurationName)
        {
            // TODO: Complete member initialization
            this.EndpointConfigurationName = endpointConfigurationName;
            Channel = ChannelFactory<T>.CreateChannel(Binding, EndpointAddress);
        }
    
        public HttpClientBase(string endpointConfigurationName, string remoteAddress)
        {
            // TODO: Complete member initialization
            this.EndpointConfigurationName = endpointConfigurationName;
            this.RemoteAddress = remoteAddress;
            Channel = ChannelFactory<T>.CreateChannel(Binding, EndpointAddress);
        }
    
        public HttpClientBase(string endpointConfigurationName, EndpointAddress remoteAddress_2)
        {
            // TODO: Complete member initialization
            this.EndpointConfigurationName = endpointConfigurationName;
            this.EndpointAddress = remoteAddress_2;
            Channel = ChannelFactory<T>.CreateChannel(Binding, EndpointAddress);
        }
    
        public HttpClientBase(Binding binding, EndpointAddress remoteAddress)
        {
            // TODO: Complete member initialization
            this.Binding = binding;
            this.EndpointAddress = remoteAddress;
            Channel = ChannelFactory<T>.CreateChannel(Binding, EndpointAddress);
        }
    }

  • In Reference.cs, change ClientBase to above HttpClientBase
  • In Reference.cs, for IService1, add the WebGet and WebInvoke attributes, these can be deduced from /help urls.
Advertisements

About vijayvepa

I'm a software consultant for Software Specialists Inc. currently working at Philips Respironics, Pittsburgh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: