我只是在學習WCF,嘗試不同的事情。使用NamedPipesBinding獲取PipeException(管道正在關閉)WCF服務
我已經設置了以下服務:
[ServiceBehavior(IncludeExceptionDetailInFaults = true ,
InstanceContextMode = InstanceContextMode.Single)]
public class TestService : ITestService
{
// This operation is defined as OneWay.
public void Throws()
{
throw new ArgumentException();
}
}
我用它從我的客戶像這樣:
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();
}
}
的方法拋出定義爲IsOneWay =真,這意味着它不會傳播任何消息給客戶端(包括錯誤)。
在循環中運行時,通信對象在一些運行後會發生故障。 我無法弄清楚這個原因。
異常的詳細信息:
System.ServiceModel.CommunicationException:有一個錯誤寫 到管道:管道被關閉。 (232,0xe8)。 ---> System.IO.PipeException:寫入管道時發生錯誤: 管道正在關閉。 (232,0xe8)。在在 System.ServiceModel.Channels.PipeConnection.Write(字節[]緩衝 System.ServiceModel.Channels.PipeConnection.StartSyncWrite(字節[] 緩衝器,的Int32偏移的Int32大小,對象&保持器),的Int32 偏移的Int32大小,布爾眼前,時間跨度超時,BufferManager bufferManage R)---內部異常堆棧跟蹤的結尾---
需要注意的是,如果我改變身體的方法拋出別的東西,像Console.WriteLine,一切運行良好。
編輯:我已經上傳示例項目到我的SkyDrive:http://sdrv.ms/NumUbR
如果有人想在本地編譯它,看看它的表現一樣。
如果連接出現問題,單向操作仍可能失敗,等等,直到服務收到整個消息。另外,我相信託管服務並在同一線程上調用客戶端通道可能會產生一些有趣的結果。 – 2012-08-13 17:02:52
據我所知,該服務未託管在同一線程中。 WCF負責旋轉額外的線程來監視傳入呼叫等。調用ServiceHost。打開塊直到服務主機打開,然後繼續。 – 2012-08-13 17:33:27
另外,這裏的連接在同一臺機器上,我不確定這個小樣本中會出現什麼問題。 – 2012-08-13 17:33:54