请查看此代码:
namespace Server.Contracts
{
[ServiceContract]
public interface IMessageService
{
[OperationContract]
void DoWork(string message);
}
}
服务器
namespace Server
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MessageService : IMessageService
{
public void DoWork(string message)
{
Console.WriteLine(message);
}
}
}
客户
namespace Client
{
class Program
{
static void Main(string[] args)
{
int i = 0;
var location = new[] { "London", "Paris", "New York", "Tokyo" };
if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length <= 2)
{
// Start my Server...
Process.Start(@"C:\Server\bin\Debug\Server.exe");
}
i = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length;
string loc = location[i];
new Program().Stt(loc);
}
private void Stt(string loc)
{
Console.WriteLine("Press ank key to continue...");
Console.ReadLine();
var uri = "net.tcp://localhost:4565/MessageService";
var binding = new NetTcpBinding(SecurityMode.None);
var channel = new ChannelFactory<IMessageService>(binding);
var endpoint = new EndpointAddress(uri);
var proxy = channel.CreateChannel(endpoint);
if (proxy != null)
proxy.DoWork(loc);
Console.ReadLine();
}
}
}
因此,当我启动server
并且server
和clients
都在运行时,我想发送消息:
if (proxy != null)
proxy.DoWork(loc);
因此,尽管我的服务器已启动并正在运行,但在这一点上我仍然遇到此错误:
System.ServiceModel.EndpointNotFoundException:'没有端点 在net.tcp:// localhost:4565 / MessageService上侦听,它可以接受 消息。这通常是由于地址或SOAP错误导致的 行动。有关更多详细信息,请参见InnerException(如果存在)。