如何从Windows服务中获取数据到WEB应用程序?

问题描述:

我已经与称重机进行交互的Windows服务,并获得结果到txt文件。而不是我想要将这些数据接收到我的Web应用程序中。我的服务代码如下。如何从Windows服务中获取数据到WEB应用程序?

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. 
    lg.WriteToLog("------------Service Started!-----------------") 
    Try 
     myPort.PortName = "COM2" 
     myPort.BaudRate = 9600 
     myPort.Parity = Parity.None 
     myPort.StopBits = System.IO.Ports.StopBits.One 
     myPort.DataBits = 8 
     AddHandler myPort.DataReceived, AddressOf DataReceived 
     myPort.Open() 
    Catch ex As Exception 
     lg.WriteToLog("Error Occurred while Initializing Serial Port !") 
     lg.WriteToLog(ex.ToString) 
    End Try 
End Sub 
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) 
    Try 
     If myPort.IsOpen Then 
      ReceivedText(myPort.ReadExisting()) 
      lg.WriteToLog("Port is open and getting the data!") 
     End If 
    Catch ex As Exception 
     lg.WriteToLog("Error Occurred while reading data!") 
     lg.WriteToLog(ex.ToString) 
    End Try  
End Sub Private Sub ReceivedText(ByVal [text] As String) 
    lg.WriteToLog(output([text])) 
End Sub` 

任何人都可以建议我如何获得相同的数据发送回WebApplication的,因为我与Web应用程序很新鲜,我试着用搜索引擎,但没有发现任何有用的答案。这里的输出只是一个简单的功能,可以以正确的格式执行重量转换,因此您可以忽略这一点!

您可以创建一个Web API并将您的数据发送到该Web api,并从那里您可以使用它。

更新:选中此How to make HTTP request in windows service?

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

+0

感谢您的答复先生,这将是巨大的,如果你能方式如何做到这一点给我。 –

+0

我已添加一个链接,请按照它。 – Eklavya