【发布时间】:2010-07-20 22:56:39
【问题描述】:
我开发了一个概念验证应用程序,用于查询 WCF 是否支持多线程。
现在,我所做的只是创建一个标有
的服务合同 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = true)]
使用两个操作来获取固定文本。
第一种方法Thread.Sleep 8 秒延迟响应,另一种直接返回数据。
我遇到的问题是,当我运行两个客户端应用程序实例并从第一个客户端请求延迟的方法并从第二个客户端请求另一个方法时,我得到了顺序响应。
当服务忙于处理另一个请求时,如何从服务获取响应?
namespace WCFSyncService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)],
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = true)]
public class ServiceImplementation : IService
{
public ServiceImplementation()
{
}
#region IService Members
public string GetDelayedResponse()
{
System.Threading.Thread.Sleep(8000);
return "Slow";
}
public string GetDirectResponse()
{
return "Fast";
}
#endregion
}
}
我需要同时调用GetDelayedResponse和GetDirectResponse方法,并在8秒结束前得到“快速”文本。
托管应用程序代码
namespace ServiceHostApplication
{
public partial class frmMain : Form
{
private WCFSessionServer.IService oService;
public frmMain()
{
InitializeComponent();
}
private void btnStartService_Click(object sender, EventArgs e)
{
ServiceHost objSvcHost;
oService = new WCFSessionServer.ServiceImplementation();
objSvcHost = new ServiceHost( typeof(WCFSessionServer.ServiceImplementation));
objSvcHost.Open();
}
}
}
下面是我实现它来测试案例的代码:
服务器端类,
-
服务接口
namespace WCFSessionServer { [ServiceContract] public interface IService { [OperationContract] string GetDelayedResponse(); [OperationContract] string GetDirectResponse(); } } -
实现类
namespace WCFSessionServer { [ServiceBehavior( InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = true)] public class ServiceImplementation : IService { public ServiceImplementation() { } #region Service Members public string GetDelayedResponse() { System.Threading.Thread.Sleep(8000); return "Slow"; } public string GetDirectResponse() { return "Fast"; } #endregion } } -
服务器端 app.config
<system.serviceModel> <services> <service behaviorConfiguration = "WCFSessionServer.IService" name = "WCFSessionServer.ServiceImplementation" > <endpoint address="http://localhost:2020/SessionService/basic/" behaviorConfiguration="WCFSessionServer.IService" binding="basicHttpBinding" name="BasicHttpBinding_IService" bindingName="myBasicHttpBinding" contract="WCFSessionServer.IService" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:2020/SessionService/" /> </baseAddresses> </host> </service> </services> <behaviors> <endpointBehaviors> <behavior name="TimeOut"> <callbackTimeouts transactionTimeout="00:00:02"/> </behavior> <behavior name="WCFSessionServer.IService" > <dataContractSerializer maxItemsInObjectGraph="2147483647" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="WCFSessionServer.IService"> <serviceThrottling maxConcurrentCalls="10" maxConcurrentSessions="10" maxConcurrentInstances="10"/> <dataContractSerializer maxItemsInObjectGraph="2147483647" /> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="True" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
客户端app.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None">
<transport
clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message
clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2020/SessionService/basic/"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService"
contract="SessionServiceProxy.IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>
【问题讨论】:
-
请停止大喊大叫!
标签: wcf