我正在尝试在vb.net NsqSharp中使用此包 在C#中有一个很好的代码,但我在vb.net中需要它。
我让它向我的NSQ服务器发送消息,但问题是得到它。
但我在consumer.AddHandler(New HandleMessage())
上收到错误,我不知道我是否宣布HandleMessage
权利。
Imports NsqSharp
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim producer = New Producer("127.0.0.1:4150")
producer.Publish("test-topic-name", Me.txt_tx.Text)
producer.Stop()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim consumer = New Consumer("test-topic-name", "channel-name")
consumer.AddHandler(New HandleMessage())
consumer.ConnectToNsqLookupd("127.0.0.1:4161")
consumer.Stop()
End Sub
Public Interface IHandler : End Interface
Public Sub HandleMessage(message As Message)
Dim msg As String = Encoding.UTF8.GetString(message.Body)
MsgBox(msg)
End Sub
Public Sub LogFailedMessage(message As Message)
Dim msg As String = Encoding.UTF8.GetString(message.Body)
MsgBox(msg)
End Sub
End Class
答案 0 :(得分:3)
但我在Implements IHandler上收到错误
问题的可靠描述,当您没有描述您看到的完全错误消息时,您无法获得有用的答案。您确实编写了错误的代码,VB.NET在接口方法实现上需要Implements
关键字。通过让IDE为您生成这些方法,您通常会陷入成功之中。只要键入“Implements IHandler”并按Enter键,IDE就会自动添加方法。
因此库引用可能也有问题。步骤一一:
Imports NsqSharp
放在源文件的顶部。你现在应该最终得到:
Public Class MessageHandler
Implements IHandler
Private Sub IHandler_HandleMessage(message As Message) Implements IHandler.HandleMessage
Dim msg As String = Encoding.UTF8.GetString(message.Body)
MessageBox.Show(msg)
End Sub
Private Sub IHandler_LogFailedMessage(message As Message) Implements IHandler.LogFailedMessage
Dim msg As String = Encoding.UTF8.GetString(message.Body)
MessageBox.Show(msg)
End Sub
End Class