Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3230100
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:53:14+00:00 2026-05-17T16:53:14+00:00

I’m trying to write a simple/small Windows Communication Foundation service application in Visual Basic

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:53:15+00:00Added an answer on May 17, 2026 at 4:53 pm

    So you don’t know the secret converter?

    Public Class MessageService
        Implements IMessage
        Private Shared ReadOnly subscribers As New List(Of IMessageCallback)()
    
        'The code in this AddMessage method is what I'd like to see re-written in VB...
        Public Sub AddMessage(message As String)
    
            subscribers.ForEach(Function(callback As IMessageCallback) Do
                If DirectCast(callback, ICommunicationObject).State = CommunicationState.Opened Then
                    callback.OnMessageAdded(message, DateTime.Now)
                Else
                    subscribers.Remove(callback)
                End If
            End Function)
        End Sub
    
        Public Function Subscribe() As Boolean
            Try
                Dim callback As IMessageCallback = OperationContext.Current.GetCallbackChannel(Of IMessageCallback)()
                If Not subscribers.Contains(callback) Then
                    subscribers.Add(callback)
                End If
                Return True
            Catch
                Return False
            End Try
        End Function
    
        Public Function Unsubscribe() As Boolean
            Try
                Dim callback As IMessageCallback = OperationContext.Current.GetCallbackChannel(Of IMessageCallback)()
                If Not subscribers.Contains(callback) Then
                    subscribers.Remove(callback)
                End If
                Return True
            Catch
                Return False
            End Try
        End Function
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.