C# - WCF:没有端点侦听
C# - WCF: There was no endpoint listening
我看过一些类似的问题,但 none 似乎有帮助。我正试图掌握 WCF,原因是我想创建一个 Windows 服务,我可以使用 Windows 表单应用程序连接到该服务。我遵循了一些教程并创建了以下 Windows 服务,该服务已安装并 运行ning 在我的 Windows 7 设备上:
using System;
using System.ServiceModel;
using System.ServiceProcess;
[ServiceContract]
public interface HelloWorld
{
[OperationContract]
string SayHello(string value);
}
public class Hello : HelloWorld
{
public string SayHello(string value)
{
string retVal = "";
if (value == "Hello")
{
retVal = "Hello World!";
}
else
{
retVal = "Say hello to me...";
}
return (retVal);
}
}
class PluginService : ServiceBase
{
public PluginService()
{
this.ServiceName = "Test Plugin Service";
this.CanStop = true;
this.CanShutdown = true;
}
static void Main()
{
ServiceBase.Run(new PluginService());
}
protected override void OnStart(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Hello),
new Uri[]{
new Uri("http://localhost:8998"),
new Uri("net.pipe://localhost")
}))
{
host.AddServiceEndpoint(typeof(HelloWorld), new BasicHttpBinding(), "Hello");
host.AddServiceEndpoint(typeof(HelloWorld), new NetNamedPipeBinding(), "PipeHello");
host.Open();
}
}
protected override void OnStop()
{
}
protected override void OnShutdown()
{
}
}
我还创建了一个应该连接到该服务的非常基本的控制台应用程序。这个想法是你通过这个控制台实用程序说 "Hello",服务 returns "Hello World":
using System;
using System.ServiceModel;
namespace PluginApplication
{
[ServiceContract]
public interface HelloWorld
{
[OperationContract]
string SayHello(string value);
}
class Program
{
static void Main(string[] args)
{
ChannelFactory<HelloWorld> httpFactory =
new ChannelFactory<HelloWorld>(
new BasicHttpBinding(),
new EndpointAddress(
"http://localhost:8998/Hello"));
ChannelFactory<HelloWorld> pipeFactory =
new ChannelFactory<HelloWorld>(
new NetNamedPipeBinding(),
new EndpointAddress(
"net.pipe://localhost/PipeHello"));
HelloWorld httpProxy = httpFactory.CreateChannel();
HelloWorld pipeProxy = pipeFactory.CreateChannel();
while (true)
{
try
{
Console.WriteLine("Say something...");
string str = Console.ReadLine();
Console.WriteLine("http: " +
httpProxy.SayHello(str));
Console.WriteLine("pipe: " +
pipeProxy.SayHello(str));
}
catch (Exception Ex)
{
Console.WriteLine(Ex);
}
}
}
}
}
当我 运行 控制台并输入 "Hello" 时,几秒钟后出现以下错误:
System.ServiceModel.EndpointNotFoundException: There was no endpoint listeniong at http://localhost:8998/Hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. No connection could be made because the target machine actively refused it 127.0.0.1:8998
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
防火墙在我的设备上完全禁用,所以我真的不确定为什么它说目标机器主动拒绝连接。任何人都可以对此有所了解吗?
您在 OnStart()
方法中使用了 using
结构。这意味着一旦该方法完成,您创建的 ServiceHost
对象将不再可用,因此您定义的端点也将不可用。相反,将 ServiceHost
变量静态存储在您的 windows 服务中。
我看过一些类似的问题,但 none 似乎有帮助。我正试图掌握 WCF,原因是我想创建一个 Windows 服务,我可以使用 Windows 表单应用程序连接到该服务。我遵循了一些教程并创建了以下 Windows 服务,该服务已安装并 运行ning 在我的 Windows 7 设备上:
using System;
using System.ServiceModel;
using System.ServiceProcess;
[ServiceContract]
public interface HelloWorld
{
[OperationContract]
string SayHello(string value);
}
public class Hello : HelloWorld
{
public string SayHello(string value)
{
string retVal = "";
if (value == "Hello")
{
retVal = "Hello World!";
}
else
{
retVal = "Say hello to me...";
}
return (retVal);
}
}
class PluginService : ServiceBase
{
public PluginService()
{
this.ServiceName = "Test Plugin Service";
this.CanStop = true;
this.CanShutdown = true;
}
static void Main()
{
ServiceBase.Run(new PluginService());
}
protected override void OnStart(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Hello),
new Uri[]{
new Uri("http://localhost:8998"),
new Uri("net.pipe://localhost")
}))
{
host.AddServiceEndpoint(typeof(HelloWorld), new BasicHttpBinding(), "Hello");
host.AddServiceEndpoint(typeof(HelloWorld), new NetNamedPipeBinding(), "PipeHello");
host.Open();
}
}
protected override void OnStop()
{
}
protected override void OnShutdown()
{
}
}
我还创建了一个应该连接到该服务的非常基本的控制台应用程序。这个想法是你通过这个控制台实用程序说 "Hello",服务 returns "Hello World":
using System;
using System.ServiceModel;
namespace PluginApplication
{
[ServiceContract]
public interface HelloWorld
{
[OperationContract]
string SayHello(string value);
}
class Program
{
static void Main(string[] args)
{
ChannelFactory<HelloWorld> httpFactory =
new ChannelFactory<HelloWorld>(
new BasicHttpBinding(),
new EndpointAddress(
"http://localhost:8998/Hello"));
ChannelFactory<HelloWorld> pipeFactory =
new ChannelFactory<HelloWorld>(
new NetNamedPipeBinding(),
new EndpointAddress(
"net.pipe://localhost/PipeHello"));
HelloWorld httpProxy = httpFactory.CreateChannel();
HelloWorld pipeProxy = pipeFactory.CreateChannel();
while (true)
{
try
{
Console.WriteLine("Say something...");
string str = Console.ReadLine();
Console.WriteLine("http: " +
httpProxy.SayHello(str));
Console.WriteLine("pipe: " +
pipeProxy.SayHello(str));
}
catch (Exception Ex)
{
Console.WriteLine(Ex);
}
}
}
}
}
当我 运行 控制台并输入 "Hello" 时,几秒钟后出现以下错误:
System.ServiceModel.EndpointNotFoundException: There was no endpoint listeniong at http://localhost:8998/Hello that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. No connection could be made because the target machine actively refused it 127.0.0.1:8998 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
防火墙在我的设备上完全禁用,所以我真的不确定为什么它说目标机器主动拒绝连接。任何人都可以对此有所了解吗?
您在 OnStart()
方法中使用了 using
结构。这意味着一旦该方法完成,您创建的 ServiceHost
对象将不再可用,因此您定义的端点也将不可用。相反,将 ServiceHost
变量静态存储在您的 windows 服务中。