使用NamedPipesBinding的WCF服务获取PipeException(管道正在关闭)

lys*_*cid 6 .net c# wcf wcf-binding wcf-client

我只是在学习WCF,尝试不同的东西.

我已经设置了以下服务:

[ServiceBehavior(IncludeExceptionDetailInFaults = true , 
    InstanceContextMode = InstanceContextMode.Single)]
    public class TestService : ITestService
    {
        // This operation is defined as OneWay.
        public void Throws()
        {
            throw new ArgumentException();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我从我的客户端使用它,如下所示:

var baseAddress = new Uri("net.pipe://localhost/hello");

// Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(TestService), baseAddress))
{
    var netBinding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
    host.AddServiceEndpoint(typeof(ITestService), netBinding, baseAddress);
    host.Open();

    Console.WriteLine("The service is ready at {0}", baseAddress);

    // Create client channel.
    var channel = ChannelFactory<ITestService>.CreateChannel(netBinding, new EndpointAddress(baseAddress));

    ((ICommunicationObject)channel).Open();

    try
    {
        foreach (var i in Enumerable.Range(0, 5000))
        {
            // channel dies after a few attempts.
            channel.Throws();
        }
    }
Run Code Online (Sandbox Code Playgroud)

该方法抛出被定义为IsOneWay =真,这意味着它不会传播的任何消息返回给客户端(包括错误).

在循环中运行时,通信对象在某些运行后出现故障.我无法弄清楚这个原因.

例外细节:

System.ServiceModel.CommunicationException:写入管道时出错:管道正在关闭.(232,0xe8).---> System.IO.PipeException:写入管道时出错:管道正在关闭.(232,0xe8).System.ServiceModel.Channels.PipeConnection.StartSyncWrite(Byte [] buffer,Int32 offset,Int32 size,Object&holder)at System.ServiceModel.Channels.PipeConnection.Write(Byte [] buffer,Int32 offset,Int32 size,Boolean immediate, TimeSpan超时,BufferManager bufferManage r)---内部异常堆栈跟踪结束---

请注意,如果我将Throws方法的主体更改为其他内容,例如Console.WriteLine,则一切正常.

编辑:我已将示例项目上传到我的SkyDrive:http://sdrv.ms/NumUbR

如果有人想在本地编译它,看它是否表现相同.

Ern*_*ieL 5

您只需在某个时刻超出可用带宽.它可能是管道,但它也可能在WCF堆栈中...处理异常是昂贵的,并且你在尽可能紧的循环中做了5000个.从异常更改为不WriteLine()返回任何内容可以解决问题,因为它可以显着减少所需的带宽/处理.(我确实看到你提到了OneWay,但我认为它没有太大变化.即使没有返回异常,它仍然需要处理).

尝试将InstanceContextMode更改为PerCall.这是"高容量"服务的标准设置.它将减轻一些拥堵.

也是为了解决这些意见,托管这样的服务很好.ServiceHost将管理自己的线程.