c# - 为什么这些异步 RIA 服务调用在 Web 服务器上串行执行?

标签 c# silverlight asynchronous entity-framework-4 wcf-ria-services

我正在调用 RIA 服务方法 ImportSubcomponentFileSetFiles(这是一个 Invoke,而不是 Query)两次,如下所示:

foreach (var viewModel in FileSetViewModels.Where(vm => vm.IsSelectedForImport))
{
    DomainContext.ImportSubcomponentFileSetFiles(viewModel.SubcomponentFileSet.Id, callback =>
                {
        //...
        DomainContext.Load(DomainContext.GetSubcomponentFileSetWithStatusQuery(subcomponentFileSetId), LoadBehavior.RefreshCurrent,
                            callback2 =>
        {
            //...
        }, true);
    }, null);
}

我可以在 Fiddler 中看到两个请求立即发送到 ImportSubcomponentFileSetFiles

ImportSubcomponentFileSetFiles大约需要 15 秒才能完成。第一个调用约 15 秒后返回,第二个调用约 15 秒后返回。我可以从日志记录中看到,直到第一次调用完成后,才会开始对 ImportSubcomponentFileSetFiles 的第二次调用。

我的问题是,为什么这些异步请求在服务器上串行处理以及如何并行处理它们?

其他信息

  • WCF Ria 服务托管在 ASP.NET Web 应用程序中。
  • 我没有在 session 中存储任何内容。
  • 我在服务方法中使用 Entity Framework 4。
  • Domain Services 类扩展了 LinqToEntitiesDomainService

我的服务方法如下:

public void ImportSubcomponentFileSetFiles(int subcomponentId)
{
    Debug.WriteLine("{0}: {1} - ImportSubcomponentFileSetFiles method started", subcomponentId, DateTime.Now);

    //...code for fetching subcomponentFileSet

    subcomponentFileSet.ImportFiles(ObjectContext);

    Debug.WriteLine("{0}: {1} - ImportSubcomponentFileSetFiles method finished", subcomponentId, DateTime.Now);
}

以下是我的日志,详细说明了在第一个方法调用完成后才开始的第二个方法调用:

File Set A: 27/1/2011 11:20:06 PM 11:20:06 PM - Calling ImportSubcomponentFileSetFiles (Silverlight Client)
File Set A: 27/1/2011 11:20:06 PM 11:20:06 PM - ImportSubcomponentFileSetFiles Called (Silverlight Client)
File Set B: 27/1/2011 11:20:06 PM 11:20:06 PM - Calling ImportSubcomponentFileSetFiles (Silverlight Client)
File Set B: 27/1/2011 11:20:06 PM 11:20:06 PM - ImportSubcomponentFileSetFiles Called (Silverlight Client)
File Set A: 01/27/2011 23:20:06 - ImportSubcomponentFileSetFiles method started (Server)
File Set A: 01/27/2011 23:20:06 - ImportFiles Started (Server)
File Set A: 01/27/2011 23:20:23 - ImportFiles Finished (Server)
File Set A: 01/27/2011 23:20:23 - ImportSubcomponentFileSetFiles method finished (Server)
File Set A: 27/1/2011 11:20:23 PM - Import callback recieved (Silverlight Client)
File Set A: 01/27/2011 23:20:23 - GetSubcomponentFileSetWithStatus Started (Server)
File Set A: 01/27/2011 23:20:23 - GetSubcomponentFileSetWithStatus Finished (Server)
File Set B: 01/27/2011 23:20:23 - ImportSubcomponentFileSetFiles method started (Server)
File Set B: 01/27/2011 23:20:23 - ImportFiles Started (Server)
File Set A: 27/1/2011 11:20:23 PM - Refresh callback recieved (Silverlight Client)
File Set B: 01/27/2011 23:20:36 - ImportFiles Finished (Server)
File Set B: 01/27/2011 23:20:36 - ImportSubcomponentFileSetFiles method finished (Server)
File Set B: 27/1/2011 11:20:36 PM - Import callback recieved (Silverlight Client)
File Set B: 01/27/2011 23:20:36 - GetSubcomponentFileSetWithStatus Started (Server)
File Set B: 01/27/2011 23:20:36 - GetSubcomponentFileSetWithStatus Finished (Server)
File Set B: 27/1/2011 11:20:36 PM - Refresh callback recieved (Silverlight Client)

干杯,
克里斯

最佳答案

我认为 LinqToEntitiesDomainService 派生自 WCF 类,在这种情况下,可以设置以下 ServiceBehaviour 属性(通过代码或配置):

ServiceBehaviourAttribute.InstanceContextMode :

The default value, PerSession, instructs the service application to create a new service object when a new communication session is established between a client and the service application

ServiceBehaviourAttribute.ConcurrencyMode :

the default is Single.

Setting ConcurrencyMode to Single instructs the system to restrict instances of the service to one thread of execution at a time, which frees you from dealing with threading issues. A value of Multiple means that service objects can be executed by multiple threads at any one time. In this case, you must ensure thread safety.


这也导致我this statement :

If InstanceContextMode is set to PerSession and ConcurrencyMode is set to Single, each proxy gets its own service instance. All simultaneous calls from a single proxy to the service instance are serialized because only one thread is allowed into the service instance at one time. Because only one thread is allowed into the service at one time, all other calls made on the same proxy will block until that thread exits. If you have a large number of clients that are all making calls this causes a bottleneck.

那么您在客户端上使用的是单个代理吗?您可以创建多个代理吗?或者这是 silverlight 客户端中对您隐藏的东西吗?

关于c# - 为什么这些异步 RIA 服务调用在 Web 服务器上串行执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4813474/

相关文章:

c# - 如何在C#中使用linq语句从数据库返回一定数量的项目

c# - .NET 中 yield 和 await 如何实现控制流?

c# - Silverlight 3 可以打开套接字吗?

c# - C# 中的命令模式和异步操作处理

c# - 等待关键属性

C# 连接必须有效并打开Mysql

c# - Entity Framework 4.1 代码优先外键 ID

c# - 银光和 WCF : Max message size

wpf - 使用 Access 数据库将 WPF 应用程序转换为 Silverlight

android - 如何在 Android 的服务中使用 AsyncTask?