首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在运行时动态更新WCF路由服务配置

在运行时动态更新WCF路由服务配置
EN

Stack Overflow用户
提问于 2014-06-12 22:30:21
回答 3查看 1.1K关注 0票数 2

我一直在遵循MSDN上的WCF路由服务教程:

Dynamic Configuration

Source (参见下面的代码)

在尝试将控制台示例转换为IIS托管原型之后,我现在有了一个WCF路由服务,它根据教程每5秒更新一次配置。

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

显然,你可以通过UDP公告和发现服务来做这样的事情,但我不希望一个简单的端点触发从另一个应用调用的更新就足够了。

为了手动调用UpdateRules方法,我如何获得对路由服务UpdateBehavior的引用?

代码语言:javascript
运行
复制
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();
        }
    }    
}
EN

回答 3

Stack Overflow用户

发布于 2015-07-20 17:17:03

从您的ServiceHost实例开始,这非常简单:

代码语言:javascript
运行
复制
var updateBahvaior = serviceHost.Description.Behaviors.Find<UpdateBehavior>();

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

票数 0
EN

Stack Overflow用户

发布于 2016-02-23 05:39:43

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

票数 0
EN

Stack Overflow用户

发布于 2020-05-05 08:52:02

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

RoutingExtension ApplyConfiguration在这方面会有所帮助。

附加代码段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24187004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档