简体   繁体   English

使用System.ServiceModel.Routing.RoutingService使用mex进行WCF 4.0路由

[英]WCF 4.0 routing with mex using System.ServiceModel.Routing.RoutingService

I have service with working MEX at: 我在MEX工作:

net.tcp://remotehost:4508

What is the shortest C#/F# code (hard time understanding XML configuration files ^_^") I could write to create a router to it at?: 什么是最短的C#/ F#代码(很难理解XML配置文件^ _ ^“)我可以编写它来创建路由器?:

net.tcp://localhost:4508

MEX should also be routed properly so that clients can use the router MEX也应该正确路由,以便客户端可以使用路由器

svcutil net.tcp://localhost:4508

to discover service methods. 发现服务方法。

Here's my answer to my question that does exactly what I want - without any XML salad, in less then 50 lines of F#: 这是我的问题的答案,它完全符合我的要求 - 没有任何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

This should be above code F# converted to C# i don't realy know the F# language so it could not be working as F# code. 这应该高于代码F#转换为C#我不会真正知道F#语言所以它不能用作F#代码。 But i tested it and it succesfuly route metadata. 但我测试了它,它成功地路由了元数据。

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

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

Look at Router Contract and Figure 6 A Simple Router Implementation. 查看路由器合同和图6简单路由器实现。 Although the examples usign basicHttpBinding, it will work for tcp as well. 虽然示例使用了basicHttpBinding,但它也适用于tcp。 Provide your endpoint details in the attribute section where required... 在需要的属性部分中提供您的端点详细信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM