c# - 将 C# WCF 应用程序转换为 Visual Basic

标签 c# vb.net wcf delegates

我正在尝试用 Visual Basic 编写一个简单/小型的 Windows Communication Foundation 服务应用程序(但我在 VB 方面非常新手),并且我在网上找到的所有好示例都是用 C# 编写的。到目前为止,我已经让我的 WCF 服务应用程序正常工作,但现在我正在尝试添加回调功能,并且程序变得更加复杂。在 C# 示例代码中,我了解一切是如何工作的,但我在将使用委托(delegate)的代码部分转换为 VB 时遇到问题。有人可以展示 VB 等效项吗?

这是我用作引用的 C# 代码示例:

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;
            }
        }
    }        
}

我想我可以做这样的事情,但我不知道如何将消息字符串从 AddMessage 传递到 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

最佳答案

所以你不知道 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

关于c# - 将 C# WCF 应用程序转换为 Visual Basic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3944754/

相关文章:

c# - 使用辅助方法创建动态 lambda

javascript - VB.Net 中Console.log 的类似操作?

c# - 用于序列化的 XML 类生成器

c# - 如何将 c# 代码转换为 vb.net (Linq)?

WCF/WebService : Interoperable exception handling

c# - 配置文件 'appsettings.json'未找到且不可选

c# - 使用 securestring 进行 sql 连接

c# - 如何清除内存流

c# - 使用 Javascript 或 C# 防止 SQL 注入(inject)的最佳方法?

WCF readerQuotas 设置 - 缺点?