1.)我有一个主方法处理,它将字符串作为参数,并且该字符串包含一些 x 数量的任务。
2.) 我有另一种方法 Status,它通过使用两个变量 TotalTests 和 CurrentTest 来跟踪第一种方法。每次都会在第一种方法(处理)中循环修改。
3.) 当多个客户端并行调用我的 Web 服务以通过传递字符串来调用处理方法时,具有不同任务的处理将需要更多时间。因此,同时客户端将使用第二个线程调用 Web 服务中的 Status 方法来获取第一个方法的状态。
4.)当第 3 点完成时,所有客户端都应该并行获取变量(TotalTests,CurrentTest),而不会与其他客户端请求混淆。
5.) 当我将它们设置为静态时,我在下面提供的代码会使所有客户端的变量结果混合在一起。如果我删除了变量的静态变量,那么客户端只会得到这两个变量的全 0,我无法修复它。请看下面的代码。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
public int TotalTests = 0;
public int CurrentTest = 0;
public string Processing(string OriginalXmlString)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(OriginalXmlString);
this.TotalTests = XmlDoc.GetElementsByTagName("TestScenario").Count; //finding the count of total test scenarios in the given xml string
this.CurrentTest = 0;
while(i<10)
{
++this.CurrentTest;
i++;
}
}
public string Status()
{
return (this.TotalTests + ";" + this.CurrentTest);
}
}
服务器配置
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="true" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
客户端配置
<wsHttpBinding>
<binding name="WSHttpBinding_IService1" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="true" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
下面提到的是我的客户端代码
class Program
{
static void Main(string[] args)
{
Program prog = new Program();
Thread JavaClientCallThread = new Thread(new ThreadStart(prog.ClientCallThreadRun));
Thread JavaStatusCallThread = new Thread(new ThreadStart(prog.StatusCallThreadRun));
JavaClientCallThread.Start();
JavaStatusCallThread.Start();
}
public void ClientCallThreadRun()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\t72CalculateReasonableWithdrawal_Input.xml");
bool error = false;
Service1Client Client = new Service1Client();
string temp = Client.Processing(doc.OuterXml, ref error);
}
public void StatusCallThreadRun()
{
int i = 0;
Service1Client Client = new Service1Client();
string temp;
while (i < 10)
{
temp = Client.Status();
Thread.Sleep(1500);
Console.WriteLine("TotalTestScenarios;CurrentTestCase = {0}", temp);
i++;
}
}
}
任何人都可以帮忙。