|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AsyncServiceLibrary
{
public static class WcfAsyncHelper
{
public static bool IsSync<TState>(IAsyncResult result)
{
return result is CompletedAsyncResult<TState>;
}
public static CustomAsyncResult<TState> BeginAsync<TState>(
TState state,
AsyncCallback wcfCallback, object wcfState,
Func<TState, AsyncCallback, object, IAsyncResult> beginCall)
{
var customState = new CustomState<TState>(wcfCallback, wcfState, state);
var externalServiceCallback = new AsyncCallback(CallbackFromExternalService<TState>);
var externalServiceResult = beginCall(state, externalServiceCallback, customState);
return new CustomAsyncResult<TState>(externalServiceResult, customState);
}
public static CompletedAsyncResult<TState> BeginSync<TState>(
TState state,
AsyncCallback wcfCallback, object wcfState)
{
//var customState = new CustomState<TState>(wcfCallback, wcfState, state);
var completedResult = new CompletedAsyncResult<TState>(state, wcfState);
wcfCallback(completedResult);
return completedResult;
}
private static void CallbackFromExternalService<TState>(IAsyncResult serviceResult)
{
var serviceState = serviceResult.AsyncState as CustomState<TState>;
serviceState.WcfCallback(
new CustomAsyncResult<TState>(serviceResult, serviceState));
}
public static TResult EndAsync<TState, TResult>(IAsyncResult result,
Func<TState, IAsyncResult, TResult> endCall,
Action<Exception, TState> onException,
Action<TState> dispose)
{
var myAsyncResult = result as CustomAsyncResult<TState>;
var myState = myAsyncResult.CustomState;
try
{
return endCall(myState.State, myAsyncResult.ExternalAsyncResult);
}
catch (Exception x)
{
onException(x, myState.State);
return default(TResult);
}
finally
{
try
{
dispose(myState.State);
}
finally
{
}
}
}
public static TResult EndSync<TState, TResult>(IAsyncResult result,
Func<TState, IAsyncResult, TResult> endCall,
Action<Exception, TState> onException,
Action<TState> dispose)
{
var myAsyncResult = result as CompletedAsyncResult<TState>;
var myState = myAsyncResult.Data;
try
{
return endCall(myState, myAsyncResult);
}
catch (Exception x)
{
onException(x, myState);
return default(TResult);
}
finally
{
try
{
dispose(myState);
}
finally
{
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.