Solved

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

Posted on 2008-06-23
5
1,846 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:JonMny
  • 2
5 Comments
 
LVL 16

Expert Comment

by:ororiole
ID: 21850410
What error are you getting? And post the code where you used it so we can take a look at the problem.
0
 
LVL 9

Accepted Solution

by:
JonMny earned 0 total points
ID: 21883003
ExampleWCFClient client = proxy = new ExampleWCFClient();


I fixed the problem, needed to cast the wcf
0
 
LVL 16

Expert Comment

by:ororiole
ID: 21886205
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript to set controls visibility 5 34
How to join on ID, with prefix? 15 58
Help with exporting to excel 4 26
Can't disable touch pad on Windows 10 computer 16 26
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question