Windows Server 2008上的WCF NetNamedPipeBinding TimeoutException

时间:2011-12-15 08:51:50

标签: wcf netnamedpipebinding

我遇到WCF NetNamedPipeBinding问题。当我在Windows XP机器上通过Visual Studio 2008运行我的服务器和客户端代码时,一切正常。但是,只要我将服务器部署为Windows服务并在Windows Server 2008中安装我的客户端应用程序,每当我尝试使用任何合同方法时,我都会在客户端获得TimeoutException。似乎我可以成功创建客户端并打开它,但不能调用任何方法。

服务初始化代码:

Uri baseAddress = new Uri("http://localhost:8500/xNet/xNetService");
string address = "net.pipe://localhost/xNet/xNetService";

_xNetAPIServiceHost = new ServiceHost(typeof(xNetService), baseAddress);

NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
_xNetAPIServiceHost.AddServiceEndpoint(typeof(IServiceAPI), binding, address);

// Add a mex endpoint
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri("http://localhost:8501/xNet/xNetService/mex");
_xNetAPIServiceHost.Description.Behaviors.Add(smb);

_xNetAPIServiceHost.Open();

客户端初始化代码:

string address = "net.pipe://localhost/xNet/xNetService";

NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);

_serviceClient = new ServiceAPIClient(binding, new EndpointAddress(address));
_serviceClient.Open();

Windows服务作为“本地系统帐户”运行。我不知道问题是什么。我不知道这是一个安全帐户问题,或者命名管道是否打开?我会假设,因为我可以成功创建并打开客户端,它似乎至少找到了命名管道。如果没有TimeoutException,我就无法调用任何服务方法。

1 个答案:

答案 0 :(得分:0)

在尝试了各种绑定并回到基础之后,我注意到示例程序工作正常,但除非我使用Visual Studio进行调试,否则我的工作没有用。那时我决定用我自己的代码继续进行。为了简化调试,我关闭了绑定中的所有安全性。

我开始评论我服务的OnStart方法中的大多数语句,以确定可能发生的事情。除了初始化ServiceHost的代码之外,我注释掉了所有内容。奇迹般地,我的客户现在可以成功地与服务进行通信。然后我开始取消注释OnStart方法中的每行代码,直到我的客户端突然再次开始给我一个TimeoutException。

我的服务类,说“MyAPI”,实现合同“IMyAPI”。除了使用“MyAPI”类作为WCF服务之外,我还在我的服务内部使用“MyAPI”类的实例来执行各种操作(“内部”方法)。在我的OnStart方法中,我首先创建了一个“MyAPI”类的实例,然后创建了ServiceHost:

MyAPI API = new MyAPI();

ServiceHost service = new ServiceHost(typeof(MyAPI));

我没有收到任何错误或异常,所以看起来一切都很好,但实际上我无法使用客户端连接到服务。一旦我改变了上述陈述的顺序,客户就开始重新开始工作了:

ServiceHost service = new ServiceHost(typeof(MyAPI));

MyAPI API = new MyAPI();

我不确定为什么会发生这种情况,我所知道的是我可以在内部使用我的API并且作为服务而不会出现任何客户端连接问题。也许那里的某些人会对这背后的原因有所了解,或者我的代码可能没有正确设计。

相关问题