2013-04-02 33 views
8

Làm thế nào để lớp trừu tượng System.ServiceModel.ClientBase có thể triển khai thực hiện giao diện IDisposable nếu khai báo phương thức Dispose() không hiển thị/khai báo?ClientBase không triển khai thành viên IDisposable

Nếu tôi cố gắng làm như vậy tôi nhận được một lỗi và không thể biên dịch

abstract class ATeste : IDisposable 
{ 
} 

'ATeste' không thực hiện thành viên giao diện 'System.IDisposable.Dispose()'

tôi m sử dụng VS 2010 và Framework 4.0.

Kiểm tra việc kê khai ClientBase:

// Summary: 
//  Provides the base implementation used to create Windows Communication Foundation 
//  (WCF) client objects that can call services. 
// 
// Type parameters: 
// TChannel: 
//  The channel to be used to connect to the service. 
public abstract class ClientBase<TChannel> : ICommunicationObject, IDisposable where TChannel : class 
{ 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class using the default target endpoint from the application configuration 
    //  file. 
    // 
    // Exceptions: 
    // System.InvalidOperationException: 
    //  Either there is no default endpoint information in the configuration file, 
    //  more than one endpoint in the file, or no configuration file. 
    protected ClientBase(); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class using the callbackInstance as the callback object in a duplex conversation. 
    // 
    // Parameters: 
    // callbackInstance: 
    //  The callback object that the client application uses to listen for messages 
    //  from the connected service. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The callback instance is null. 
    // 
    // System.InvalidOperationException: 
    //  Either there is no default endpoint information in the configuration file, 
    //  more than one endpoint in the file, or no configuration file. 
    protected ClientBase(InstanceContext callbackInstance); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class using the configuration information specified in the application configuration 
    //  file by endpointConfigurationName. 
    // 
    // Parameters: 
    // endpointConfigurationName: 
    //  The name of the endpoint in the application configuration file. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The specified endpoint information is null. 
    // 
    // System.InvalidOperationException: 
    //  The endpoint cannot be found or the endpoint contract is not valid. 
    protected ClientBase(string endpointConfigurationName); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class using the specified binding and target address. 
    // 
    // Parameters: 
    // binding: 
    //  The binding with which to make calls to the service. 
    // 
    // remoteAddress: 
    //  The address of the service endpoint. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The binding is null. 
    // 
    // System.ArgumentNullException: 
    //  The remote address is null. 
    protected ClientBase(Binding binding, EndpointAddress remoteAddress); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class using the specified callback service and endpoint configuration information. 
    // 
    // Parameters: 
    // callbackInstance: 
    //  The callback object that the client uses to listen for messages from the 
    //  connected service. 
    // 
    // endpointConfigurationName: 
    //  The name of the endpoint in the application configuration file. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The callback instance is null. 
    // 
    // System.ArgumentNullException: 
    //  The endpoint is null. 
    // 
    // System.InvalidOperationException: 
    //  The endpoint cannot be found or the endpoint contract is not valid. 
    protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class using the specified target address and endpoint information. 
    // 
    // Parameters: 
    // endpointConfigurationName: 
    //  The name of the endpoint in the application configuration file. 
    // 
    // remoteAddress: 
    //  The address of the service. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The endpoint is null. 
    // 
    // System.ArgumentNullException: 
    //  The remote address is null. 
    // 
    // System.InvalidOperationException: 
    //  The endpoint cannot be found or the endpoint contract is not valid. 
    protected ClientBase(string endpointConfigurationName, EndpointAddress remoteAddress); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class. 
    // 
    // Parameters: 
    // endpointConfigurationName: 
    //  The name of the endpoint in the application configuration file. 
    // 
    // remoteAddress: 
    //  The address of the service. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The endpoint is null. 
    // 
    // System.ArgumentNullException: 
    //  The remote address is null. 
    // 
    // System.InvalidOperationException: 
    //  The endpoint cannot be found or the endpoint contract is not valid. 
    protected ClientBase(string endpointConfigurationName, string remoteAddress); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class. 
    // 
    // Parameters: 
    // callbackInstance: 
    //  The callback service. 
    // 
    // binding: 
    //  The binding with which to call the service. 
    // 
    // remoteAddress: 
    //  The address of the service endpoint. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The callback instance is null. 
    // 
    // System.ArgumentNullException: 
    //  The binding is null. 
    // 
    // System.ArgumentNullException: 
    //  The remote address is null. 
    protected ClientBase(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class. 
    // 
    // Parameters: 
    // callbackInstance: 
    //  The callback object that the client uses to listen for messages from the 
    //  connected service. 
    // 
    // endpointConfigurationName: 
    //  The name of the endpoint in the application configuration file. 
    // 
    // remoteAddress: 
    //  The address of the service. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The callback instance is null. 
    // 
    // System.ArgumentNullException: 
    //  The endpoint is null. 
    // 
    // System.ArgumentNullException: 
    //  The remote address is null. 
    // 
    // System.InvalidOperationException: 
    //  The endpoint cannot be found or the endpoint contract is not valid. 
    protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress); 
    // 
    // Summary: 
    //  Initializes a new instance of the System.ServiceModel.ClientBase<TChannel> 
    //  class. 
    // 
    // Parameters: 
    // callbackInstance: 
    //  The callback object that the client uses to listen for messages from the 
    //  connected service. 
    // 
    // endpointConfigurationName: 
    //  The name of the endpoint in the application configuration file. 
    // 
    // remoteAddress: 
    //  The address of the service. 
    // 
    // Exceptions: 
    // System.ArgumentNullException: 
    //  The callback instance is null. 
    // 
    // System.ArgumentNullException: 
    //  The endpoint is null. 
    // 
    // System.ArgumentNullException: 
    //  The remote address is null. 
    // 
    // System.InvalidOperationException: 
    //  The endpoint cannot be found or the endpoint contract is not valid. 
    protected ClientBase(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress); 

    // Summary: 
    //  Gets the inner channel used to communicate with the service. 
    // 
    // Returns: 
    //  An implementation of the target service contract interface passed as the 
    //  type parameter to the constructor. 
    protected TChannel Channel { get; } 
    // 
    // Summary: 
    //  Gets the underlying System.ServiceModel.ChannelFactory<TChannel> object. 
    // 
    // Returns: 
    //  A System.ServiceModel.ChannelFactory<TChannel> object. 
    public ChannelFactory<TChannel> ChannelFactory { get; } 
    // 
    // Summary: 
    //  Gets the client credentials used to call an operation. 
    // 
    // Returns: 
    //  Returns a System.ServiceModel.Description.ClientCredentials that represents 
    //  the proof of identity presented by the client. 
    public ClientCredentials ClientCredentials { get; } 
    // 
    // Summary: 
    //  Gets the target endpoint for the service to which the WCF client can connect. 
    // 
    // Returns: 
    //  The target endpoint. 
    public ServiceEndpoint Endpoint { get; } 
    // 
    // Summary: 
    //  Gets the underlying System.ServiceModel.IClientChannel implementation. 
    // 
    // Returns: 
    //  The client channel for the WCF client object. 
    public IClientChannel InnerChannel { get; } 
    // 
    // Summary: 
    //  Gets the current state of the System.ServiceModel.ClientBase<TChannel> object. 
    // 
    // Returns: 
    //  The value of the System.ServiceModel.CommunicationState of the object. 
    public CommunicationState State { get; } 

    // Summary: 
    //  Causes the System.ServiceModel.ClientBase<TChannel> object to transition 
    //  immediately from its current state into the closed state. 
    public void Abort(); 
    // 
    // Summary: 
    //  Causes the System.ServiceModel.ClientBase<TChannel> object to transition 
    //  from its current state into the closed state. 
    public void Close(); 
    // 
    // Summary: 
    //  Returns a new channel to the service. 
    // 
    // Returns: 
    //  A channel of the type of the service contract. 
    protected virtual TChannel CreateChannel(); 
    // 
    // Summary: 
    //  Instructs the inner channel to display a user interface if one is required 
    //  to initialize the channel prior to using it. 
    public void DisplayInitializationUI(); 
    // 
    // Summary: 
    //  Replicates the behavior of the default keyword in C#. 
    // 
    // Type parameters: 
    // T: 
    // 
    // Returns: 
    //  Returns null if T is a reference type and zero if T is a numeric value type. 
    protected T GetDefaultValueForInitialization<T>(); 
    protected void InvokeAsync(ClientBase<TChannel>.BeginOperationDelegate beginOperationDelegate, object[] inValues, ClientBase<TChannel>.EndOperationDelegate endOperationDelegate, SendOrPostCallback operationCompletedCallback, object userState); 
    // 
    // Summary: 
    //  Causes the System.ServiceModel.ClientBase<TChannel> object to transition 
    //  from the created state into the opened state. 
    public void Open(); 

    // Summary: 
    //  A delegate that is used by System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object) 
    //  for calling asynchronous operations on the client. 
    // 
    // Parameters: 
    // inValues: 
    //  The input values to the asynchronous call. 
    // 
    // asyncCallback: 
    //  Reference to the method to be called when the corresponding asynchronous 
    //  operation completes. 
    // 
    // state: 
    //  An System.Object that lets the client distinguish between different asynchronous 
    //  calls. It is made available to the client in the arguments parameter of the 
    //  event completion callback. 
    // 
    // Returns: 
    //  The result of the asynchronous call. 
    protected delegate IAsyncResult BeginOperationDelegate(object[] inValues, AsyncCallback asyncCallback, object state); 

    // Summary: 
    //  A delegate that is invoked by System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object) 
    //  on successful completion of the call made by System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object) 
    //  to System.ServiceModel.ClientBase<TChannel>.BeginOperationDelegate. 
    // 
    // Parameters: 
    // result: 
    //  The result returned by the call made by System.ServiceModel.ClientBase<TChannel>.InvokeAsync(System.ServiceModel.ClientBase.BeginOperationDelegate,System.Object[],System.ServiceModel.ClientBase.EndOperationDelegate,System.Threading.SendOrPostCallback,System.Object)to 
    //  System.ServiceModel.ClientBase<TChannel>.BeginOperationDelegate. 
    // 
    // Returns: 
    //  An array of System.Object that contains the results of the call to the asynchronous 
    //  method. The operation may have multiple return values, which are all returned 
    //  in this object array. 
    protected delegate object[] EndOperationDelegate(IAsyncResult result); 

    // Summary: 
    //  Stores the results from an asynchronous call made by the client. 
    protected class InvokeAsyncCompletedEventArgs : AsyncCompletedEventArgs 
    { 
     // Summary: 
     //  Gets the results from an asynchronous call made by the client. 
     // 
     // Returns: 
     //  An array of System.Object that contains the results from an asynchronous 
     //  call made by the client. 
     public object[] Results { get; } 
    } 
} 

Tôi đã thử nghiệm một lần nữa, nhưng lần này explicity thực hiện IDisposable. Bây giờ nó có ý nghĩa

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

class Test : ATest 
{ 
} 

Bây giờ, tôi không thể truy cập các phương thức Dispose() khi tôi nhanh chóng Test Class:

 Test t = new Test(); 
     t.Dispose(); 
+0

Tôi không chắc chắn nơi mã bạn được đăng đến từ đâu, nhưng theo [tài liệu] (http: // MSDN. microsoft.com/en-us/library/bb340248.aspx) nó thực hiện 'IDisposable.Dispose' rõ ràng là –

+0

+1 cho một người nào đó cho thấy cách thực hiện' Dispose' một cách rõ ràng trong lớp 'ATeste' của anh ta. –

Trả lời

8

using explicit interface implementation.

IDisposable có thể nhìn thấy và có thể được gọi là

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

Tôi đã thử nghiệm nó ngay bây giờ. Bạn nói đúng, cảm ơn. Và xin lỗi vì câu hỏi noob của tôi. –

3

ClientBase<TChannel> cụ IDisposable sử dụng explicit interface implementation.

Việc thực hiện cho điều này chỉ gọi gần:

void IDisposable.Dispose() 
{ 
    this.Close(); 
} 
5

Như khá nhiều tất cả mọi người đã chỉ ra, các phương pháp giao diện được thực hiện một cách rõ ràng. Mã nguồn bạn đang xem cho ClientBase<TChannel>from Metadata (xem Metadata as Source trong Visual Studio). Lưu ý việc thiếu triển khai cho bất kỳ chức năng nào hoặc từ khóa partial.

Visual Studio sẽ không hiển thị được thực hiện một cách rõ ràng các thành viên giao diện from Metadata (xem Stack Overflow question 72686320)


Edit:

Một rõ ràng phương pháp giao diện thực hiện phải được gọi từ giao diện trực tiếp (đây là cách nó khác với cái đã được triển khai hoàn toàn). Các cách chính xác để gọi nó là như sau

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(); 
    } 
} 

Kết quả là Unhandled Exception: System.NotImplementedException: The requested feature is not implemented.

Các vấn đề liên quan