Results 1 to 3 of 3

Thread: [RESOLVED] Asynchronous WCF Service

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Resolved [RESOLVED] Asynchronous WCF Service

    Hello everybody!

    I'm new to the WCF architecture and need some help in realizing an
    asynchronous WCF Service.

    Currently I just don't get it how to bring it working.


    First of all this is how my main service contract is coded:

    VB.Net Code:
    1. <ServiceContract()>
    2. Public Interface IMainService
    3.  
    4.   <OperationContract(ProtectionLevel:=Net.Security.ProtectionLevel.None)>
    5.   Function GetData(ByVal value As Integer) As String
    6.  
    7.   <OperationContract(AsyncPattern:=True)>
    8.   Function BeginAdd(ByVal value1 As Integer, _
    9.                     ByVal value2 As Integer, _
    10.                     ByVal callback As AsyncCallback, _
    11.                     ByVal state As Object) As IAsyncResult
    12.  
    13.   Function EndAdd(ByVal ar As IAsyncResult) As String
    14.  
    15. End Interface

    Here is the implementation:

    VB.Net Code:
    1. Public Class MainService
    2.   Implements IMainService
    3.  
    4.   Public Sub New()
    5.   End Sub
    6.  
    7.   Public Function GetData(ByVal value As Integer) As String _
    8.                                                   Implements IMainService.GetData
    9.  
    10.     Return String.Format("You entered: {0}", value)
    11.  
    12.   End Function
    13.  
    14.   Public Function BeginAdd(ByVal value1 As Integer, ByVal value2 As Integer, _
    15.                            ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult _
    16.                                                                                           Implements IMainService.BeginAdd
    17.  
    18.     Return New CompletedAsyncResult(Of Integer)(value1 + value2)
    19.  
    20.   End Function
    21.  
    22.   Public Function EndAdd(ByVal ar As System.IAsyncResult) As String _
    23.                                                           Implements IMainService.EndAdd
    24.     Dim result As CompletedAsyncResult(Of String) = TryCast(ar, CompletedAsyncResult(Of String))
    25.     Return result.Data
    26.  
    27.   End Function
    28.  
    29. End Class
    30.  
    31. Friend Class CompletedAsyncResult(Of T)
    32.   Implements IAsyncResult
    33.  
    34.   Private data_Renamed As T
    35.  
    36.   Public Sub New(ByVal data As T)
    37.     Me.data_Renamed = data
    38.   End Sub
    39.  
    40.   Public ReadOnly Property Data() As T
    41.     Get
    42.       Return data_Renamed
    43.     End Get
    44.   End Property
    45.  
    46. #Region "IAsyncResult Members"
    47.   Public ReadOnly Property AsyncState() As Object Implements IAsyncResult.AsyncState
    48.     Get
    49.       Return CObj(data_Renamed)
    50.     End Get
    51.   End Property
    52.  
    53.   Public ReadOnly Property CompletedSynchronously() As Boolean Implements IAsyncResult.CompletedSynchronously
    54.     Get
    55.       Return True
    56.     End Get
    57.   End Property
    58.  
    59.   Public ReadOnly Property IsCompleted() As Boolean Implements IAsyncResult.IsCompleted
    60.     Get
    61.       Return True
    62.     End Get
    63.   End Property
    64.  
    65.   Public ReadOnly Property AsyncWaitHandle As System.Threading.WaitHandle Implements System.IAsyncResult.AsyncWaitHandle
    66.     Get
    67.       Throw New Exception("The method or operation is not implemented.")
    68.     End Get
    69.   End Property
    70. #End Region
    71.  
    72. End Class

    I'm familiar with the basics on using asynchronous operations "offline".
    But I think I'm missing some links to the "online" usage of such operations.

    Here is my client code:

    VB.Net Code:
    1. Imports System.ServiceModel
    2.  
    3. Module MainModule
    4.  
    5.   Sub Main()
    6.  
    7.     Dim basicHttpBind As New BasicHttpBinding()
    8.     Dim endpointAddress As New EndpointAddress("http://localhost:2053/MainService.svc")
    9.     Dim mainService As IMainService = New ChannelFactory(Of IMainService)(basicHttpBind, endpointAddress).CreateChannel
    10.  
    11.     'Console.WriteLine(mainservice.GetData(123))
    12.  
    13.     Dim value1 As Integer = 100
    14.     Dim value2 As Integer = 15
    15.  
    16.     Dim arAdd As IAsyncResult = mainService.BeginAdd(value1, value2, Nothing, mainService)
    17.     Console.WriteLine("Add({0},{1})", value1, value2)
    18.  
    19.     While Not arAdd.IsCompleted
    20.       Console.WriteLine("Waiting...")
    21.       Threading.Thread.Sleep(1000)
    22.     End While
    23.  
    24.     Console.WriteLine(mainService.EndAdd(arAdd))
    25.  
    26.     Console.ReadKey()
    27.  
    28.   End Sub
    29.  
    30.   Private Sub AddCallback(ByVal ar As IAsyncResult)
    31.     Dim result = (CType(ar.AsyncState, IMainService)).EndAdd(ar)
    32.     Console.WriteLine("Add Result: {0}", result)
    33.   End Sub
    34.  
    35. End Module

    The Callback isn't working.

    So I would be really happy if someone could help me out.
    On the net I did not find any useful hints for me.

    It's important to me to understand the whole process in detail.

    Kind regards
    Robin

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Asynchronous WCF Service

    I didn't run the code (no IDE at the moment, beside I prefer C#), but I see the AddCallback function is never called. I think it should by in this line:
    vb Code:
    1. Dim arAdd As IAsyncResult = mainService.BeginAdd(value1, value2, [B]AddCallback[/B], mainService
    An other thing is the you neven want to wait for a async action in a loop. The whole point of async is the "not waiting"
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    2

    Thumbs up Re: Asynchronous WCF Service

    Hi Lightning!

    Thanks for the hint.

    Indeed I forget to use that callback


    The next error was, that the Begin and End Functions weren't type safe.

    I changed both to Integer.


    Now it works all well :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width