Reputation: 123
I have taken my first steps into using WCF for developing a WINDOWS service hosted calc engine. The service is working great but it only ever seems to use one thread for all my calls but I need it to be scalable and use multi-threading.
Ignore all the trace in the code etc as it is VERY early stages of this application folks.
It calls some third party software (COM) that on service start up reads information into memory (ThirdParty.Initialise(strInit)) and then each call to ProcessInformation returns back a calculated results from a given XML string.
So far all calls just seem to use the one thread no matter what I set the ServiceBehavior atts to and no matter how many seperate consumers I use - can anybody help?
Code overview:
[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;
}
}
}
Config:
<?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>
Upvotes: 1
Views: 3881
Reputation: 123
The issue is that the third party DLL is only a single-threaded COM model. I was lead to believe it was multi-threaded but by running a replacement function it is now 100% a third party problem. Thanks for all the quick reponses anyhow guys. Looks like some fancy footwork to make use of some of the extra cores may be needed. I have sent an email to the company seeing if they have a MT version but I doubt it. Thanks again.
Upvotes: 0
Reputation: 12135
It seems quite likely that your third party COM component is Apartment threaded and therefore has thread affinity. If so, there will be only a single thread doing the work within the COM component.
However many .NET threads are being used by the service implementation, they will all be having to queue up to use the particular STA thread which is the only one able to call the COM object.
Upvotes: 2
Reputation: 4797
Per Microsoft docs:
The use of concurrency is related to the instancing mode. In PerCall instancing, concurrency is not relevant, because each message is processed by a new service instance.
// Multiple allows concurrent processing of multiple messages by a service instance.
// The service implementation should be thread-safe. This can be used to increase throughput.
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
// Uses Thread.Sleep to vary the execution time of each operation.
public class CalculatorService : ICalculatorConcurrency
{
int operationCount;
public double Add(double n1, double n2)
{
operationCount++;
System.Threading.Thread.Sleep(180);
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
operationCount++;
System.Threading.Thread.Sleep(100);
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
operationCount++;
System.Threading.Thread.Sleep(150);
return n1 * n2;
}
public double Divide(double n1, double n2)
{
operationCount++;
System.Threading.Thread.Sleep(120);
return n1 / n2;
}
public string GetConcurrencyMode()
{
// Return the ConcurrencyMode of the service.
ServiceHost host = (ServiceHost)OperationContext.Current.Host;
ServiceBehaviorAttribute behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
return behavior.ConcurrencyMode.ToString();
}
public int GetOperationCount()
{
// Return the number of operations.
return operationCount;
}
}
Sample output: (number in [] indicates thread id)
2012-07-31 09:07:28,509 [9] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 752.919376325402
2012-07-31 09:07:28,512 [17] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 752.919376325402
2012-07-31 09:07:28,524 [13] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 2143.10173334651
2012-07-31 09:07:28,524 [11] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Entering: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double n1 = 15.5665354410031, double n2 = 48.3678194919451)
2012-07-31 09:07:28,532 [22] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Entering: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double n1 = 94.7194438868758, double n2 = 29.8120223590229)
2012-07-31 09:07:28,534 [9] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Entering: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double n1 = 99.2045067247024, double n2 = 88.4957458770349)
2012-07-31 09:07:28,539 [4] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Entering: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double n1 = 99.2045067247024, double n2 = 88.4957458770349)
2012-07-31 09:07:28,539 [7] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Entering: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double n1 = 37.991849630136, double n2 = 41.7864370820049)
2012-07-31 09:07:28,539 [17] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Entering: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double n1 = 11.331077670367, double n2 = 55.5888338273339)
2012-07-31 09:07:28,539 [11] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 752.919376325402
2012-07-31 09:07:28,539 [22] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 2823.77817898976
2012-07-31 09:07:28,539 [17] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 629.881393702645
2012-07-31 09:07:28,542 [9] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 8779.17681696586
2012-07-31 09:07:28,544 [4] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 8779.17681696586
2012-07-31 09:07:28,544 [7] DEBUG Microsoft.ServiceModel.Samples.CalculatorService - Leaving: Microsoft.ServiceModel.Samples.CalculatorService.Multiply(double, double) : 1587.54403419867
Upvotes: 1