gpt4 book ai didi

c# - Topshelf 服务(充当 TCP 服务器)与自托管 OWIN WebAPI 之间的通信

转载 作者:可可西里 更新时间:2023-11-01 02:53:52 26 4
gpt4 key购买 nike

我有一个用作 TCP 服务器的 Topshelf Windows 服务。在此服务中,我还有一个自托管 (OWIN) WebAPI。

我的目标是以某种方式允许 WebAPI 与同一服务中包含和运行的 TCP 服务器进行通信。自然地,我可以简单地使用诸如“触发器”文件或共享数据库之类的东西可以经常进行轮询,但我想知道实现此目的的任何更优化/ native 方法。

为了更好地了解该项目,请考虑使用我的 API 并使用任意字符串参数进行某些调用的单页应用程序。然后应将此数据传递给连接到正在运行的 TCP 服务器的客户端(使用 winsock 的 C++ 控制台应用程序)。

以下容器被实例化并传递给 Topshelf HostConfigurator

class ContainerService
{
private APIService _apiService;
private EngineService _engineService;
protected IDisposable WebAppHolder { get; set; }

public bool Start(HostControl hostControl)
{
var host = hostControl;
_apiService = new APIService();
_engineService = new EngineService();

// Initialize API service
if (WebAppHolder == null)
{
WebAppHolder = _apiService.Initialize();
}

// Initialize Engine service
_engineService.Initialize();

return true;
}

public bool Stop(HostControl hostControl)
{
// Stop API service
if (WebAppHolder != null)
{
WebAppHolder.Dispose();
WebAppHolder = null;
}

// Stop Engine service
_engineService.Stop();

return true;
}
}

程序入口点中的标准 Topshelf 内容(如上所述):

HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<ContainerService>(containerService =>
{
containerService.WhenStarted((service, control) => service.Start(control));
containerService.WhenStopped((service, control) => service.Stop(control));
});

hostConfigurator.RunAsLocalSystem();
hostConfigurator.SetServiceName("Educe Service Host");
hostConfigurator.SetDisplayName("Communication Service");
hostConfigurator.SetDescription("Responsible for API and Engine services");
});

TCP 服务器:

public void Initialize()
{
_serverListener = new TcpListener(new IPEndPoint(hostAddress, (int)port));
_serverListener.Start();

_threadDoBeginAcceptTcpClient = new Thread(() => DoBeginAcceptTcpClient(_serverListener));
_threadDoBeginAcceptTcpClient.Start();
}

...

    public void DoBeginAcceptTcpClient(TcpListener listener)
{
while(!_breakThread)
{
// Set the event to nonsignaled state.
TcpClientConnected.Reset();

// Start to listen for connections from a client.
Console.WriteLine("Waiting for a connection...");

// Accept the connection.
listener.BeginAcceptTcpClient(DoAcceptTcpClientCallback, listener);

// Wait until a connection is made and processed before continuing.
TcpClientConnected.WaitOne();
}
}

// Process the client connection.
public void DoAcceptTcpClientCallback(IAsyncResult ar)
{
// Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;

// End the operation and display the received data on the console.
Console.WriteLine("Client connection completed");
Clients.Add(listener.EndAcceptTcpClient(ar));

// Signal the calling thread to continue.
TcpClientConnected.Set();
}

WebAPI Controller :

public class ValuesController : ApiController
{
// GET api/values/5
public string Get(int id)
{
return $"Foo: {id}";
}
}

如前所述,我寻求的是WebAPI与windows服务之间的“通信”。如何将 WebAPI 调用中的“id”参数传递到我的 Windows 服务中的 _engineService 对象?也许类似于 WPF 的 MVVM Light Messenger?这个想法是它会被解析并发送到存储在客户端列表中的适当的 TcpClient。

任何关于如何实现这一目标的建议将不胜感激。请随时要求澄清/更多代码。

最佳答案

您找到问题的答案了吗?

我不太明白你在寻找他们两个之间的交流时试图达到什么目的?您是否希望以某种方式依赖 TCP/IP 来中继此 ID 或内存中的中继?

潜在地,您可以考虑调解器模式并使用这种在我理解的情况下似乎非常有用的库:https://github.com/jbogard/MediatR

在更简单的方法中,我将依靠事件来实现您想要做的事情,即从 HTTP 请求到 C++ 用户的响应式(Reactive)通信。

我了解您的需求吗?我很好奇解决方案

关于c# - Topshelf 服务(充当 TCP 服务器)与自托管 OWIN WebAPI 之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41556103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com