在现有流程实例上执行方法.Vb.Net



我有一个 Vb.Net 编写的Windows服务。作为此服务的一部分,它调用具有长时间运行的 Process 的类。

当我想通过服务中的 ServerCommand(( 类执行命令时,我可以对此过程执行命令,但是我想远程调用这些命令。可能来自网站或单击一次 WPF 应用程序。

为此,我使用了一个简单的 Tcp.Ip WCF 示例,并验证它是否正常工作。

这称为 OnStart((

Private _serverCommands As ServerCommands
Protected Overrides Sub OnStart(ByVal args() As String)
    ' Add code here to start your service. This method should set things
    ' in motion so your service can do its work.
    Debugger.Launch()
    ' Action a new implementaion of the WCF Service on localhost
    _host.AddServiceEndpoint(GetType(ICommunicationService), New NetTcpBinding(), String.Format("net.tcp://127.0.0.1:{0}", AppSettings.TcpServicePort))
    _host.Open()
    ' Start the server command
    _serverCommands = New ServerCommands()
    _serverCommands.StartServer()
End Sub

然而。。。当我通过 WCF 调用服务时,它会启动 ServerCommand(( 类的新实例,而不是附加到已经运行的线程。

以下调用

Public Function DoWork() As String Implements ICommunicationService.DoWork
    Dim command As String = "say hello world"
    Dim service As IMinecraftService = New MinecraftService()
    service.ExecuteServerSideCommand(command)
    Return "Command Executed"
End Function

在主服务上实现这一点。

Public Sub ExecuteServerSideCommand(command As String) Implements IMinecraftService.ExecuteServerSideCommand
    If (_serverCommands IsNot Nothing) Then
        _serverCommands.SendCommand(command)
    End If
End Sub

似乎在调试_serverCommands中,当它应该运行时是 Nothing。

如何确保通过 WCF 执行的任何命令都与正在运行的实例通信,而不是创建新的 ServerCommand(( 实例?

我以前没有尝试过WCF,所以我可能会陷入死胡同......但是我确信这是可能的。

提前谢谢。

我发现每次通过WCF发送命令时,我都会调用MinecraftService的新实例。

正如 Jeff 正确所说,我没有使对象共享,我只是访问了这个类的新实例。

我把它从

我的主类

    Private _serverCommands As ServerCommands

我的 WCF 服务

    Dim command As String = "say hello world"
    MinecraftService.ServerCommands.SendCommand(command)

我的主类

    Public Shared ServerCommands As ServerCommands

我的 WCF 服务

    MinecraftService.ServerCommands.SendCommand(command)

最新更新