How to do it: Consuming an WCF Service through a custom WCF Client

Code Samples
Table of Contents
Summary
WCF Custom client application

WCF Custom client application

Description

If a client application implements manually a ClientBase<T> derived class then the configuration file of the service must be build also manually.

After add an  configuration file to the client application then it must be edited manually or through the WCF configuration file editor (included on the Visual Studio tools):

Step 1: Open editor and initiate configuration of the “Client” section

WCF configuration file editor (Step 1)

Step 2: Specify that the “Client” is going to be configured from start

Step 3: Indicate service contract of the ABC of the endpoint

Step 4: Chose communication mode (i.e. binding) of the ABC of the endpoint

Step 5: Indicate the address of the ABC of the endpoint

Step 6: Confirm Client configuration

Step 7:  Return to the editor main view

Step 8: Confirm that the “Client” section is configured as need

 

 

Operations
File "Program.cs"
How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCF
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                var _proxy = new TestServiceProxy();
                var lvCategory = _proxy.getCategoryAsXML("1");

            }catch (Exception ex) {
                Console.WriteLine(ex);
            }
        }

    }
}
File "App.config"

The configuration file (created manually) contains only the required data for the Client application to access the WCF service.

How to do it
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="http://localhost:55545/training/wcf/app/testservice1/soap"
                binding="wsHttpBinding" bindingConfiguration="" contract="WCF.IwcfTestService"
                name="" kind="" endpointConfiguration="" />
        </client>
    </system.serviceModel>
</configuration>
File "proxy.cs"
How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WCF
{
    public class TestServiceProxy : ClientBase<WCF.IwcfTestService>, WCF.IwcfTestService
    {
        public TestServiceProxy()
        {
        }

        public TestServiceProxy(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }

        public TestServiceProxy(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        public TestServiceProxy(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }

        public TestServiceProxy(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }

        public WCF.Category getCategoryWithDataContractAsXML(string pId)
        {
            return base.Channel.getCategoryWithDataContractAsXML(pId);
        }

        public WCF.Category getCategoryWithDataContractAsJSON(string pId)
        {
            return base.Channel.getCategoryWithDataContractAsJSON(pId);
        }

        public List<WCF.Category> getCategoriesWithDataContractAsXML()
        {
            return base.Channel.getCategoriesWithDataContractAsXML();
        }

        public List< WCF.Category> getCategoriesWithDataContractAsJSON()
        {
            return base.Channel.getCategoriesWithDataContractAsJSON();
        }

        public int addCategoryWithDataContractAsXML(WCF.Category pCategory)
        {
            return base.Channel.addCategoryWithDataContractAsXML(pCategory);
        }

        public string getCategoryAsXML(string pId)
        {
            return base.Channel.getCategoryAsXML(pId);
        }

        public string getCategoryAsJSON(string pId)
        {
            return base.Channel.getCategoryAsJSON(pId);
        }

        public List<string> getCategoriesAsXML()
        {
            return base.Channel.getCategoriesAsXML();
        }

        public List<string> getCategoriesAsJSON()
        {
            return base.Channel.getCategoriesAsJSON();
        }

        public int addCategory(string pName)
        {
            return base.Channel.addCategory(pName);
        }
    }
}