我已经做了相当多的搜索,但却找不到解决方案或答案.我有一个我写的测试应用程序来显示"问题".(我对这就是WCF工作的方式持开放态度,我无能为力.)
我的测试应用程序是一个简单的WCF服务和客户端与Visual Studio生成的代理.我将代理指向一个已启动但未运行该服务的远程计算机.这一点很重要.如果远程计算机根本没有运行,则WCF调用立即失败.
客户端应用程序创建客户端代理类的实例,启动新任务,然后尝试在服务接口上进行调用,这将阻止,因为另一端的服务不存在.后台任务休眠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}", …Run Code Online (Sandbox Code Playgroud)