Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
WCF ASync to Sql Test Service
public class AsyncSqlTestService : IAsyncSqlTestService
{
public int DoStuffSynchronously(int value)
{
using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testDatabase"].ConnectionString))
{
conn.Open();
using (var tran = conn.BeginTransaction(IsolationLevel.Serializable))
{
using (var cmd = new SqlCommand("waitfor delay '00:00:08'", conn, tran))
{
try
{
return cmd.ExecuteNonQuery();
}
catch (SqlException se)
{
Debug.Write(se);
}
finally
{
conn.Close();
}
}
}
}
return 100;
}
public IAsyncResult BeginDoingStuff(int value, AsyncCallback callback, object state)
{
Debug.WriteLine(string.Format("begin called with {0} on thread {1}", value, Thread.CurrentThread.ManagedThreadId));
var result = new QueryAsyncResult(callback, state);
result.BeginExecution();
return result;
}
public int EndDoingStuff(IAsyncResult result)
{
var asyncResult = result as QueryAsyncResult;
Debug.WriteLine(string.Format("end called with {0} on thread {1}", asyncResult.Data, Thread.CurrentThread.ManagedThreadId));
return asyncResult.Data;
}
public ThreadCount GetAvailableThreads()
{
int workerThreads = 0, ioThreads = 0;
ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);
return new ThreadCount { IOThreads = ioThreads, WorkerThreads = workerThreads };
}
}
class QueryAsyncResult : IAsyncResult
{
int data;
private AsyncCallback _callback;
private object _state;
public QueryAsyncResult(AsyncCallback callback, object state)
{
_callback = callback;
_state = state;
}
public int Data
{ get { return data; } }
#region IAsyncResult Members
public object AsyncState
{ get { return _state; } }
public WaitHandle AsyncWaitHandle
{ get { throw new Exception("The method or operation is not implemented."); } }
public bool CompletedSynchronously
{ get { return true; } }
public bool IsCompleted
{ get { return true; } }
#endregion
public void BeginExecution()
{
SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=IE;user id=sa;password=Password123;Asynchronous Processing=true");
conn.Open();
SqlCommand cmd = new SqlCommand("waitfor delay '00:00:08'", conn);
IAsyncResult result = cmd.BeginExecuteNonQuery(new AsyncCallback(OnSqlExecute), cmd);
if (result.CompletedSynchronously)
{
Debug.Fail("gone sync");
}
}
public void OnSqlExecute(IAsyncResult ar)
{
Debug.Write(string.Format("End exec from thread {0}", Thread.CurrentThread.ManagedThreadId));
SqlCommand cmd = (SqlCommand)ar.AsyncState;
data = (int)cmd.EndExecuteNonQuery(ar);
if (cmd.Connection.State.Equals(ConnectionState.Open))
cmd.Connection.Close();
if (_callback != null)
{
_callback(this);
}
}
}
class Program
{
private const int threadCount = 8;
public static void ExecuteACallAgainstTheService()
{
var proxy = new AsyncSqlTestServiceClient();
try
{
int result = proxy.DoingStuff(0);
//int result = proxy.DoStuffSynchronously(0);
Console.WriteLine("Result {0} in thread {1}", result, Thread.CurrentThread.ManagedThreadId);
}
finally
{
try
{
proxy.Close();
}
catch
{
proxy.Abort();
}
}
}
private static void OnDoingStuff(IAsyncResult ar)
{
Console.WriteLine(ar.AsyncState);
}
public static void MonitorThreadPool()
{
while (true)
{
using (AsyncSqlTestServiceClient proxy = new AsyncSqlTestServiceClient())
{
try
{
var threadCnt = proxy.GetAvailableThreads();
Console.WriteLine("Wrk {0} io {1}", threadCnt.WorkerThreads, threadCnt.IOThreads);
}
finally
{
try
{
proxy.Close();
}
catch
{
proxy.Abort();
}
}
}
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
StartThreadPoolMonitor();
Thread.Sleep(4000);
RunTestAgainstService();
Console.ReadLine();
}
private static void RunTestAgainstService()
{
Thread[] threads = new Thread[threadCount];
InitialiseWorkers(threadCount, threads);
StartWorkers(threads);
JoinWorkers(threads);
}
private static void StartThreadPoolMonitor()
{
Thread t = new Thread(MonitorThreadPool);
t.Start();
}
private static void JoinWorkers(Thread[] threads)
{
foreach (var thread in threads)
{
thread.Join();
}
}
private static void StartWorkers(Thread[] threads)
{
foreach (var thread in threads)
{
thread.Start();
}
}
private static void InitialiseWorkers(int threadCount, Thread[] threads)
{
for (int i = 0; i < threadCount; i++)
{
threads[i] = new Thread(ExecuteACallAgainstTheService);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.