NetTcpBinding - 自托管 WCF - 无法连接客户端
NetTcpBinding - Self-Hosted WCF - Can't get client connected
尝试获取 NetTcpBinding 的简单演示,以便将其扩展到另一个项目。
架构:2 个控制台应用程序(1 个 host/server、1 个客户端)和 1 个类型库项目。两个控制台应用程序都引用了类型库项目。
宿主应用程序:
class Program
{
static void Main()
{
var netTcpBinding = new NetTcpBinding(SecurityMode.None)
{
PortSharingEnabled = true
};
var netTcpAdddress = new Uri("net.tcp://127.0.0.1:1234/HelloWorldService/");
var tcpHost = new ServiceHost(typeof(HelloWorldService), netTcpAdddress);
tcpHost.AddServiceEndpoint(typeof(IHelloWorld), netTcpBinding, "IHelloWorld");
tcpHost.Open();
Console.WriteLine($"tcpHost is {tcpHost.State}. Press enter to close.");
Console.ReadLine();
tcpHost.Close();
}
}
public class HelloWorldService : IHelloWorld
{
public void HelloWorld()
{
Console.WriteLine("Hello World!");
}
public void WriteMe(string text)
{
Console.WriteLine($"WriteMe: {text}");
}
}
客户端应用程序:
static void Main()
{
Console.WriteLine("Press enter when the service is opened.");
Console.ReadLine();
var endPoint = new EndpointAddress("net.tcp://127.0.0.1:1234/HelloWorldService/");
var binding = new NetTcpBinding ();
var channel = new ChannelFactory<IHelloWorld>(binding, endPoint);
var client = channel.CreateChannel();
try
{
Console.WriteLine("Invoking HelloWorld on TcpService.");
client.HelloWorld();
Console.WriteLine("Successful.");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
Console.WriteLine("Press enter to quit.");
Console.ReadLine();
}
类型库:
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
void HelloWorld();
[OperationContract]
void WriteMe(string text);
}
我相信我已经安装了所有必要的服务并且运行:
显然我正在尝试在运行时进行所有配置。
我一直在客户端收到此错误消息:
在 TcpService 上调用 HelloWorld。
Exception: There was no endpoint listening at
net.tcp://127.0.0.1:1234/HelloWorldService/ that could accept the
message. This is often caused by an incorrect address or SOAP action.
See InnerException, if present, for more details. Press enter to quit.
我是不是遗漏了什么明显的东西?
您的服务正在公开地址的端点:
net.tcp://127.0.0.1:1234/HelloWorldService/IHelloWorld
但您的客户端正在连接到:
net.tcp://127.0.0.1:1234/HelloWorldService/
您还需要将客户端 NetTcpBinding
SecurityMode
设置为与服务器相同 (None
)。
尝试获取 NetTcpBinding 的简单演示,以便将其扩展到另一个项目。
架构:2 个控制台应用程序(1 个 host/server、1 个客户端)和 1 个类型库项目。两个控制台应用程序都引用了类型库项目。
宿主应用程序:
class Program
{
static void Main()
{
var netTcpBinding = new NetTcpBinding(SecurityMode.None)
{
PortSharingEnabled = true
};
var netTcpAdddress = new Uri("net.tcp://127.0.0.1:1234/HelloWorldService/");
var tcpHost = new ServiceHost(typeof(HelloWorldService), netTcpAdddress);
tcpHost.AddServiceEndpoint(typeof(IHelloWorld), netTcpBinding, "IHelloWorld");
tcpHost.Open();
Console.WriteLine($"tcpHost is {tcpHost.State}. Press enter to close.");
Console.ReadLine();
tcpHost.Close();
}
}
public class HelloWorldService : IHelloWorld
{
public void HelloWorld()
{
Console.WriteLine("Hello World!");
}
public void WriteMe(string text)
{
Console.WriteLine($"WriteMe: {text}");
}
}
客户端应用程序:
static void Main()
{
Console.WriteLine("Press enter when the service is opened.");
Console.ReadLine();
var endPoint = new EndpointAddress("net.tcp://127.0.0.1:1234/HelloWorldService/");
var binding = new NetTcpBinding ();
var channel = new ChannelFactory<IHelloWorld>(binding, endPoint);
var client = channel.CreateChannel();
try
{
Console.WriteLine("Invoking HelloWorld on TcpService.");
client.HelloWorld();
Console.WriteLine("Successful.");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
Console.WriteLine("Press enter to quit.");
Console.ReadLine();
}
类型库:
[ServiceContract]
public interface IHelloWorld
{
[OperationContract]
void HelloWorld();
[OperationContract]
void WriteMe(string text);
}
我相信我已经安装了所有必要的服务并且运行:
显然我正在尝试在运行时进行所有配置。
我一直在客户端收到此错误消息:
在 TcpService 上调用 HelloWorld。
Exception: There was no endpoint listening at net.tcp://127.0.0.1:1234/HelloWorldService/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. Press enter to quit.
我是不是遗漏了什么明显的东西?
您的服务正在公开地址的端点:
net.tcp://127.0.0.1:1234/HelloWorldService/IHelloWorld
但您的客户端正在连接到:
net.tcp://127.0.0.1:1234/HelloWorldService/
您还需要将客户端 NetTcpBinding
SecurityMode
设置为与服务器相同 (None
)。