出于学习目的,我正在尝试模仿 WCF 管道服务主机。
我有以下简单的服务定义
[ServiceContract]
public interface IPipeService
{
[OperationContract]
void PipeIn(string data);
}
以及一个通过网络管道使用它的客户端控制台应用程序(DotNet 8 SDK)
string URI = "net.pipe://localhost/Pipe";
var cf = new ChannelFactory<IPipeService>(
new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),
new EndpointAddress(URI));
cf.Open();
IPipeService proxy = cf.CreateChannel();
proxy.PipeIn("Hello");
我正在尝试点击原始消息,所以我有一个服务器端进程正在运行这个简单的两行代码:
var pipeIn = new NamedPipeServerStream("net.pipe://localhost/Pipe", PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.None);
pipeIn.WaitForConnectionAsync(token);
问题是 pipelineIn.WaitForConnectionAsync() 永远等待,在客户端,当 proxy.PipeIn() 被调用时,我立即抛出此错误:
There was no endpoint listening at net.pipe://localhost/Pipe that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
我尝试调试客户端代码,深入研究 WCF 的内部,但无济于事。我找不到执行实际“举重”功能的代码,并且我怀疑由于客户端代理是自动生成的,因此 VS22 调试器无法正确调试它。
我错过了什么? 我错过了什么?
您的地址有问题,解决方案如下:
您的服务器端口的URL地址不能包含接口地址。
如:
服务
var baseAddress = new Uri("net.pipe://localhost");
using (var host = new ServiceHost(typeof(PipeService), baseAddress))
{
var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
host.AddServiceEndpoint(typeof(IPipeService), binding, "PipeService");
host.Open();
Console.WriteLine("Host run");
Console.ReadLine();
}
客户:
string URI = "net.pipe://localhost/PipeService";
var cf = new ChannelFactory<IPipeService>(
new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),
new EndpointAddress(URI));
IPipeService proxy = cf.CreateChannel();
proxy.PipeIn("Hello");
Console.ReadLine();
如果您的服务器 URL 地址与客户端的匹配,您将收到错误消息: