gpt4 book ai didi

c# - 如何实现接口(interface)返回带有空方法体的 IAsyncResult

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:21 25 4
gpt4 key购买 nike

如果我想继承 ChannelBase,WCF 需要我实现 IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) 等方法。因为当 channel 打开时我无事可做,所以我宁愿将该方法主体留空。在这些空方法中,我应该返回什么作为 IAsyncResult

最佳答案

通过将 IAsyncResult 实现添加为已完成的异步结果来解决

internal class CompletedAsyncResult : IAsyncResult
{
public CompletedAsyncResult(object state)
{
this.AsyncState = state;
}

public object AsyncState { get; set; }

public WaitHandle AsyncWaitHandle => new ManualResetEvent(true);

public bool CompletedSynchronously => true;

public bool IsCompleted => true;
}

像这样使用

protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
var result = new CompletedAsyncResult(state);
callback?.Invoke(result);
return result;
}

关于c# - 如何实现接口(interface)返回带有空方法体的 IAsyncResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54342202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com