Translating a C# WCF app into Visual Basic

Posted by MikeG on Stack Overflow See other posts from Stack Overflow or by MikeG
Published on 2010-10-15T17:55:20Z Indexed on 2012/04/16 5:29 UTC
Read the original article Hit count: 149

Filed under:
|
|
|

I'm trying to write a simple/small Windows Communication Foundation service application in Visual Basic (but I am very novice in VB) and all the good examples I've found on the net are written in C#. So far I've gotten my WCF service application working but now I'm trying to add callback functionality and the program has gotten more complicated. In the C# example code I understand how everything works but I am having trouble translating into VB the portion of code that uses a delegate. Can someone please show the VB equivalent?

Here is the C# code sample I'm using for reference:

namespace WCFCallbacks
{
    using System;
    using System.ServiceModel;

    [ServiceContract(CallbackContract = typeof(IMessageCallback))]
    public interface IMessage
    {
        [OperationContract]
        void AddMessage(string message);

        [OperationContract]
        bool Subscribe();

        [OperationContract]
        bool Unsubscribe();
    }

    interface IMessageCallback
    {
        [OperationContract(IsOneWay = true)]
        void OnMessageAdded(string message, DateTime timestamp);
    }        
}

namespace WCFCallbacks
{
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;

    public class MessageService : IMessage
    {
        private static readonly List<IMessageCallback> subscribers = new List<IMessageCallback>();

        //The code in this AddMessage method is what I'd like to see re-written in VB...
        public void AddMessage(string message)
        {

            subscribers.ForEach(delegate(IMessageCallback callback)
            {
                if (((ICommunicationObject)callback).State == CommunicationState.Opened)
                {
                    callback.OnMessageAdded(message, DateTime.Now);
                }
                else
                {
                    subscribers.Remove(callback);
                }
            });
        }

        public bool Subscribe()
        {
            try
            {
                IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
                if (!subscribers.Contains(callback))
                    subscribers.Add(callback);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Unsubscribe()
        {
            try
            {
                IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
                if (!subscribers.Contains(callback))
                    subscribers.Remove(callback);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }        
}

I was thinking I could do something like this but I don't know how to pass the message string from AddMessage to DoSomething...

Dim subscribers As New List(Of IMessageCallback)

Public Sub AddMessage(ByVal message As String) Implements IMessage.AddMessage

    Dim action As Action(Of IMessageCallback)
    action = AddressOf DoSomething
subscribers.ForEach(action)

'Or this instead of the above three lines:
'subscribers.ForEach(AddressOf DoSomething)

End Sub

Public Sub DoSomething(ByVal callback As IMessageCallback)

    'I am also confused by:
    '((ICommunicationObject)callback).State
    'Is that casting the callback object as type ICommunicationObject? 
    'How is that done in VB?

End Sub

© Stack Overflow or respective owner

Related posts about c#

Related posts about vb.net