<b> WCF后台服务代码
private static ILog log = new LogHelper("SeqService"); // 日志
private static Dictionary<int, DateTime> dic_thread = new Dictionary<int, DateTime>(); // 线程列表
private static long Num = 0; // 线程个数
private static object lock_Num = 0; // 共享数据-锁
/// <summary>
/// 在线申请流水号
/// </summary>
/// <returns></returns>
[WebGet(UriTemplate = "GetSeqNum/Json", ResponseFormat = WebMessageFormat.Json)]
public string GetSeqNumber()
{
lock (lock_Num)
{
Num++;
int id_thread = System.Threading.Thread.CurrentThread.ManagedThreadId;
DateTime now = DateTime.Now;
if (!dic_thread.TryGetValue(id_thread, out now))
{
dic_thread.Add(id_thread, DateTime.Now);
}
}
string ret = DateTime.Now.ToString("yyyyMMdd") + Num.ToString(new string('0', 9));
log.Info(string.Format("{0}, Thread={1}/{2}", ret, System.Threading.Thread.CurrentThread.ManagedThreadId, dic_thread.Count));
return ret;
}
5. 实验结果
1. 10000个WCF网络http请求,CPU分成每次10个(10可以按需求调整)线程并发执行,并且主程序在所有请求都执行完毕后,才退出主程序。

1. 前端日志:LogFileAdd_ThreadInfo

2. WCF日志:LogFileSeqServiceInfo

注:相关教程知识阅读请移步到c#教程频道。










