gpt4 book ai didi

c# - 自托管WCF服务: ConcurrencyMode.多个但仅使用一个线程?

转载 作者:行者123 更新时间:2023-12-03 13:21:16 25 4
gpt4 key购买 nike

我已开始使用WCF来开发WINDOWS服务托管的calc引擎的第一步。该服务运行良好,但似乎所有调用都只使用一个线程,但我需要它具有可伸缩性并使用多线程。

忽略代码等中的所有跟踪信息,因为这是该应用程序人员的早期。

它调用一些第三方软件(COM),该软件在服务启动时将信息读入内存(ThirdParty.Initialise(strInit)),然后每次调用ProcessInformation都会从给定的XML字符串中返回计算出的结果。

到目前为止,无论我将ServiceBehavior属性设置为什么,而且无论我使用多少个单独的使用者,所有调用似乎都只使用一个线程-有人可以帮忙吗?

代码概述:

[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线程的,因此具有线程相似性。如果是这样,则COM组件中将只有一个线程在执行该工作。

无论服务实现使用了多少.NET线程,它们都必须排队才能使用特定的STA线程,后者是唯一能够调用COM对象的线程。

关于c# - 自托管WCF服务: ConcurrencyMode.多个但仅使用一个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com