簡體   English   中英

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

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

我在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

這應該高於代碼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();
        }
    }
}

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

查看路由器合同和圖6簡單路由器實現。 雖然示例使用了basicHttpBinding,但它也適用於tcp。 在需要的屬性部分中提供您的端點詳細信息...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM