Golan Kiviti
Golan Kiviti

Reputation: 4245

WCF Proxy - Creating a generic invoke method to excute every access to the service

I'm using WCF and I made my own proxy in the client, and i want to create a method using lambda expression or Action that will excute everything.

Here is my proxy:

public class BooksProxy
{
    public ChannelFactory<IBooksService> Channel { get; set; }

    public BooksProxy()
    {
        Channel = new ChannelFactory<IBooksService>("endpoint");
    }

    public IBooksService CreateChannel()
    {
        return Channel.CreateChannel();
    }
}

Here is how i use the proxy:

IBooksService proxy = BooksProxy.CreateChannel();
IList<string> lst = proxy.GetStrings();
((ICommunicationObject)proxy).Close();

I want to do something like this in the BooksProxy class:

public void Execute(Action<...> action)
{
    IBooksService proxy = this.CreateChannel();

    /* executing here. */

    ((ICummunicationObject)proxy).Close();
}

And to call it like this maybe:

IList<string> result = null;
BooksProxy.Execute(proxy => { result = proxy.GetStrings(); });

Not quite sure how to do that...

Upvotes: 1

Views: 1186

Answers (1)

Golan Kiviti
Golan Kiviti

Reputation: 4245

Ok, so I figured how to do it.

Here is the Proxy, The idea is to make it generic:

public class Proxy<T>
{
    public ChannelFactory<T> Channel { get; set; }

    public Proxy()
    {
        Channel = new ChannelFactory<T>("endpoint");
    }

    public T CreateChannel()
    {
        return Channel.CreateChannel();
    }
}

Now here is the trick :

For void methods :

public void Execute(Action<T> action)
{
    T proxy = CreateChannel();

    action(proxy);

    ((ICommunicationObject)proxy).Close();
}

For return:

public TResult Execute<TResult>(Func<T, TResult> function)
    {
        T proxy = CreateChannel();

        var result = function(proxy);

        ((ICommunicationObject)proxy).Close();

        return result;
    }

Where the TResult is the returning type.

How to use:

Proxy<IService> proxy = new Proxy();
// for a void method
Proxy.Execute(prxy => prxy.Method());
// for non void method.
var result = Proxy.Execute(prxy => prxy.Method());

So, to sum up, here is how the proxy class should look like:

public class Proxy<T>
    {
        public ChannelFactory<T> Channel { get; set; }

        public Proxy()
        {
            Channel = new ChannelFactory<T>("endpoint");
        }

        public T CreateChannel()
        {
            return Channel.CreateChannel();
        }

        public void Execute(Action<T> action)
        {
            T proxy = CreateChannel();

            action(proxy);

            ((ICommunicationObject)proxy).Close();
        }

        public TResult Execute<TResult>(Func<T, TResult> function)
        {
            T proxy = CreateChannel();

            var result = function(proxy);

            ((ICommunicationObject)proxy).Close();

            return result;
        }
    }

I recommend this solution for a custom wcf proxy without using any service reference, its really simple and easy.

Upvotes: 2

Related Questions