SignalR 客户端 - Titanium Web 代理 - 工作者服务

问题描述

我想制作一个钛代理服务器作为工作服务(.net 5),用于捕获网络流量并通过 SignalR(客户端信号器)发送一些 http 响应。当我将服务作为 Windows 服务运行时会出现问题。如果我从 Visual Studio 调试或发布模式运行它,它会按预期工作。 我还使用此配置文件设置发布了它:

enter image description here

它与 Monitoring.exe 也能正常工作,但是当我将它作为 Windows 服务启动时它不起作用。

为了创建 Windows 服务,我使用 PowerShell(管理员)和这个命令:

sc.exe create Monitoring binpath= C:\Users\user\Documents\DeploymentFolder\Monitoring.exe start= auto

到目前为止我做了什么:

private void InitializeProxyServer()
{
    proxyServer.BeforeRequest += ProxyServer_BeforeRequest;
    proxyServer.ServerCertificateValidationCallback += ProxyServer_ServerCertificateValidationCallback;
    proxyServer.ClientCertificateSelectionCallback += ProxyServer_ClientCertificateSelectionCallback;

    explicitProxyEndPoint = new ExplicitProxyEndPoint(IPAddress.Any,config.GetSection("ExplicitProxyEndpoint").GetValue<int>("Port"),true);
    explicitProxyEndPoint.BeforeTunnelConnectRequest += ExplicitEndPoint_BeforeTunnelConnectRequest;

    proxyServer.AddEndPoint(_explicitProxyEndPoint);
    proxyServer.CertificateManager.EnsureRootCertificate(false,false,false);
}
private void StartProxyService()
{
     proxyServer.Start();            
     proxyServer.SetAsSystemHttpProxy(explicitProxyEndPoint);
     proxyServer.SetAsSystemHttpsProxy(explicitProxyEndPoint);          
}
private void InitializeHubConnection()
{
    var url = config.GetSection("SignalRHub").GetValue<string>("Url");
    connection = new HubConnectionBuilder()
       .WithUrl(url)
       .WithAutomaticReconnect()
       .Build();           
}
private async Task ConnectToSignalrserverHub()
{
    try
    {
        await _connection.StartAsync();
        _logger.Loginformation("Connection to SignalR Server Hub started.");
    }
    catch (Exception ex)
    {
        _logger.LogError(ex.Message);
    } 
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
    InitializeProxyServer();
    StartProxyService();

    InitializeHubConnection();
    await ConnectToSignalrserverHub();

    await base.StartAsync(cancellationToken);
}

我也使用 DI

public static IHostBuilder CreateHostBuilder(string[] args)
{
    return Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureServices((hostContext,services) =>
                {
                    services.AddHostedService<Worker>();
                    services.AddSingleton<ProxyServer>();
                });
}

从 Windows 事件查看器我得到这个错误

- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Monitoring" /> 
  <EventID Qualifiers="0">0</EventID> 
  <Version>0</Version> 
  <Level>2</Level> 
  <Task>0</Task> 
  <Opcode>0</Opcode> 
  <Keywords>0x80000000000000</Keywords> 
  <TimeCreated SystemTime="2021-03-18T18:46:57.5860079Z" /> 
  <EventRecordID>10024</EventRecordID> 
  <Correlation /> 
  <Execution ProcessID="0" ThreadID="0" /> 
  <Channel>Application</Channel> 
  <Computer>User1</Computer> 
  <Security /> 
  </System>
- <EventData>
  <Data>Category: Monitoring.Worker EventId: 0 The SSL connection Could not be established,see inner exception.</Data> 
  </EventData>
  </Event>

我缺少什么并且无法按预期工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)