1.) 我有一个 main 方法 Processing,它将字符串作为参数,该字符串包含一些 x 个任务。
2.) 我有另一个方法 状态,它通过使用两个变量 TotalTests 和 CurrentTest 来跟踪第一个方法。每次都会在第一种方法(处理)中循环修改。
3.) 当多个客户端并行调用我的 Web 服务以通过传递字符串来调用 Processing 方法时,具有不同任务的字符串将花费更多时间来处理。因此,当客户端将使用第二个线程调用 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++;
}
}
}
任何人都可以帮忙。
首先因为需要同时访问服务,所以当服务正在处理第一个客户端调用(Processing)时,需要将服务并发模式更改为 Multiple。
此外,您希望维护每个客户端处理状态,因此您需要将实例上下文模式设置为 PerSession。
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode= InstanceContextMode.PerSession)]
注意
- 默认实例上下文模式为 PerSession
- 默认并发模式为"单个
"
您可以执行以下操作以确保您的配置与 PerSession 实例上下文模式兼容,使用此方法,如有必要,WCF 将引发运行时异常
[ServiceContract(SessionMode=SessionMode.Required)]
注意 使用 InstanceContextMode.PerSession 时,您将在创建的每个代理中获得不同的实例
因此,每个客户端只需要一个"Service1Client"实例,您将调用其 Process 方法并从中检索状态。
同样对于虚拟繁重处理,您可以使用 Thread.Sleep(毫秒)仅在"处理"方法(服务端)中进行测试建议。
对于客户端应用程序,如果要调用"处理"方法,然后使用 Status 方法检索状态,则需要异步调用 Process 方法。
1.右键单击解决方案资源管理器中的服务引用并选择"配置服务引用",然后选中"生成异步操作"并按确定。
2.像这样更改客户端代码
static void Main(string[] args)
{
StartProcessing();
StatusReport();
Console.ReadLine();
}
static ServiceClient Client = new ServiceClient();
private static bool Completed = false;
public static void StartProcessing()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"D:t72CalculateReasonableWithdrawal_Input.xml");
bool error = false;
Client.ProcessingCompleted += Client_ProcessingCompleted;
Client.ProcessingAsync(doc.OuterXml);
Console.WriteLine("Processing...");
}
static void Client_ProcessingCompleted(object sender, ProcessingCompletedEventArgs e)
{
// processing is completed, retreive the return value of Processing operation
Completed = true;
Console.WriteLine(e.Result);
}
public static void StatusReport()
{
int i = 0;
string temp;
while (!Completed)
{
temp = Client.Status();
Console.WriteLine("TotalTestScenarios;CurrentTestCase = {0}", temp);
Thread.Sleep(500);
i++;
}
}
PerSession
不会使您的静态变量不在对象实例之间共享。PerSession
上下文模式唯一做的事情就是控制对象生存期。
使用 PerSession
WCF 在会话结束之前不会销毁服务对象。会话可以由客户端显式关闭,也可以按超时显式关闭(默认值为 10 分钟)。WCF 将来自具有相同会话 ID 的客户端的每个下一次调用路由到现有对象。
变量不应是静态的,以防止在不同服务实例之间共享。只要您使用维护会话的InstanceContextMode.PerSession
和绑定,WCF 就会维护变量的状态。
public int TotalTests = 0;
public int CurrentTest = 0;
我还会在合同中添加SessionMode.Required
以确保正确配置服务。
[ServiceContract(SessionMode = SessionMode.Required )]