C# WCF 4.0使用System.ServiceModel.routing.RoutingService与mex进行路由
我在以下地点与MEX合作:C# WCF 4.0使用System.ServiceModel.routing.RoutingService与mex进行路由,c#,wcf,f#,routing,wcf-routing,C#,Wcf,F#,Routing,Wcf Routing,我在以下地点与MEX合作: net.tcp://remotehost:4508 我可以编写什么样的最短C#/F#代码(难以理解XML配置文件^ ^)来创建路由器 net.tcp://localhost:4508 MEX也应该正确路由,以便客户端可以使用路由器 svcutil net.tcp://localhost:4508 发现服务方法。 查看Router Contract和图6一个简单的路由器实现。虽然示例使用了Sign basicHttpBinding,但它也适用于tcp。在需要的地方
net.tcp://remotehost:4508
我可以编写什么样的最短C#/F#代码(难以理解XML配置文件^ ^)来创建路由器
net.tcp://localhost:4508
MEX也应该正确路由,以便客户端可以使用路由器
svcutil net.tcp://localhost:4508
发现服务方法。
查看Router Contract和图6一个简单的路由器实现。虽然示例使用了Sign basicHttpBinding,但它也适用于tcp。在需要的地方,在属性部分提供您的端点详细信息…以下是我对我的问题的回答,它完全符合我的要求-没有任何XML沙拉,用不到50行的F#:
模块应用=
开放系统
开放系统.ServiceModel
open System.ServiceModel.Routing
open System.ServiceModel.Dispatcher
open System.ServiceModel.Description
让createSimpleRouter createBinding(routerAddress:string)服务地址=
让routerType=typeof
让routerContract=ContractDescription.GetContract(routerType)
让端点地址=新的ServiceEndpoint(routerContract,createBinding(),新的端点地址(address))
让serviceEndpoints=[|端点服务地址|]
let配置=新路由配置()
configuration.FilterTable.Add(新的MatchAllMessageFilter(),serviceEndpoints)
let host=新服务主机(typeof)
忽略(ra,sa)
|——>(“净。tcp://localhost:4508/“,”净。tcp://remotehost:4508/")
让netTcp()=新的NetTcpBinding(SecurityMode.None)
让mexTcp()=MetadataExchangeBindings.CreateMexTcpBinding()
让TcpOuter=createSimpleRouter netTcp routerAddress serviceAddress
让mexRouter=createSimpleRouter mexTcp(routerAddress+“mex”)(serviceAddress+“mex”)
tcpRouter.Open()
mexRouter.Open()
WriteLine(“路由…\n{0}R:{1}”,serviceAddress,routerAddress)
忽略这应该在代码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();
}
}
}
使用系统;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
使用System.ServiceModel.Routing;
使用System.ServiceModel.Dispatcher;
使用System.ServiceModel.Description;
命名空间FSharpRouterInCSharp
{
班级计划
{
静态ServiceHost createSimpleRouter(绑定createBinding、字符串routerAddress、字符串serviceAddress)
{
var routerType=类型(IReQuestreplyOuter);
var routerContract=ContractDescription.GetContract(routerType);
//var endpoint=新的ServiceEndpoint(routerContract、createBinding、新端点地址(address));
var serviceEndpoints=new ServiceEndpoint[]{new ServiceEndpoint(routerContract,createBinding,new EndpointAddress(serviceAddress))};
var配置=新路由配置();
添加(新的MatchAllMessageFilter(),serviceEndpoints);
var host=新服务主机(typeof(RoutingService));
AddServiceEndpoint(routerType、createBinding、routerAddress);
host.Description.Behaviors.Add(新路由行为(配置));
返回主机;
}
静态void Main(字符串[]参数)
{
string routerAddress=“net。tcp://localhost:4508/";
string serviceAddress=“net。tcp://remotehost:4508/";
var netTcp=新的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();
WriteLine(“路由…\n{0}R:{1}”,serviceAddress,routerAddress);
Console.ReadKey();
}
}
}
很抱歉,我没有寻找关于旧版本WCF或包含XML配置文件的文章的参考资料。这很好,您有C版本吗?
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();
}
}
}