Dynamically update WCF Routing Service configuration at runtime

1.2k Views Asked by At

I have been following the WCF Routing Service tutorials on MSDN:

Dynamic Configuration
Source (See code below)

After much pain trying to convert the console examples to IIS hosted prototypes, I now have a WCF Routing Service which updates its configuration once every 5 seconds as per the tutorial.

I now need to trigger this update from a Web page instead of the timer auto updating every 5 seconds but cannot find any examples of how to do this. Something like an admin screen which handles CRUD ops for endpoints stored in a DB. If a user makes a change to the config, the Routing Service will need to update its configuration dynamically.

Apparently you can do something like this with UDP Announcements and Discovery Services, but I don't want that A simple endpoint which triggers the update called from another app will suffice.

How do I get a reference to the routing service UpdateBehavior in order to manually call the UpdateRules method?

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Routing;
using System.Threading;

namespace ErpRoutingService
{
    public class UpdateBehavior : BehaviorExtensionElement, IServiceBehavior
    {
        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }

        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            RulesUpdateExtension rulesUpdateExtension = new RulesUpdateExtension();
            serviceHostBase.Extensions.Add(rulesUpdateExtension);
        }
        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }

        class RulesUpdateExtension : IExtension<ServiceHostBase>, IDisposable
        {
            bool primary = false;
            ServiceHostBase owner;
            Timer timer;

            void IExtension<ServiceHostBase>.Attach(ServiceHostBase owner)
            {
                this.owner = owner;
                //Call immediately, then every 5 seconds after that.
                this.timer = new Timer(this.UpdateRules, this, TimeSpan.Zero, TimeSpan.FromSeconds(5));
            }

            void IExtension<ServiceHostBase>.Detach(ServiceHostBase owner)
            {
                this.Dispose();
            }

            public void Dispose()
            {
                if (this.timer != null)
                {
                    this.timer.Dispose();
                    this.timer = null;
                }
            }

            void UpdateRules(object state)
            {
                //Updating Routing Configuration
                RoutingConfiguration rc = new RoutingConfiguration();

                var inspector = new ErpMessageInspectorBehavior();

                if (this.primary)
                {
                    ServiceEndpoint endPoint101 = new ServiceEndpoint(
                    ContractDescription.GetContract(typeof(IRequestReplyRouter)),
                    new BasicHttpBinding(),
                    new EndpointAddress("http://meldev:62395/ErpIntegrationService.svc"));
                    endPoint101.EndpointBehaviors.Add(inspector);
                    rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { endPoint101 });                    
                }
                else
                {
                    ServiceEndpoint endPoint102 = new ServiceEndpoint(
                        ContractDescription.GetContract(typeof(IRequestReplyRouter)),
                        new BasicHttpBinding(),
                        new EndpointAddress("http://meldev:62396/ErpIntegrationService.svc"));
                    endPoint102.EndpointBehaviors.Add(inspector);
                    rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { endPoint102 });                    
                }

                this.owner.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc);


                this.primary = !this.primary;
            }
        }

        public override Type BehaviorType
        {
            get { return typeof(UpdateBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new UpdateBehavior();
        }
    }    
}
3

There are 3 best solutions below

0
Maku On

Starting from your ServiceHost instance it's pretty easy:

var updateBahvaior = serviceHost.Description.Behaviors.Find<UpdateBehavior>();

Then, if you expose a method that calls UpdateRules method from the inner private class, you can call it.

0
Jason Shuler On

You might have to use public static variables in your custom class...

0
AAATechGuy On

Found this document online. https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/routing-introduction#dynamic-configuration

RoutingExtension ApplyConfiguration would help here.

Attaching snippet. enter image description here