簡體   English   中英

單元測試服務和模擬自動生成的WCF客戶端代理

[英]Unit test Service and Mock Autogenerated WCF Client proxy

我在服務中引用自動生成的WCF客戶端。

//Autogenerated Service client
public partial class ServiceClient : 
    System.ServiceModel.ClientBase<IService>, IService 
{
    //...
}
//Autogenerated Interface Client
public interface IService {
    //...
}

以下列方式:

public MyService{

    public IExternalWsClientFactory ExternalWsClientFactory {get; set; }

    public void MyMethod(){
        using (var wsCliente = ExternalWsClientFactory.ServiceClient())
        { 
            //... 
        }
    }
}

public class ExternalWsClientFactory : IExternalWsClientFactory
{
    public ServiceClient ServiceClient()
    {
        var wsClient = new ServiceClient();
        return wsClient;
    }
}

我引用了實現,因為我想使用using語句來處理代碼塊末尾的資源。 並且因為IDisposableClientBase下,並且界面不是部分的。

我的問題是我想模擬ServiceClient (我已經模擬了外部WsClientFactory )但是因為我使用了實現,所以我很難做到這一點。

注意:實現中自動生成的方法ServiceClient不是virtual

這個班是偏袒的。 界面不是。

創建自己的接口,該接口派生自原始接口,並使用IDisposable擴展。

public interface IServiceClient: ICommunicationObject, IService, IDisposable { }

使用自定義界面擴展分部類

public partial class ServiceClient : IServiceClient { }

現在你應該可以使用擴展接口和using語句了

public class ExternalWsClientFactory : IExternalWsClientFactory {
    public IServiceClient ServiceClient() {
        var wsClient = new ServiceClient();
        return wsClient;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM