将C#WCF应用程序转换为Visual Basic 我试图在Visual Basic中编写一个简单的/小的Windows通信基础服务应用程序(但我在VB中非常新手),我在网上找到的所有好例子都是用C语言编写的。到目前为止,我已经让我的WCF服务应用程序正常工作,但现在我正在尝试添加回调功能,程序变得更加复杂。在C#示例代码中,我了解所有的工作原理,但在将使用委托的代码部分翻译成VB时遇到困难。有人能展示一下VB的等价物吗
下面是我用来参考的C#代码示例:将C#WCF应用程序转换为Visual Basic 我试图在Visual Basic中编写一个简单的/小的Windows通信基础服务应用程序(但我在VB中非常新手),我在网上找到的所有好例子都是用C语言编写的。到目前为止,我已经让我的WCF服务应用程序正常工作,但现在我正在尝试添加回调功能,程序变得更加复杂。在C#示例代码中,我了解所有的工作原理,但在将使用委托的代码部分翻译成VB时遇到困难。有人能展示一下VB的等价物吗,c#,vb.net,wcf,delegates,C#,Vb.net,Wcf,Delegates,下面是我用来参考的C#代码示例: namespace WCFCallbacks { using System; using System.ServiceModel; [ServiceContract(CallbackContract = typeof(IMessageCallback))] public interface IMessage { [OperationContract] void AddMessage(str
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;
}
}
}
}
所以你不知道这个
这里的在线C#到VB转换工具:成功地转换代码。我个人使用-它甚至可以对上传的文件进行批量转换。现在你从中获得了所有乐趣!Telerik的转换器非常糟糕。
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
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