c# - WCF 4.0 路由与 mex 使用 System.ServiceModel.Routing.RoutingService

标签 c# wcf f# routes wcf-routing

我在工作 MEX 服务于:

net.tcp://remotehost:4508

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

net.tcp://localhost:4508

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

svcutil net.tcp://localhost:4508

发现服务方法。

最佳答案

这是我对我的问题的回答,它完全符合我的要求——没有任何 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# - WCF 4.0 路由与 mex 使用 System.ServiceModel.Routing.RoutingService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5214858/

相关文章:

c# - 如何在 c# WPF 框架中禁用导航快捷方式

c# - 制作二维数组/集合类的最佳实践

c# - Entity Framework 5 软删除

c# - 为什么使用错误的方法签名生成 Reference.cs(WCF 服务引用)

c# - 如何从我的 C# 测试访问服务日志?

methods - F#编译器需要项目引用,但方法是私有(private)的

c# - 如何检查针对 Sql Server 数据库的 linq 查询结果是否存在数据?

c# - 在 .NET 4.5 或更高版本中使用 WCF 连接到远程 JSON/XML REST 服务+

f# - FSharp.Data 可选类型抛出异常

algorithm - 在 F# 中编写重复文件查找器的技巧