我是 .net 的新手,对 WCF 知之甚少,所以如果有任何愚蠢的问题,请多多包涵。如果我的代码没有显式生成任何线程,我想知道 WCF 如何处理 SELF-HOST 场景中的同时调用。因此,在阅读了很多关于 stackoverflow 的内容后,我创建了一个测试应用程序,但它似乎无法正常工作。请指教。非常感谢。
请注意 ...
- 我的问题只是关于 WCF SELF HOSTING,所以请不要参考任何与 IIS 相关的内容。
- 我正在使用webHttpBinding。
- 我知道有 maxConnection 和服务限制设置,但我只对我的研究设置中的2 个同时调用感兴趣。所以不应该有最大 conn 或线程池问题。
- 我的测试服务没有使用会话。
代码如下...
namespace myApp
{
[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface ITestService
{
[OperationContract]
[WebGet(UriTemplate="test?id={id}")]
string Test(int id);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple)]
public class TestService : ITestService
{
private static ManualResetEvent done = new ManualResetEvent(false);
public string Test(int id)
{
if (id == 1)
{
done.Reset();
done.WaitOne();
}
else
{
done.Set();
}
}
}
}
应用程序配置...
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name = "TestEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name = "myApp.TestService">
<endpoint address = "" behaviorConfiguration="TestEndpointBehavior"
binding = "webHttpBinding"
contract = "myApp.ITestService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/test/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.web>
<sessionState mode = "Off" />
</system.web>
我是如何测试...
应用程序运行后,我打开浏览器,FF 以防万一,调用http://localhost:8080/test/test?id=1。该请求使应用程序暂停等待信号,即WaitOne。然后在另一个浏览器选项卡中再次调用http://localhost:8080/test/test?id=2。预期的是此请求将设置信号,因此服务器将为这两个请求返回。
但我看到应用程序挂起,第二个请求从未输入测试功能。所以显然我的代码不支持同时/并发调用。哪里不对了?