Маршрутизация WCF 4.0 с mex с использованием System.ServiceModel.Routing.RoutingService - PullRequest
3 голосов
/ 07 марта 2011

У меня есть служба с рабочим MEX по адресу:

net.tcp://remotehost:4508

Какой самый короткий код C # / F # (трудно понять файлы конфигурации XML ^ _ ^ "), который я мог написать для создания маршрутизатора к нему на?:

net.tcp://localhost:4508

MEX также должен быть правильно маршрутизирован, чтобы клиенты могли использовать маршрутизатор

svcutil net.tcp://localhost:4508

для обнаружения методов обслуживания.

Ответы [ 3 ]

3 голосов
/ 07 марта 2011

Вот мой ответ на мой вопрос, который делает именно то, что я хочу - без какого-либо салата XML, менее чем в 50 строках F #:

namespace CORSIS

module Application =

    open System

    open System.ServiceModel
    open System.ServiceModel.Routing
    open System.ServiceModel.Dispatcher
    open System.ServiceModel.Description


    let createSimpleRouter createBinding (routerAddress : string) serviceAddress = 

        let routerType = typeof<IRequestReplyRouter>
        let routerContract = ContractDescription.GetContract(routerType)
        let endpoint address = new ServiceEndpoint(routerContract, createBinding(), new EndpointAddress(address))

        let serviceEndpoints = [| endpoint serviceAddress |]
        let configuration = new RoutingConfiguration()
        configuration.FilterTable.Add(new MatchAllMessageFilter(), serviceEndpoints)

        let host = new ServiceHost(typeof<RoutingService>)
        ignore <| host.AddServiceEndpoint(routerType, createBinding(), routerAddress)
        host.Description.Behaviors.Add(new RoutingBehavior(configuration))
        host        

    [<EntryPoint>]
    let main(args) =

        let (routerAddress, serviceAddress) =
            match args with
            | [| ra; sa |] -> (ra, sa)
            | _ -> ("net.tcp://localhost:4508/", "net.tcp://remotehost:4508/")

        let netTcp() = new NetTcpBinding(SecurityMode.None)
        let mexTcp() = MetadataExchangeBindings.CreateMexTcpBinding()

        let tcpRouter = createSimpleRouter netTcp  routerAddress           serviceAddress
        let mexRouter = createSimpleRouter mexTcp (routerAddress + "mex") (serviceAddress + "mex")

        tcpRouter.Open()
        mexRouter.Open()

        Console.WriteLine("routing ...\n{0} <-> R:{1}", serviceAddress, routerAddress)

        ignore <| Console.ReadKey true

        0
3 голосов
/ 10 сентября 2011

Это должно быть выше кода F #, преобразованного в C # Я действительно не знаю язык F #, поэтому он не может работать как код F #.Но я проверил это, и это успешно метаданные маршрута.

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Routing;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;

namespace FSharpRouterInCSharp
{
    class Program
    {
        static ServiceHost createSimpleRouter (Binding createBinding, string routerAddress, string serviceAddress)
        {
            var routerType = typeof (IRequestReplyRouter);
            var routerContract = ContractDescription.GetContract(routerType);

            //var endpoint = new ServiceEndpoint(routerContract, createBinding, new EndpointAddress(address));

            var serviceEndpoints = new ServiceEndpoint[] { new ServiceEndpoint(routerContract, createBinding, new EndpointAddress(serviceAddress))};
            var configuration = new RoutingConfiguration();
            configuration.FilterTable.Add(new MatchAllMessageFilter(), serviceEndpoints);

            var host = new ServiceHost(typeof (RoutingService));
            host.AddServiceEndpoint(routerType, createBinding, routerAddress);
            host.Description.Behaviors.Add(new RoutingBehavior(configuration));
            return host;
        }

        static void Main(string[] args)
        {
            string routerAddress = "net.tcp://localhost:4508/";
            string serviceAddress = "net.tcp://remotehost:4508/";

            var netTcp = new NetTcpBinding(SecurityMode.None);
            var mextTcp = MetadataExchangeBindings.CreateMexTcpBinding();

            var tcpRouter = createSimpleRouter(netTcp, routerAddress, serviceAddress);
            var mexRouter = createSimpleRouter(mextTcp,routerAddress+"mex",serviceAddress+"mex");

            tcpRouter.Open();
            mexRouter.Open();

            Console.WriteLine("routing ...\n{0} <-> R:{1}", serviceAddress, routerAddress);
            Console.ReadKey();
        }
    }
}
0 голосов
/ 07 марта 2011

http://msdn.microsoft.com/en-us/magazine/cc500646.aspx

Посмотрите на контракт маршрутизатора и рисунок 6 Простая реализация маршрутизатора. Хотя в примерах используется basicHttpBinding, он будет работать и для tcp. Укажите данные своей конечной точки в разделе атрибутов, где это необходимо ...

...