2

我一直在关注 MSDN 上的 WCF 路由服务教程:

动态配置
(见下面的代码)

在尝试将控制台示例转换为 IIS 托管原型时经历了很多痛苦之后,我现在有了一个 WCF 路由服务,它按照教程每 5 秒更新一次其配置。

我现在需要从网页触发此更新,而不是定时器每 5 秒自动更新一次,但找不到任何有关如何执行此操作的示例。类似于管理屏幕的东西,它处理存储在数据库中的端点的 CRUD 操作。如果用户对配置进行更改,路由服务将需要动态更新其配置。

显然,您可以使用 UDP 公告和发现服务执行类似的操作,但我不希望触发从另一个应用程序调用的更新的简单端点就足够了。

如何获取对路由服务的引用UpdateBehavior以便手动调用该UpdateRules方法?

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();
        }
    }    
}
4

3 回答 3

0

从您的ServiceHost实例开始,这很容易:

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

然后,如果您公开一个UpdateRules从内部私有类调用方法的方法,则可以调用它。

于 2015-07-20T09:17:03.080 回答
0

在网上找到了这个文件。 https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/routing-introduction#dynamic-configuration

RoutingExtension ApplyConfiguration 在这里会有所帮助。

附加片段。 在此处输入图像描述

于 2020-05-05T00:52:01.503 回答
0

您可能必须在自定义类中使用公共静态变量...

于 2016-02-22T21:39:43.180 回答