如何中断因远程服务未运行而在连接状态下被阻止的WCF代理



我已经做了大量的搜索,但一直未能找到解决方案或答案。我写了一个测试应用程序来显示"问题"。(我对这只是WCF的工作方式持开放态度,对此我无能为力。)

我的测试应用程序是一个简单的WCF服务和客户端,带有Visual Studio生成的代理。我将代理指向一台已启动但未运行服务的远程机器。这一点很重要。如果远程计算机根本没有运行,WCF调用将立即失败。

客户端应用程序创建客户端代理类的一个实例,启动一个新的Task,然后尝试对服务接口进行调用,该调用将被阻止,因为另一端的服务不存在。后台任务睡眠5秒钟,然后关闭代理。我本以为另一个线程在关闭代理时会立即出错,但事实并非如此。在发出初始呼叫大约20秒后,它就会出现故障。

这是我的客户代码:

using System;
using System.ServiceModel;
using System.Threading;
using System.Threading.Tasks;
using TestConsoleClient.MyTestServiceReference;
namespace TestConsoleClient
{
class Program
{
static void Main(string[] args)
{
MyTestServiceClient client = new MyTestServiceClient();
// Start new thread and wait a little bit before trying to close connection.
Task.Factory.StartNew(() =>
{
LogMessage("Sleeping for 5 seconds ...");
Thread.Sleep(5000);
if (client == null)
{
LogMessage("Woke up!  Proxy was closed before waking up.");
return;
}
LogMessage("Woke up!  Attempting to abort connection.");
LogMessage(string.Format("Channel state before Close: {0}", 
((ICommunicationObject)client).State));
client.Close();
((IDisposable)client).Dispose();
client.Abort();
// Channel state is Closed, as expected, but blocked thread does not wake up?
LogMessage(string.Format("Channel state after Abort: {0}",
((ICommunicationObject)client).State));
client = null;
});
// Start connecting.
LogMessage("Starting connection ...");
try
{
LogMessage("Calling MyTestService.DoWork()");
client.DoWork();  // Timeout is 60 seconds.  State is "Connecting"
}
catch (Exception ex)
{
LogMessage(string.Format("Exception caught: {0}", ex.Message));
if (client != null)
{
client.Abort();
((IDisposable) client).Dispose();
client = null;
}
}
finally
{
if (client != null)
{
client.Close();
client = null;
}
}
LogMessage("Press Enter to exit ...");
Console.ReadLine();
}
private static void LogMessage(string msg)
{
Console.WriteLine("{0}: [{1}] {2}",
DateTime.Now.ToString("hh:mm:ss tt"),   Thread.CurrentThread.ManagedThreadId, msg);
}
}
}

这是我的程序的输出:

01:48:31 PM: [9] Starting connection ...
01:48:31 PM: [9] Calling MyTestService.DoWork()
01:48:31 PM: [10] Sleeping for 5 seconds ...
01:48:36 PM: [10] Woke up!  Attempting to abort connection.
01:48:36 PM: [10] Channel state before Close: Opening
01:48:36 PM: [10] Channel state after Abort: Closed
01:48:54 PM: [9] Exception caught: Could not connect to net.tcp://th-niconevm:80
80/MyTestService. The connection attempt lasted for a time span of 00:00:22.0573
009. TCP error code 10060: A connection attempt failed because the connected par
ty did not properly respond after a period of time, or established connection fa
iled because connected host has failed to respond 10.10.10.10:8080.
01:48:54 PM: [9] Press Enter to exit ...

我试图实现的是让用户中断连接,这样他们就可以在必要时进行调整,然后再试一次。正如您在输出中看到的,通道随后从"打开"状态变为"关闭"状态,但服务调用一直被阻止,直到超时。我当然可以绕过它,但似乎应该有一种方法来打断它

这是客户端app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IMyTestService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://remotehost:8080/MyTestService" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IMyTestService" contract="MyTestServiceReference.IMyTestService"
name="NetTcpBinding_IMyTestService">
<identity>
<userPrincipalName value="remotehostClientUser" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

如果使用异步操作生成代理类,那么代码可能会变得更加简单。

// Asynchronously call DoWork
MyTestServiceClient client = new MyTestServiceClient();
IAsyncResult iar = client.BeginDoWork(null, null);
// Sleep, dance, do the shopping, whatever
Thread.Sleep(5000);
bool requestCompletedInFiveSeconds = iar.IsCompleted;
// When you're ready, remember to call EndDoWork to tidy up
client.EndDoWork(iar);
client.Close();

如果这听起来可能有帮助,请阅读异步模式。这个MSKB非常有用,它展示了几种编写异步代码的方法。

实际上,您的问题可能是无法取消正在进行的WCF操作;您必须为此编写自己的取消机制。例如,您将无法判断请求是否真的在进行中(即:DoWork执行需要中止),或者某个网络或其他机器延迟是否是阻碍。

最新更新