none
help with WCF Callback code in VB RRS feed

  • Question

  • I'm trying to figure out how to use WCF with callback. I have little knowlage of c#, but found an example in C# which I converted to VB. This code works as it should in C#, however it fails in VB. It apears as though the connection is not being made, but I'm not sure. Could someone look this over and point me in the right direction for troubleshooting it.

    The code is in 5 'modules' in 3 projects. Here it is by project module...

    Project CommonParts (one Module)

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.ServiceModel
    Namespace CommonParts
        <ServiceContract([Namespace]:="rf.services", CallbackContract:=GetType(IDataOutputCallback), SessionMode:=SessionMode.Required)> _
        Public Interface IServerWithCallback
            <OperationContract(IsOneWay:=True)> _
            Sub StartDataOutput()
        End Interface
    
        Public Interface IDataOutputCallback
            <OperationContract(IsOneWay:=True)> _
            Sub SendDataPacket(data As String)
        End Interface
    End Namespace
    

    Project RF.WCF.Callback.Client (2 Modules)

    CallbackImpl (First Module)

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports CommonParts
    
    Namespace RF.WCF.Callback.Client
        Class CallbackImpl
            Implements IDataOutputCallback
    
    #Region "IDataOutputCallback Members"
    
            Public Sub SendDataPacket(data As String)
                Console.WriteLine("Server sent: {0}", data)
            End Sub
    
    #End Region
    
            Public Sub SendDataPacket1(data As String) Implements CommonParts.IDataOutputCallback.SendDataPacket
    
            End Sub
        End Class
    
    End Namespace

    Program (Second Module)

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.ServiceModel
    Imports CommonParts
    Namespace RF.WCF.Callback.Client
        Module Program
            Public Sub Main()
                Try
                    Dim cf As New DuplexChannelFactory(Of IServerWithCallback)(New CallbackImpl(), _
                              New NetTcpBinding(), _
                              New EndpointAddress("net.tcp://localhost:51321/DataService"))
                    Dim srv As IServerWithCallback = cf.CreateChannel()
                    srv.StartDataOutput()
                    Console.WriteLine("Start")
                    Console.ReadLine()
                Catch ex As Exception
    
                End Try
    
            End Sub
        End Module
    End Namespace

    Project RF.WCF.Callback.Server

    ServerWCallbackImpl (First Module)

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports CommonParts
    Imports System.ServiceModel
    
    Namespace RF.WCF.Callback.Server
        Class ServerWCallbackImpl
            Implements IServerWithCallback
    
    #Region "IServerWithCallback Members"
    
            Public Sub StartDataOutput()
                'OperationContext.Current.
                Dim callback As IDataOutputCallback = OperationContext.Current.GetCallbackChannel(Of IDataOutputCallback)()
                For i As Integer = 0 To 9
                    Dim r As New Random()
                    Dim interval As Integer = r.[Next](500, 3000)
                    System.Threading.Thread.Sleep(interval)
                    callback.SendDataPacket("Packet " & i.ToString())
                Next
                callback.SendDataPacket("Last packet is this one :)")
            End Sub
    #End Region
    
            Public Sub StartDataOutput1() Implements CommonParts.IServerWithCallback.StartDataOutput
    
            End Sub
        End Class
    End Namespace

    Program (Second Module)

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.ServiceModel
    Imports CommonParts
    Namespace RF.WCF.Callback.Server
        Module Program
            Public Sub Main()
                Try
                    Dim duplex As New ServiceHost(GetType(ServerWCallbackImpl))
                    duplex.AddServiceEndpoint(GetType(IServerWithCallback), _
                            New NetTcpBinding(), _
                            "net.tcp://localhost:51321/DataService")
                    duplex.Open()
                    Console.WriteLine("Host is running, press <ENTER> to exit.")
                    Console.ReadLine()
                Catch ex As Exception
    
                End Try
    
            End Sub
        End Module
    End Namespace

    My ultimate goal is to have the server 'call' a function on the clients (preferable all clients at once) when a server condition is met.

    Thanks in advance.

    Thursday, April 25, 2013 1:21 AM

Answers

  • Hi,

    >>It apears as though the connection is not being made, but I'm not sure.

    From your code, you are trying to host a duplex service in a Console application, does the service build and run properly? If not, is there any error message? I would suggest you compare the project with the C# one(that works correctly) to check if you have missed something.

    In addition you can refer a sample(in VB) using WSDualHttpBinding for duplex communication provided by CarlosFigueira in this thread.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 26, 2013 6:32 AM
    Moderator