C# WCF服务不是多线程的
我正在设计一个WPF应用程序使用的WCF服务。 该服务将由50个客户端使用,并托管在多核服务器上。这就是为什么我希望它是多线程的 我是这样宣布的:C# WCF服务不是多线程的,c#,multithreading,web-services,wcf,C#,Multithreading,Web Services,Wcf,我正在设计一个WPF应用程序使用的WCF服务。 该服务将由50个客户端使用,并托管在多核服务器上。这就是为什么我希望它是多线程的 我是这样宣布的: [ServiceContract( SessionMode = SessionMode.Required, Namespace = Constants.NameSpace, CallbackContract = typeof (ISaphirServiceCallback))] public interface ISaphir
[ServiceContract(
SessionMode = SessionMode.Required,
Namespace = Constants.NameSpace,
CallbackContract = typeof (ISaphirServiceCallback))]
public interface ISaphirService
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode=InstanceContextMode.PerSession)]
public partial class SaphirService : ISaphirService
以及服务器端配置:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NewBinding0" receiveTimeout="00:59:00" sendTimeout="00:59:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="true"/>
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</netTcpBinding>
<customBinding>
<binding name="ServicePECB2ServiceBinding">
<textMessageEncoding messageVersion="Soap12WSAddressing10" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://qualiflps.services-ps.ameli.fr/lps" binding="customBinding" bindingConfiguration="ServicePECB2ServiceBinding" contract="ServiceReference1.ServicePECB2Service" name="ServicePECB2Service" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceThrottling maxConcurrentCalls="50" maxConcurrentSessions="50" maxConcurrentInstances="50"/>
<serviceAuthorization serviceAuthorizationManagerType="Service.Authorizations.AuthorizationPolicy, Service">
<authorizationPolicies>
<add policyType="Service.Authorizations.AuthorizationPolicy, Service" />
</authorizationPolicies>
</serviceAuthorization>
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:80/Service" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<serviceCertificate storeLocation="CurrentUser" storeName="TrustedPeople" x509FindType="FindBySubjectName" findValue="*****" />
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Service.Authorizations.CustomValidator, Service" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior0" name="Service.Services.SaphirService">
<endpoint address="basic" binding="netTcpBinding" bindingConfiguration="NewBinding0" contract="ServiceInterfaces.IServices.ISaphirService">
<identity>
<dns value="*****" />
</identity>
</endpoint>
</service>
</services>
</system.serviceModel>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_ISaphirService" receiveTimeout="00:30:00" sendTimeout="00:05:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="true"/>
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://****:4224/service/basic" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ISaphirService" contract="ISaphirService" name="NetTcpBinding_ISaphirService" behaviorConfiguration="CustomBehavior">
<identity>
<certificate encodedValue="****" />
</identity>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="PeerOrChainTrust" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
以下是客户端配置:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NewBinding0" receiveTimeout="00:59:00" sendTimeout="00:59:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="true"/>
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</netTcpBinding>
<customBinding>
<binding name="ServicePECB2ServiceBinding">
<textMessageEncoding messageVersion="Soap12WSAddressing10" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://qualiflps.services-ps.ameli.fr/lps" binding="customBinding" bindingConfiguration="ServicePECB2ServiceBinding" contract="ServiceReference1.ServicePECB2Service" name="ServicePECB2Service" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceThrottling maxConcurrentCalls="50" maxConcurrentSessions="50" maxConcurrentInstances="50"/>
<serviceAuthorization serviceAuthorizationManagerType="Service.Authorizations.AuthorizationPolicy, Service">
<authorizationPolicies>
<add policyType="Service.Authorizations.AuthorizationPolicy, Service" />
</authorizationPolicies>
</serviceAuthorization>
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:80/Service" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<serviceCertificate storeLocation="CurrentUser" storeName="TrustedPeople" x509FindType="FindBySubjectName" findValue="*****" />
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Service.Authorizations.CustomValidator, Service" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior0" name="Service.Services.SaphirService">
<endpoint address="basic" binding="netTcpBinding" bindingConfiguration="NewBinding0" contract="ServiceInterfaces.IServices.ISaphirService">
<identity>
<dns value="*****" />
</identity>
</endpoint>
</service>
</services>
</system.serviceModel>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_ISaphirService" receiveTimeout="00:30:00" sendTimeout="00:05:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="true"/>
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://****:4224/service/basic" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ISaphirService" contract="ISaphirService" name="NetTcpBinding_ISaphirService" behaviorConfiguration="CustomBehavior">
<identity>
<certificate encodedValue="****" />
</identity>
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="PeerOrChainTrust" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
问题是,每个请求都在同一个线程上处理。
我在网上查了很多,但对我来说一切都很好
你们知道吗
谢谢 如果希望服务在来自同一客户端的调用之间保持某些状态,那么值得一提的是
InstanceContextMode=InstanceContextMode.PerSession
。否则,您可以在打开服务主机时使用InstanceContextMode=InstanceContextMode.PerCall
WCF捕获当前同步上下文
,并将其用于所有调用。WPF的同步上下文将每个调用发布到调度程序队列,该队列最终在UI线程上执行
您有两个选择:
- 在没有同步上下文的其他线程上启动服务。这样做的另一个优点是不会阻塞等待加载服务的UI线程。例如,您可以使用:
Task.Run(() => serviceHost.Open());
- 指定服务不应使用同步上下文:
[ServiceBehavior(UseSynchronizationContext = false)]
请注意,如果您在服务方法中修改UI对象,您可能需要自己将它们分派回UI线程。您是如何检查的?如果50个客户端执行该服务,而您在其中设置了睡眠,那么它们是否会一个接一个地等待处理?有多少个请求,在什么时间段内,以及持续多长时间?我与两个客户端进行了检查。首先,可以清楚地看到,来自客户端2的请求1正在等待来自客户端1的第一个请求,还有一些控制台。写入(ManagedThreadId)到目前为止,来自2个客户端的20个请求,每个请求5秒这个WCF服务是如何托管的(IIS、Windows服务等)?我确实需要会话,我使用了大量来自服务的推送请求。我不确定模拟50个客户端调用时所需的验证级别。但是,ServiceThrottling可能是最好的起点。你也可以看看这篇文章,我认为我的配置在服务节流方面还可以,不是吗?太棒了!一行代码,一切都很顺利:D非常感谢!