WCF路由-如何以编程方式正确添加过滤器表 [英] WCF routing -- how to correctly add filter table programmatically
问题描述
我正在使用WCF 4路由服务,并且需要以编程方式配置服务(而不是通过config).我所见过的示例(很少见)创建了如下的MessageFilterTable:
I am using the WCF 4 routing service, and need to configure the service programmatically (as opposed to via config). The examples I have seen of doing so, which are rare, create a MessageFilterTable as follows:
var filterTable=new MessageFilterTable<IEnumerable<ServiceEndpoint>>();
但是,该方法的通用参数应该是TFilterData(要过滤的数据类型)吗?我有一个接受字符串的自定义过滤器-我仍然可以这种方式创建过滤器表吗?
But, the generic parameter to that method is supposed to be TFilterData (the type of data you are filtering on)? I have my own custom filter that accepts a string -- can I still create the filter table this way?
如果这行得通...路由基础架构会在我传入的列表之外创建客户端端点吗?
If this will work...will the routing infrastructure create client endpoints out of the list I pass in?
推荐答案
我已经创建了WCF 4路由服务并以编程方式对其进行了配置.我的代码之间的间隔比需要的要多一些(可维护性是其他人的关注点,因此是注释),但是它绝对有效.它有两个过滤器:一个过滤器将某些特定的操作过滤到给定的端点,第二个过滤器将其余的操作发送到通用端点.
I have created a WCF 4 routing service and configured it programmatically. My code is a bit more spaced out than it needs to be (maintainability for others being a concern, hence the comments), but it definitely works. This has two filters: one filters some specific Actions to a given endpoint, and the second sends the remaining actions to a generic endpoint.
// Create the message filter table used for routing messages
MessageFilterTable<IEnumerable<ServiceEndpoint>> filterTable = new MessageFilterTable<IEnumerable<ServiceEndpoint>>();
// If we're processing a subscribe or unsubscribe, send to the subscription endpoint
filterTable.Add(
new ActionMessageFilter(
"http://etcetcetc/ISubscription/Subscribe",
"http://etcetcetc/ISubscription/KeepAlive",
"http://etcetcetc/ISubscription/Unsubscribe"),
new List<ServiceEndpoint>()
{
new ServiceEndpoint(
new ContractDescription("ISubscription", "http://etcetcetc/"),
binding,
new EndpointAddress(String.Format("{0}{1}{2}", TCPPrefix, HostName, SubscriptionSuffix)))
},
HighRoutingPriority);
// Otherwise, send all other packets to the routing endpoint
MatchAllMessageFilter filter = new MatchAllMessageFilter();
filterTable.Add(
filter,
new List<ServiceEndpoint>()
{
new ServiceEndpoint(
new ContractDescription("IRouter", "http://etcetcetc/"),
binding,
new EndpointAddress(String.Format("{0}{1}{2}", TCPPrefix, HostName, RouterSuffix)))
},
LowRoutingPriority);
// Then attach the filter table as part of a RoutingBehaviour to the host
_routingHost.Description.Behaviors.Add(
new RoutingBehavior(new RoutingConfiguration(filterTable, false)));
这篇关于WCF路由-如何以编程方式正确添加过滤器表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!