110,566
社区成员
发帖
与我相关
我的任务
分享
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
UseSynchronizationContext = false,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class TestService : ITestService
{
static int count = 0;
public void DoWork(String id, int millisecondsSleep)
{
count++;
Console.WriteLine("DoWork [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "] Start :" + id + " count:" + count);
Thread.Sleep(millisecondsSleep); //模式服务器执行长时间操作,由客户端传入
Console.WriteLine("DoWork [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "] END :" + id + " count:" + count);
}
}
<?xml version="1.0"?>
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100"/>
</connectionManagement>
</system.net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
<service name="WCF.TestService">
<endpoint address="http://localhost:8009/TestService" binding="basicHttpBinding" name="webwcfService" contract="WCF.ITestService"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8009/TestService"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("====客户端准备中:按任意键开始测试...");
Console.ReadLine();
ThreadPool.SetMinThreads(100, 100);
for (int i = 0; i < 100; i++)
{
System.Threading.ThreadPool.QueueUserWorkItem(DoWork, Convert.ToString(i));
}
Console.ReadLine();
}
static void DoWork(Object key)
{
try
{
Console.WriteLine("DoWork [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "] Start :" + key);
TestServiceClient client = new TestServiceClient();
client.DoWork(Convert.ToString(key), 8 * 1000);
//client.DoWork(Convert.ToString(valuei), 10);
client.Close();
Console.WriteLine("DoWork [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "] END:" + key);
}
catch (Exception ex)
{
Console.WriteLine("=============================DoWork [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "] ERROR:" + key + "," + ex.Message);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="webwcfService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8009/TestService" binding="basicHttpBinding"
bindingConfiguration="webwcfService" contract="TestServiceReference.ITestService"
name="webwcfService" />
</client>
</system.serviceModel>
</configuration>