ОК, я наконец-то получил все это!
Сначала: вам нужен собственный класс AsyncResult
class AsyncResult : IAsyncResult , IDisposable
{
object _state;
ManualResetEvent _waitHandle = new ManualResetEvent(false);
bool _isCompleted;
#region IAsyncResult Members
public object AsyncState
{
get { return _state; }
}
public System.Threading.WaitHandle AsyncWaitHandle
{
get { return _waitHandle; }
}
public bool CompletedSynchronously
{
get { return false; }
}
public bool IsCompleted
{
get { return _isCompleted; }
}
#endregion
Exception _exception;
internal Exception Exception
{
get { return _exception; }
}
Accounts.Account _result;
internal Accounts.Account Result
{
get { return _result; }
}
internal AsyncResult(PortSet<Accounts.Account, Exception> port, DispatcherQueue queue, AsyncCallback callback, object state)
{
_state = state;
Arbiter.Activate(queue,
Arbiter.Choice(port,
r =>
{
_result = r;
Complete(callback);
},
e =>
{
_exception = e;
Complete(callback);
}
)
);
}
private void Complete(AsyncCallback callback)
{
_isCompleted = true;
_waitHandle.Set();
if (callback != null)
{
ThreadPool.QueueUserWorkItem(s => callback(this));
}
}
private bool disposedValue = false;
public void Dispose()
{
if (!this.disposedValue)
{
_waitHandle.Close();
_waitHandle = null;
_state = null;
}
this.disposedValue = true;
}
}
Хорошо, тогда нам нужно связать это с вызовами метода Async WCF:
public class Service : IService
{
private Dispatcher dispatcher;
private DispatcherQueue dq;
public Service()
{
dispatcher = new Dispatcher();
dq = new DispatcherQueue("CCR DispatcherQueue", dispatcher);
}
public IAsyncResult BeginGetAccount(int id, AsyncCallback callback, object state)
{
PortSet<Accounts.Account, Exception> port = new PortSet<Accounts.Account, Exception>();
Accounts.Manager manager = new Accounts.Manager();
manager.GetAccountData(dq, port, id);
AsyncResult result = new AsyncResult(port, dq, callback, state);
return result;
}
public string EndGetAccount(IAsyncResult result)
{
{
var AccountName = string.Empty;
if ((result != null))
{
using (Common.AsyncResult asyncResult = result as Common.AsyncResult)
{
if (asyncResult == null)
{
throw new NullReferenceException("IAsynchResult Parameter is Null");
}
asyncResult.AsyncWaitHandle.WaitOne();
if (asyncResult.Result != null)
{
AccountName = asyncResult.Result.Name;
}
}
}
return AccountName;
}
}
}
Тогда вам просто нужен метод IEnumerator, чтобы отправить ответ на порт