• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Experts
      • Alles
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Dapr
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DevOps
    • Digital Days
    • Docker
    • eHealth
    • Enterprise Architecture
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Stryker
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • Alles
    • Bloggers
    • Speakers
Home » How to implement a generic retry mechanism for a web service proxy?
  • How to implement a generic retry mechanism for a web service proxy?

    • By Marcel de Vries
    • .NET 16 years ago
    • .NET 0 comments
    • .NET .NET
    How to implement a generic retry mechanism for a web service proxy?

    For a customer I work for I had the need to implement a web service proxy that can do an automatic retry for me when I call a web service. I have a system that has web services in the local environment, but in the case the web service is not responding I have to retry the call on another server at the other end of the country cross a slow link. I did not want to implement the logic of switching the server in all subsequent client invocations so there my quest started for a generic implementation. When investigating the proxy generated by Visual Studio I discovered that the generated class unfortunately does not provide virtual methods for the method invocations. This means I could not work my way around this using a simple inheritance solution. (Implement my own invoke and do the retry there in case of an exception)


     


    But fortunately I found that the generated proxy class is derived from SoapHttpClientProtocol and that class on his turn inherits from MarshalByRefObject. In the past I have done some interception on MarshalByRefObjects and that is exactly the way I could implement this as well.


     


    I created a AutoRetryProxy that derives from RealProxy. The real proxy has a method called Invoke that will pass on all methods calls. This invoke method is the place where I could do my implementation of the retry. See the code sample below.


     


    public class AutoRetryProxy :RealProxy


    {


      private const string normalEndpoint = “http://localhost/webservice1/service1.asmx”;


      private const string alternativeEndpoint = “http://localhost/webservice1/service2.asmx”;


     


      HttpWebClientProtocol target;


     


      public AutoRetryProxy (HttpWebClientProtocol targetObj) : base(targetObj.GetType())


      {


       target=targetObj;


      }


     


      public override IMessage Invoke(IMessage message)


      {


       // just let the remoting infrastructure handle the call


       IMethodReturnMessage returnMessage =


                        RemotingServices.ExecuteMessage(target, (IMethodCallMessage) message);


     


       // check if we got an exception that we need to handle. These are WebExceptions


       // that indicate connectivity problems


       if((returnMessage.Exception != null) &&


                                      (returnMessage.Exception is System.Net.WebException))


       {


         System.Net.WebException ex = (System.Net.WebException )returnMessage.Exception;


         if((ex.Status == WebExceptionStatus.ConnectFailure) ||


                                                   (ex.Status == WebExceptionStatus.Timeout))


           {


             // Do some nifty alternative service discovery here 🙂


             target.Url = alternativeEndpoint;


            // now retry the call on the alternative server


             returnMessage =


                        RemotingServices.ExecuteMessage(target, (IMethodCallMessage) message);               


           }


         }


         return returnMessage;


       }


     


       public static object CreateProxy(Type generatedWebserviceProxyType)


       {


         if(generatedWebserviceProxyType.IsSubclassOf(typeof(SoapHttpClientProtocol)))


         {


           SoapHttpClientProtocol webserviceProxy =


             (SoapHttpClientProtocol)Activator.CreateInstance(generatedWebserviceProxyType);


           // use the default endpoint


           webserviceProxy.Url = normalEndpoint;


           return new AutoRetryProxy(webserviceProxy).GetTransparentProxy();


         }


         else


           throw new ArgumentException(“The type must be a derived from SoapHttpClientProtocol”);


       }


      }


     


    At the client side I only need to create the instance of the web service proxy generated by Visual Studio using the static Method I created on the AutoRetryProxy proxy.  You can see the client only needs to do a different create then the new operator, but this saves a lot of other code so that works fine for me. See the code below on how the client works with the proxy.


     


    private void button1_Click(object sender, System.EventArgs e)


    {


      webservice.Timeout = 5000;


      label1.Text =  webservice.HelloWorld();     


    }


     


    private void Form1_Load(object sender, System.EventArgs e)


    {


      webservice = (Service1)AutoRetryWSProxy.AutoRetryProxy.CreateProxy(typeof(Service1));


    }


     


     

    Share this

Marcel de Vries

View profile

Related IT training

Go to training website

Related Consultancy solutions

Go to infosupport.com

Related blogs

  • Innovative patterns in software quality management

    Innovative patterns in software quality management Tom van den Berg - 1 month ago

  • Developing a Service Fabric service using another host

    Developing a Service Fabric service using another host Tim van de Lockand - 5 months ago

  • Adding a package to your private WinGet.RestSource feed…

    Adding a package to your private WinGet.RestSource feed… Léon Bouquiet - 10 months ago

Related downloads

  • Beslisboom voor een rechtmatig ‘kopietje productie’

  • Klantreferentie: Remmicom zet wetgeving om in intellige…

  • Klantreferentie RDW: Samenwerken voor veilig en vertrou…

  • Klantreferentie BeFrank: Strategische IT voor een innov…

  • Wie durft te experimenteren met data in de zorg?

Related videos

  • mijnverzekeringenopeenrij.nl

    mijnverzekeringenopeenrij.nl

  • Winnaar | Innovation Projects 2017

    Winnaar | Innovation Projects 2017

  • Explore | Info Support & HAN & Poliskluis

    Explore | Info Support & HAN & Poliskluis

  • LifeApps bij HagaZiekenhuis

    LifeApps bij HagaZiekenhuis

  • Info Support | Bedrijfsfilm

    Info Support | Bedrijfsfilm

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet
Beheer cookie toestemming
Deze website maakt gebruik van Functionele en Analytische cookies voor website optimalisatie en statistieken.
Functioneel
Altijd actief
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door uw Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een website of over verschillende websites te volgen voor soortgelijke marketingdoeleinden.
Beheer opties Beheer diensten Beheer leveranciers Lees meer over deze doeleinden
Voorkeuren
{title} {title} {title}