Ho sviluppato un'applicazione proof of concept che interroga se WCF supporta il multi-threading.WCF supporta il multi-threading stesso?
Ora, tutto quello che ho fatto è la creazione di un contratto di servizio contrassegnato da
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = true)]
con due operazioni per ottenere testi fissi. Il primo metodo esegue uno Thread.Sleep
per 8 secondi per ritardare la risposta e l'altro per restituire i dati direttamente.
Il problema che ho dovuto affrontare è quando eseguo due istanze di applicazione client e richiedo dal primo client il metodo con ritardo e richiedo l'altro metodo dal secondo client, ho ottenuto una risposta sequenziale.
Come posso ottenere la risposta dal servizio mentre il servizio è occupato con un'altra richiesta?
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
}
}
ho bisogno di chiamare i metodi GetDelayedResponse
e GetDirectResponse
allo stesso tempo e ottenere il testo "veloce" prima della fine del 8 secondi.
Hosting codice dell'applicazione
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();
}
}
}
seguito è riportato il codice di implemento per testare il caso: le classi laterali
Server,
servizio di interfaccia
namespace WCFSessionServer { [ServiceContract] public interface IService { [OperationContract] string GetDelayedResponse(); [OperationContract] string GetDirectResponse(); } }
classe di implementazione
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 } }
lato server 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>
lato client 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>
STOP YELLING, PER FAVORE! –