public
Last active

WCF ASync to Sql Test Service

  • Download Gist
AsyncSqlTestService.svc.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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);
}
}
}
TestClient.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.