Click here to Skip to main content
Click here to Skip to main content

Using WCF without a generated proxy, by using ChannelFactory

By , 5 Jul 2010
Rate this:
Please Sign up or sign in to vote.
I find myself re-using this sort of snippet often, so I'm posting it here for all to enjoy. Smile | :)
 
The issue here is: Using the built in proxy generator for referencing WCF services from client code is OK, but a neater way to do it is shown below - this ONLY works if the server and client code are both controlled by the author - its no good for publicly exposed web services where you expect the user to query the WSDL or service metadata as this will always end up with a SVCUTIL built proxy.
 
So what you do is, pull out your interface and your contract objects, to a 3rd assembly, and reference that from your service code and also your client. Then simply use the class shown below, from the client, plugging in the contract interface where the generic type is required. Add a config line pointing at the service endpoint. Voila!
 
public class ServiceWrapper<T> : IDisposable where T: class
    {
        ChannelFactory<T> factory;
        private T channel;
 
        private readonly BasicHttpBinding binding;
        private readonly EndpointAddress endpoint;
 
        private readonly object lockObject = new object();
        private bool disposed;
 
        public ServiceWrapper(string configName)
        {
            if (ConfigurationManager.AppSettings[configName] == null)
            {
                throw new ConfigurationErrorsException(configName + " is not present in the config file");
            }
 
            binding = new BasicHttpBinding();
            endpoint = new EndpointAddress(ConfigurationManager.AppSettings[configName]);
            disposed = false;
        }
 
        public T Channel
        {
            get
            {
                if (disposed)
                {
                    throw new ObjectDisposedException("Resource ServiceWrapper<"+typeof(T)+"> has been disposed");
                }
 
                lock (lockObject)
                {
                    if (factory == null)
                    {
                        factory = new ChannelFactory<T>(binding, endpoint);
                        channel = factory.CreateChannel();
                    }
                }
                return channel;
            }
        }
 
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 

        public void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    lock (lockObject)
                    {
                        if (channel != null)
                        {
                            ((IClientChannel) channel).Close();
                        }
                        if (factory != null)
                        {
                            factory.Close();
                        }
                    }
 
                    channel = null;
                    factory = null;
                    disposed = true;
                }
            }
        }
    }
 
Usage:
(Don't forget to wrap the service wrapper in a using statement so that the dispose gets called when it goes out of scope)
 
 
private const string SERVICE_ENDPOINT = "ServiceEndpoint";
 
...
 
using (var sw = new ServiceWrapper<IDataService>(SERVICE_ENDPOINT))
 {
    MyType t = sw.Channel.MyFunction(3);
}
 
Config:
 
<appSettings>
    <add key="ServiceEndpoint" value="http://localhost:55757/DataService.svc" />
  </appSettings>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gizz
Architect
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMiled Felizardo8-Feb-13 5:05 
GeneralReason for my vote of 5 nice one PinmemberPranay Rana30-Dec-10 17:18 
GeneralQuick & short explanation will expose the beauty of this tip... PinmentorMd. Marufuzzaman30-Jun-10 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 5 Jul 2010
Article Copyright 2010 by Gizz
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid