我有一个引用WCF服务的.Net Framework Web项目。
我正在将此Web项目移至.Net Standard。我添加了与连接服务相同的WCF服务。即使WCF服务相同,我也可以注意到2个主要区别:
好吧,我更改了所有代码以支持异步方法,但是我不确定我处理类的方式是否正确...这里的代码:
public class MyCustomClient : IMyCustomClient
{
private bool _disposed;
WSClient _client;
public MyCustomClient()
{
this._client = new WSClient(WSClient.EndpointConfiguration.WSClientSoap12);
}
public async Task<MyResult> DoWorkAsync(...)
{
...
MyResult something = await this._client.DoWork();
...
return something;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (this._disposed)
return;
if (disposing)
{
if (this._client.State != System.ServiceModel.CommunicationState.Closed &&
this._client.State != System.ServiceModel.CommunicationState.Closing)
this._client.CloseAsync(); //Here the doubt... The Close method is async, but the dispose not....
this._client = null;
}
this._disposed = true;
}
}
正如您在上面看到的,Close方法是异步的,但处理方式不是...。我的想法是,我应该根据Web项目收到的HTTP请求打开和关闭wcf客户端...处理包装客户的正确方法?
谢谢...