调用依次执行的异步WCF服务

时间:2017-06-02 07:15:43

标签: vb.net multithreading wcf asynchronous task-parallel-library

我已经构建了一个基本的WCF控制台服务器应用程序。我的目标是以并行方式处理多个调用,但当前版本按顺序处理它们。

请耐心等待,因为有一道代码可以跟随,但这是非常基本的东西。我很难孤立,因为它是一个相当大的VS解决方案的一部分。

我已经走了TPL和async / await关键字的道路,我基本上理解和喜欢。

服务界面:

<ServiceContract()>
Public Interface IGetBackendData

    <OperationContract>
    Function SendRequest(request As Request) As Task(Of RequestResponse)

    <OperationContract>
    Function GetNextPackage(serverJobID As Guid) As Task(Of PackageBase)

End Interface

代理:

Public Class imBackendServerProxy
    Inherits ClientBase(Of IGetBackendData)
    Implements IGetBackendData

    Public Function SendRequest(request As Request) As Task(Of RequestResponse) Implements IGetBackendData.SendRequest
        Return Channel.SendRequest(request)
    End Function

    Public Function GetNextPackage(serverJobID As Guid) As Task(Of PackageBase) Implements IGetBackendData.GetNextPackage
        Return Channel.GetNextPackage(serverJobID)
    End Function
End Class

实施:

Public Class GetDataService
    Implements IGetBackendData

    Private ActiveJobs As New Dictionary(Of Guid, ServiceJobBase)

    Private Function ProcessRequest(request As Request) As RequestResponse
        Dim newJob As ServiceJobBase

        Select Case request.Command.CommandType
            Case ImagiroQueryLanguage.CommandTypes.CommandHello
                newJob = New HelloJob
            Case Else
                Throw New ArgumentException("Do not know how to process request")
        End Select

        If newJob IsNot Nothing Then
            newJob.AssignedRequest = request
            ActiveJobs.Add(newJob.ID, newJob)
            Return newJob.GetResponse()
        End If

        Throw New ArgumentException("job could not be started")
    End Function

    Public Async Function SendRequest(request As Request) As Task(Of RequestResponse) Implements IGetBackendData.SendRequest
        Console.WriteLine("Request recieved")
        Dim mytask As Task(Of RequestResponse) = Task.Factory.StartNew(Function() ProcessRequest(request))
        Await Task.Delay(1500)
        Return Await mytask.ConfigureAwait(True)

    End Function


    Private Function GenerateNextPackage(jobid As Guid) As PackageBase
        If Not ActiveJobs.ContainsKey(jobid) Then
            Throw New ArgumentException("job could Not be found")
        End If

        Dim nextPackage As PackageBase = ActiveJobs(jobid).GetNextPackage()
        If TypeOf (nextPackage) Is PackageEnd Then
            ActiveJobs.Remove(jobid)
        End If
        Return nextPackage
    End Function

    Public Async Function GetNextPackage(serverTaskID As Guid) As Task(Of PackageBase) Implements IGetBackendData.GetNextPackage
        Dim mytask As Task(Of PackageBase) = Task.Factory.StartNew(Of PackageBase)(Function() GenerateNextPackage(serverTaskID))
        Await Task.Delay(1500)
        Return Await mytask.ConfigureAwait(True)
    End Function
End Class

“Request”对象包含“Command”对象(派生自CommandBase)以及其他信息。 “包”对象(派生自PackageBase)包含要从服务器传输到客户端的数据。

沟通如何运作的基本思路是这样的:

1. "Request" phase
Client --Request--> Server
Client <--GUID A -- Server 

2. "Data" phase
Client --  GUID A  --> Server
Client <--DataOrStop-- Server

3. Repeat step 2. until Server says stop.

要使用数据和请求响应,我有以下类:

Public Class DataReceiver
    Public Event DataPackageRecieved(sender As Object, arg As DataPackageReceivedEventArgs)
    Public Event EndOfTransmission(sender As Object, arg As EventArgs)

    Public Sub New(response As RequestResponse, proxy As imBackendServerProxy, dataRecieved As DataPackageRecievedEventHandler, endOfTransmission As EndOfTransmissionEventHandler)
        ID = response.JobID
        p = proxy

        AddHandler Me.DataPackageRecieved, dataRecieved
        AddHandler Me.EndOfTransmission, endOfTransmission

        FetchData()
    End Sub

    Public Property ID As Guid
    Private p As imBackendServerProxy

    Private Sub FetchData()
        Dim t As Task(Of PackageBase) = Task.Factory.StartNew(Of PackageBase)(Function() p.GetNextPackage(ID).Result)
        Debug.Print("Waiting for Result FetchData")
        t.ContinueWith(AddressOf DoneFetching)
    End Sub

    Public Delegate Sub ProcessDataPackageDelegate(recievedDataPackage As PackageBase)

    Public Property ProcessDataPackage As ProcessDataPackageDelegate

    Private Sub DoneFetching(arg As Task(Of PackageBase))
        If arg.IsCompleted Then
            If TypeOf (arg.Result) Is PackageEnd Then
                RaiseEvent EndOfTransmission(Me, Nothing)
            Else
                RaiseEvent DataPackageRecieved(Me, New DataPackageReceivedEventArgs With {.DataPackage = arg.Result})
                FetchData()
            End If
        End If
    End Sub

End Class

在我的WPF测试客户端应用程序中,我有一个Button,我可以将请求发送到服务器。 HelloCommand(派生自CommandBase)类用于将整数“n”传输到服务器。然后,服务器使用GetNextPackage(源自HelloPackage)并最终使用PackageBase(源自EndPackage)来响应以下每个PackageBase次呼叫

这个逻辑在ServiceJob对象中处理(从ServiceJobBase派生) - 基本上每个“Command”对象都有一个对应的“ServiceJob”对象,后者又将相应的“Package”对象发送给顺序客户端请求

当客户端处理“数据请求”所需的“顺序性”,即对GetNextPackage函数的顺序调用时,这些调用将永远不会重叠。但我非常希望在服务器上并行执行两个或更多单独的GetNextPackage调用 - 以及它们各自的“ServiceJobs”。而这种情况并没有发生。

HelloServiceJob类中添加一个简单的计数器以轻松识别每个请求,按一下我的WPF客户端按钮就会在服务器上产生以下输出,同时UI保持响应。

Request recieved (0)
Sending HelloPackage - 6 remaining (0)
Sending HelloPackage - 5 remaining (0)
Sending HelloPackage - 4 remaining (0)
Sending HelloPackage - 3 remaining (0)
Sending HelloPackage - 2 remaining (0)
Sending HelloPackage - 1 remaining (0)
Sending HelloPackage - 0 remaining (0)
Sending no more HelloPackages

每条线之间的距离为1.5秒。

三次快速连续按下会在服务器上产生以下输出,而UI保持响应。

Request recieved (1)
Request recieved (2)
Request recieved (3)
Sending HelloPackage - 6 remaining (1)
Sending HelloPackage - 6 remaining (2)
Sending HelloPackage - 6 remaining (3)
Sending HelloPackage - 5 remaining (1)
Sending HelloPackage - 5 remaining (2)
Sending HelloPackage - 5 remaining (3)
Sending HelloPackage - 4 remaining (1)
Sending HelloPackage - 4 remaining (2)
Sending HelloPackage - 4 remaining (3)
Sending HelloPackage - 3 remaining (1)
Sending HelloPackage - 3 remaining (2)
Sending HelloPackage - 3 remaining (3)
Sending HelloPackage - 2 remaining (1)
Sending HelloPackage - 2 remaining (2)
Sending HelloPackage - 2 remaining (3)
Sending HelloPackage - 1 remaining (1)
Sending HelloPackage - 1 remaining (2)
Sending HelloPackage - 1 remaining (3)
Sending HelloPackage - 0 remaining (1)
Sending HelloPackage - 0 remaining (2)
Sending HelloPackage - 0 remaining
Sending no more HelloPackages (1)
Sending no more HelloPackages (2)
Sending no more HelloPackages (3)

虽然订单是预期的,但每行需要1.5秒才能执行,客户端和服务器一次只能在消息上进行交换。

在阅读了很多文章后,我比任何事情都更困惑。我无法确定我需要做什么来使三个“作业”并行执行,甚至不是这是完全错误的方式来解决这个问题还是一个简单的配置错误。

我在同一台计算机上运行服务器和客户端,使用netTcpBinding,如果我理解它是正确的,并且适合客户端和服务器之间的多个并行请求。

我已阅读并(希望)理解以下文章,但我不知道这是如何转化为我的案例:tasks are still not threads and async is not parallel

如何让正在接听电话的作业在不同的线程上运行?我完全清楚Return Await语句只是等待执行完成,但这不是问题。我想要的是其中三个语句等待并行完成但是服务器和客户端之间的管道似乎一次只能容纳一条消息?

谢谢大家的时间和投入,我真的很感激。

1 个答案:

答案 0 :(得分:1)

设置ConcurrencyMode:=ConcurrencyMode.Multiple可以解决问题。

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Multiple)>
Public Class GetDataService
    [...]
End Class

Service​Behavior​Attribute.​Concurrency​Mode Property

默认为“单一”