使用 Single InstanceContextMode 在 WCF 服务上调用异步方法
Call asynchronous method on a WCF service with Single InstanceContextMode
我正在尝试在具有单个 instanceContextMode 的 WCF 上异步调用一个方法。
有没有办法在等待异步方法时重用服务实例?我使用任务方式在我的 WCF 服务引用上生成异步操作。
我做了一个测试项目,因为我的应用程序有一些问题。
我的 TestService 公开了 2 个方法:
- 一个应该被同步调用的快速方法
- 应该异步调用的长方法
出于其他一些原因,我的服务应该处于单实例上下文模式:
[ServiceContract]
public interface ITestService
{
[OperationContract]
string FastMethod(string name);
[OperationContract]
Task<string> LongMethodAsync(string name);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class TestService : ITestService
{
public TestService() { }
public string FastMethod(string name)
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - FastMethod call - {name}");
return $"FastMethod - {name}";
}
public async Task<string> LongMethodAsync(string name)
{
for (int i = 5; i > 0; i--)
{
await Task.Delay(1000);
Console.WriteLine($"LongMethod pending {i}");
}
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - LongMethod call - {name}");
return $"LongMethod - {name}";
}
}
我的主机是一个简单的控制台应用程序,它允许我通过 Console.WriteLine() 方法查看 WS 调用:
class Program
{
static void Main(string[] args)
{
using (ServiceHost hostTest = new ServiceHost(typeof(TestService)))
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - Service starting...");
hostTest.Open();
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - Service started");
Console.ReadKey();
hostTest.Close();
}
}
}
在我的客户端,我只有一个显示调用结果的简单表单:
private async void button1_Click(object sender, EventArgs e)
{
string result;
result = srvClient.FastMethod("test1");
resultTextBox.Text = $"{DateTime.Now.ToLongTimeString()} - {result}";
Task<string> t1 = srvClient.LongMethodAsync("test2");
result = srvClient.FastMethod("test3");
resultTextBox.Text += $"\r\n{DateTime.Now.ToLongTimeString()} - {result}";
System.Threading.Thread.Sleep(1000);
result = srvClient.FastMethod("test4");
resultTextBox.Text += $"\r\n{DateTime.Now.ToLongTimeString()} - {result}";
result = await t1;
resultTextBox.Text += $"\r\n{DateTime.Now.ToLongTimeString()} - {result}";
}
当我这样做时,我可以在 resultTestBox
和主机控制台中看到 "test3" 和 "test4" 仅在 "test2" 结束后调用。
如果我在本地(不是通过 WCF 服务)进行相同的测试,行为就像预期的那样,"test3" 和 "test4" 在 "test2" 等待时被调用。
If the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to ConcurrencyMode.
(好像忘记说ConcurrencyMode是什么了)
所以只需在您的服务上设置正确的ConcurrencyMode
:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
不过请确保您的代码是无状态且线程安全的。这种组合很容易出错。
我正在尝试在具有单个 instanceContextMode 的 WCF 上异步调用一个方法。
有没有办法在等待异步方法时重用服务实例?我使用任务方式在我的 WCF 服务引用上生成异步操作。
我做了一个测试项目,因为我的应用程序有一些问题。 我的 TestService 公开了 2 个方法:
- - 一个应该被同步调用的快速方法
- - 应该异步调用的长方法
出于其他一些原因,我的服务应该处于单实例上下文模式:
[ServiceContract]
public interface ITestService
{
[OperationContract]
string FastMethod(string name);
[OperationContract]
Task<string> LongMethodAsync(string name);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class TestService : ITestService
{
public TestService() { }
public string FastMethod(string name)
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - FastMethod call - {name}");
return $"FastMethod - {name}";
}
public async Task<string> LongMethodAsync(string name)
{
for (int i = 5; i > 0; i--)
{
await Task.Delay(1000);
Console.WriteLine($"LongMethod pending {i}");
}
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - LongMethod call - {name}");
return $"LongMethod - {name}";
}
}
我的主机是一个简单的控制台应用程序,它允许我通过 Console.WriteLine() 方法查看 WS 调用:
class Program
{
static void Main(string[] args)
{
using (ServiceHost hostTest = new ServiceHost(typeof(TestService)))
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - Service starting...");
hostTest.Open();
Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - Service started");
Console.ReadKey();
hostTest.Close();
}
}
}
在我的客户端,我只有一个显示调用结果的简单表单:
private async void button1_Click(object sender, EventArgs e)
{
string result;
result = srvClient.FastMethod("test1");
resultTextBox.Text = $"{DateTime.Now.ToLongTimeString()} - {result}";
Task<string> t1 = srvClient.LongMethodAsync("test2");
result = srvClient.FastMethod("test3");
resultTextBox.Text += $"\r\n{DateTime.Now.ToLongTimeString()} - {result}";
System.Threading.Thread.Sleep(1000);
result = srvClient.FastMethod("test4");
resultTextBox.Text += $"\r\n{DateTime.Now.ToLongTimeString()} - {result}";
result = await t1;
resultTextBox.Text += $"\r\n{DateTime.Now.ToLongTimeString()} - {result}";
}
当我这样做时,我可以在 resultTestBox
和主机控制台中看到 "test3" 和 "test4" 仅在 "test2" 结束后调用。
如果我在本地(不是通过 WCF 服务)进行相同的测试,行为就像预期的那样,"test3" 和 "test4" 在 "test2" 等待时被调用。
If the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to ConcurrencyMode.
(好像忘记说ConcurrencyMode是什么了)
所以只需在您的服务上设置正确的ConcurrencyMode
:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
不过请确保您的代码是无状态且线程安全的。这种组合很容易出错。