【问题标题】:WCF 4.0 routing with mex using System.ServiceModel.Routing.RoutingService使用 System.ServiceModel.Routing.RoutingService 与 mex 进行 WCF 4.0 路由
【发布时间】:2011-07-10 01:09:44
【问题描述】:

我在以下地点为 MEX 工作:

net.tcp://remotehost:4508

什么是最短的 C#/F# 代码(很难理解 XML 配置文件 ^_^")我可以写什么来为它创建一个路由器?:

net.tcp://localhost:4508

MEX 也应该正确路由,以便客户端可以使用路由器

svcutil net.tcp://localhost:4508

发现服务方法。

【问题讨论】:

    标签: c# wcf f# routing wcf-routing


    【解决方案1】:

    这是我对我的问题的回答,它完全符合我的要求 - 没有任何 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
    

    【讨论】:

    • 这个很好,有C#版本吗?
    【解决方案2】:

    这应该是上面的代码 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();
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      查看路由器契约和图 6 一个简单的路由器实现。尽管这些示例使用 basicHttpBinding,但它也适用于 tcp。在需要的属性部分提供您的端点详细信息...

      【讨论】:

      • 抱歉,我不是在寻找有关讨论旧版本 WCF 或包括 XML 配置文件的文章的参考资料。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 2011-11-23
      • 2018-11-16
      • 2013-01-13
      • 2014-06-10
      相关资源
      最近更新 更多