我已經邁出了使用WCF開發WINDOWS服務託管計算引擎的第一步。該服務工作的很好,但它似乎只使用一個線程爲我所有的電話,但我需要它是可擴展的,並使用多線程。自託管的WCF服務:ConcurrencyMode.Multiple但僅使用一個線程?
忽略代碼中的所有跟蹤等,因爲它是這個應用程序的非常早期的階段。
它調用一些在服務啓動時將信息讀入內存的第三方軟件(ThirdParty.Initialise(strInit)),然後每次調用ProcessInformation都會返回給定XML字符串的計算結果。
到目前爲止,無論我設置ServiceBehavior atts以及無論使用多少獨立的消費者,所有調用都只使用一個線程 - 任何人都可以提供幫助嗎?
代碼概述:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall, UseSynchronizationContext = false)]
public class TestWCFService : ITestWCFService
{
private static ThirdPartyLib.ThirdPartyComClass ThirdParty;
private static bool initCalled = false;
private static int cntInit = 0;
private static int cntTrans = 0;
public TestWCFService()
{
if (ThirdParty == null)
{
ThirdParty = new ThirdPartyLib.ThirdPartyComClass();
}
}
public bool InitialiseThirdParty(string strInit, out string strError)
{
try
{
if (!initCalled)
{
cntInit++;
ThirdParty.Initialise(strInit);
initCalled = true;
}
strError = "Call Num " + cntInit;
return true;
}
catch (Exception ex)
{
strError = "ThirdParty.Initialise exception " + ex.Message + " 0n call number " + cntInit;
return false;
}
}
public bool ProcessInformation(string strRequestXML, int quoteMarker, out string strResponseXML, out string strError, out int quoteMarkerReturned)
{
try
{
cntTrans++;
quoteMarkerReturned = quoteMarker;
ThirdParty.ProcessInformation(strRequestXML, out strResponseXML);
strError = "Call Trans Num " + cntTrans;
return true;
}
catch (Exception ex)
{
strError = ex.Message + " On call trans num " + cntTrans;
strResponseXML = "Error";
quoteMarkerReturned = quoteMarker;
return false;
}
}
}
配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="TestServiceBinding" bypassProxyOnLocal="true"
useDefaultWebProxy="false">
<readerQuotas maxDepth="524288" maxStringContentLength="524288"
maxArrayLength="524288" maxBytesPerRead="524288" maxNameTableCharCount="524288" />
<reliableSession inactivityTimeout="00:30:00" enabled="true" />
</binding>
</wsHttpBinding>
<mexHttpBinding>
<binding name="MEXTestServiceBinding" openTimeout="00:02:00"
sendTimeout="00:02:00" />
</mexHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="TestWcfServiceLibrary.TestWCFServiceBehavior"
name="TestWcfServiceLibrary.TestWCFService">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="TestServiceBinding"
name="" contract="TestWcfServiceLibrary.ITestWCFService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="MEXTestServiceBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/TestWcfServiceLibrary/TestWCFService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestWcfServiceLibrary.TestWCFServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="100"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
你怎麼知道只有一個線程被使用?我猜想你的第三方COM組件是Apartment線程,因此具有線程關聯性,所以無論服務實現使用多少.NET線程,它們都必須排隊以使用唯一的STA線程能夠調用COM對象。 – 2012-07-31 12:32:13