Is it possible to use WCF discovery to expose a WCF endpoint that is using named pipes?(是否可以使用 WCF 发现来公开使用命名管道的 WCF 端点?)
问题描述
我有一个使用发现的应用程序,它可以本地部署在同一台 PC 上,也可以远程部署在它使用的服务之外.有没有办法通过 WCF 发现公开命名管道绑定?如果不是,我想我可以在发现服务后进行协商以确定最合适的绑定.
I have an application using discovery that may be deployed locally on the same PC or remote from the service it consumes. Is there any way to expose the named pipe binding via WCF discovery? If not I suppose I can negotiate after the service is discovered to determine the most suitable binding.
推荐答案
是的,可以这样做.但由于地址将被列为localhost"(net.pipe 唯一可能的地址),您可能需要进行某种测试来验证该服务实际上与发现客户端在同一台机器上运行.
Yes, it's possible to do that. But since the address will be listed as "localhost" (the only possible address for net.pipe), you'll likely need some sort of test to verify that the service is actually running on the same machine as the discovery client.
public class StackOverflow_7068743
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
}
public class Service : ITest
{
public string Echo(string text)
{
return text + " (via " + OperationContext.Current.IncomingMessageHeaders.To + ")";
}
}
public static void Test()
{
string baseAddressHttp = "http://" + Environment.MachineName + ":8000/Service";
string baseAddressPipe = "net.pipe://localhost/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddressHttp), new Uri(baseAddressPipe));
host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.AddServiceEndpoint(typeof(ITest), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), "");
host.Open();
Console.WriteLine("Host opened");
DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(ITest)));
Console.WriteLine(findResponse.Endpoints.Count);
EndpointAddress address = null;
Binding binding = null;
foreach (var endpoint in findResponse.Endpoints)
{
if (endpoint.Address.Uri.Scheme == Uri.UriSchemeHttp)
{
address = endpoint.Address;
binding = new BasicHttpBinding();
}
else if (endpoint.Address.Uri.Scheme == Uri.UriSchemeNetPipe)
{
address = endpoint.Address;
binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
break; // this is the preferred
}
Console.WriteLine(endpoint.Address);
}
if (binding == null)
{
Console.WriteLine("No known bindings");
}
else
{
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(binding, address);
ITest proxy = factory.CreateChannel();
Console.WriteLine(proxy.Echo("Hello"));
((IClientChannel)proxy).Close();
factory.Close();
}
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
这篇关于是否可以使用 WCF 发现来公开使用命名管道的 WCF 端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以使用 WCF 发现来公开使用命名管道的 WCF 端点?
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01