从Windows服务中承载WCF服务.无法联系到

本文关键字:服务 联系 WCF Windows | 更新日期: 2023-09-27 18:29:25

所以我有一个作为本地系统运行的windows服务。

该windows服务然后启动WCF服务。

从我的机器上看,没有问题,工作正常
从测试控制台应用程序中,在目标机器上,它运行良好
在目标计算机上的windows服务中,它不起作用。它也没有抛出异常。。。

我真的很纠结(

这可能是权限吗?

m_tknCancelToken = new CancellationTokenSource();
/**************************************************************************************/
/***  Create and start the task                                                     ***/
/**************************************************************************************/
m_tskService = Task.Factory.StartNew((object o) => 
{
    RunService();
}, 
m_tknCancelToken);
/**************************************************************************************/
/***  Set the handler when the task is cancelled or faulted                         ***/
/**************************************************************************************/
m_tskService.ContinueWith(
    TaskEndedHandler, 
    TaskContinuationOptions.OnlyOnFaulted);
m_tskService.ContinueWith(
    TaskEndedHandler,
    TaskContinuationOptions.OnlyOnCanceled);

然后捕捉错误。

private void TaskEndedHandler(Task tskTask)
{
    Log.Log(String.Format("{0} has ended", ServiceName), "WHS010CI");
    if (tskTask.Exception != null)
    {
        Log.LogEx(tskTask.Exception, "WHS0103E");
        if (tskTask.Exception.InnerExceptions != null)
        {
            foreach (Exception ex in tskTask.Exception.InnerExceptions)
            {
                Log.LogEx(ex, "WHS0104E");
            }
        }
    }
    if(tskTask.IsCanceled)
    {
        Log.Log(String.Format("[{0}] has been cancelled", ServiceName), "WHS0104W");
    }
}

从Windows服务中承载WCF服务.无法联系到

和往常一样,这是一个愚蠢的错误。

在我的控制台应用程序中,我将SSL证书绑定到一个端口,这被删除了,因为当权者不希望在生产代码中这样做,这是可以理解的。所以我删除了它,然后有一个单独的批处理文件或其他必须手动运行的文件。。。然而,这是我忘记做的

对于那些感兴趣的人,下面是我的测试应用程序中的代码。

process = new Process();
process.StartInfo = BindCertToPort(port, certificate);
process.Start();

方法:

private static ProcessStartInfo BindCertToPort(int iPort, X509Certificate2 certificate, bool bRemove = false)
{
    string strAction = null;
    string strExtraArguments = null;
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    startInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");
    if (bRemove)
    {
        strAction = "delete";
    }
    else
    {
        strAction = "add";
        strExtraArguments = string.Format(" certhash={0} appid={{{1}}}", certificate.Thumbprint, Guid.NewGuid());
    }
    startInfo.Arguments = string.Format("http {0} sslcert ipport=0.0.0.0:{1}{2}", strAction, iPort, strExtraArguments);
    startInfo.RedirectStandardOutput = true;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    return startInfo;
}