Link to home
Start Free TrialLog in
Avatar of JonMny
JonMny

asked on

C# WCF proxy Help me understand this example code...

I am trying to use this class for calling my WCF Service


http://electroholic.com/?p=10

in the sample the author has this code
ExampleWCFClient client = proxy = new ExampleWCFClient();

this will not compile.


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.ServiceModel;
 
namespace ConsoleApplication1
{
    public delegate void SafeProxyInvocationHandler(out ICommunicationObject proxy);
 
    public static class SafeProxyHandler
    {
        public static Exception Invoke(SafeProxyInvocationHandler del)
        {
            return Invoke(null, del);
        }
 
        public static Exception Invoke(string messageTemplate,
                                        SafeProxyInvocationHandler del)
        {
            if (del == null)
                throw new ArgumentException("Expected delegate instance for handler to invoke!");
 
            ICommunicationObject proxy = null;
 
            try
            {
                del(out proxy);
                HandleClose(proxy);
 
                return null;
            }
            catch (TimeoutException exception)
            {
                HandleException(proxy, exception, messageTemplate);
                return exception;
            }
            catch (FaultException exception)
            {
                HandleException(proxy, exception, messageTemplate);
                return exception;
            }
            catch (CommunicationException exception)
            {
                HandleException(proxy, exception, messageTemplate);
                return exception;
            }
            catch (Exception exception)
            {
                HandleException(proxy, exception, messageTemplate);
                return exception;
            }
        }
 
        private static void HandleClose(ICommunicationObject proxy)
        {
            if (proxy != null
                && proxy.State != CommunicationState.Closed
                && proxy.State != CommunicationState.Closing
                && proxy.State != CommunicationState.Faulted)
                proxy.Close();
        }
 
        private static void HandleException(ICommunicationObject proxy,
                                                          Exception e,
                                                          string messageTemplate)
        {
            AbortClient(proxy);
            ReportException(e, messageTemplate);
        }
 
        private static void ReportException(Exception e, string template)
        {
            Trace.WriteLine(FormatMessage(template, e.GetType().Name));
        }
 
        private static string FormatMessage(string message, params string[] args)
        {
            return args == null || args.Length == 0 ? message : string.Format(message, args);
        }
 
        private static void AbortClient(ICommunicationObject proxy)
        {
            if (proxy != null)
                proxy.Abort();
        }
    }
}

Open in new window

Avatar of ororiole
ororiole
Flag of United States of America image

What error are you getting? And post the code where you used it so we can take a look at the problem.
ASKER CERTIFIED SOLUTION
Avatar of JonMny
JonMny

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did request of the asker to post the code where he was using that line. As It is not in the first post. And post the error he was getting. Since I added value to the question, and several days elapsed, I'm confident that at least partial value should be awarded.