WCF 4.0 using mex using System.ServiceModel.Routing.RoutingService

I have a service with a working MEX at:

net.tcp://remotehost:4508

What is the shortest C # / F # code (it’s hard to understand the XML configuration files ^ _ ^ ") I could write to create a router on it :?

net.tcp://localhost:4508

MEX must also be properly routed so that clients can use the router

svcutil net.tcp://localhost:4508

to discover service methods.

+3
source share
3 answers

Here is my answer to my question that does exactly what I want - without XML salad, in less than 50 F # lines:

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
source

F #, #, 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();
        }
    }
}
+3

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

Look at the contract with the router and Figure 6 Simple implementation of the router. Although the examples are usign basicHttpBinding, it will work for tcp as well. Provide endpoint information in the attribute section where you want ...

-1
source

Source: https://habr.com/ru/post/1796710/


All Articles