MSDN の WCF ルーティング サービスのチュートリアルに従っています。
コンソールの例を IIS でホストされているプロトタイプに変換するのに苦労した後、チュートリアルに従って 5 秒ごとに構成を更新する WCF ルーティング サービスができました。
5 秒ごとにタイマーを自動更新する代わりに、Web ページからこの更新をトリガーする必要がありますが、これを行う方法の例が見つかりません。DB に格納されたエンドポイントの CRUD ops を処理する管理画面のようなもの。ユーザーが構成を変更した場合、Routing Service はその構成を動的に更新する必要があります。
どうやら 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();
}
}
}