WCF routing — how to correctly add filter table programmatically

早过忘川 提交于 2019-12-23 17:15:07

问题


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

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?


回答1:


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)));



回答2:


You can find a good example on MSDN here: How To: Dynamic Update Routing Table

Note how they dont directly create an instance of the MessageFilterTable, but instead use the 'FilterTable' property provided by a new RoutingConfiguration instance.

If you have written a custom filter, then you will add it like this:

rc.FilterTable.Add(new CustomMessageFilter("customStringParameter"), new List<ServiceEndpoint> { physicalServiceEndpoint });

The CustomMessageFilter will be your filter, and the "customStringParameter" is the string that (I believe) you are talking about. When the Router receives a connection request, it will attempt to map it via this table entry, if this is successful, then you are right, the router will create a client endpoint to talk to the ServiceEndpoint that you provided.



来源:https://stackoverflow.com/questions/6118201/wcf-routing-how-to-correctly-add-filter-table-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!