How to connect to web service without .config

Oct 4, 2010 at 3:23 PM

Hi,

I redistribute my application only exe file without .config file.

ClientChannel<RCS.GSelector.Services.Core.Interfaces.IGSService> cl = new ClientChannel<RCS.GSelector.Services.Core.Interfaces.IGSService>("netTcp", ChannelManageOptions.Sequential);
cl.Address = service_uri;                
m_service = cl.Channel;                                

How to create ClientChannel without endpoint in .config file??? Or how to create endpoint section "netTcp" in the source code?

pls.

Coordinator
Oct 5, 2010 at 1:22 PM

Hi misak,

The built-in ClientChannel supports loading of a ChannelFactory according to a configuration entry.
In order to be able to provide it with a ChannelFactory in runtime, you need to create a sub-class of your own and override the 'CreateChannelFactoryCore' as you wish.

You can create the channel factory just as you would have done without using WCF Contrib and return it in that method and it should all work.

Actually, it is a general recommendation that you create a general sub-class that all the code would use. That way, you will have a central place where you can customize behavior.

The following example should do the trick -

Sub-classes:

    using WcfContrib.Client;
    using System.ServiceModel;

    public class MyClientChannel<T> : MyClientChannel<MyClientChannel<T>, T>
        where T : class
    {
        public MyClientChannel() { }

        public MyClientChannel(shannelManageOptions manageOptions)
            : base(manageOptions) { }

        public MyClientChannel(ChannelFactory channelFactory)
            : base(channelFactory) { }

        public MyClientChannel(ChannelFactory channelFactory, ChannelManageOptions manageOptions)
            : base(channelFactory, manageOptions) { }
    }

    public class MyClientChannel<TInstance, T> : ClientChannel<TInstance, T>
        where T : class
        where TInstance : ClientChannel<T>, new()
    {
        private ChannelFactory _factory;
       
        public MyClientChannel()
        {
            OnInitialize(null);
        }

        public MyClientChannel(ChannelManageOptions manageOptions)
            : base(manageOptions)
        {
            OnInitialize(null);
        }

        public MyClientChannel(ChannelFactory channelFactory)
        {
            OnInitialize(channelFactory);
        }

        public MyClientChannel(ChannelFactory channelFactory, ChannelManageOptions manageOptions)
            : base(manageOptions)
        {
            OnInitialize(channelFactory);
        }

        void OnInitialize(ChannelFactory channelFactory)
        {
            //You can decide to disable factory caching if you provide it with a factory which is cached already
            //CacheChannelFactory = (channelFactory != null);
           
            _factory = channelFactory;
        }

        protected override ChannelFactory CreateChannelFactoryCore(string endpointName)
        {
            if (_factory == null)
            {
                //If the factory wasn't provided from outside, just call the base which tries to load it from the configuration
                _factory = base.CreateChannelFactoryCore(string endpointName);
            }

            return _factory;
        }
    }

Usage:

//Create factory in runtime - provide it with your own Binding and EndpointAddress for example
ChannelFactory factory = CreateFactory<IMyService>();

var clientChannel = new MyClientChannel<IMyService>(factory);
clientChannel.Channel.DoOperation();

I hope that helps, let me know if anything else is needed.

Regards,
Amir Zuker