ClientBase doesn't implement IDisposable member

房东的猫 提交于 2019-12-04 02:57:04

using explicit interface implementation.

IDisposable is visible and can be invoked as

var client = new WCFTestServiceClient(); // assumingWCFTestServiceClient is WCF client proxy that inherits from ClientBase
(client as IDisposable).Dispose();
jerry

As pretty much everyone has pointed out, the interface method is explicitly implemented. The source code you are seeing for ClientBase<TChannel> is from Metadata (see Metadata as Source in Visual Studio). Note the lack of implementation for any function or a partial keyword.

Visual Studio will not show explicitly implemented interface members from Metadata (see Stack Overflow question 72686320)


Edit:

An explicitly implemented interface method must be called from the interface directly (this is how it differs from one that was implemented implicitly). The correct way to call it is as follows

using System;

abstract class ATest : IDisposable
{
    void IDisposable.Dispose()
    {
        throw new NotImplementedException();
    }
}

class Test : ATest
{
}

class OtherClass
{
    public static void Main()
    {
        Test t = new Test();
        ((IDisposable)t).Dispose();
    }
}

The result is Unhandled Exception: System.NotImplementedException: The requested feature is not implemented.

ClientBase<TChannel> implements IDisposable using explicit interface implementation.

The implementation for this just calls close:

void IDisposable.Dispose()
{
    this.Close();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!