CoreWCF 转换工作正常,但在制作 exe 文件后但未启动

问题描述 投票:0回答:1

我刚刚将 WCF ServiceHost 转换为 CoreWCF。之后,当我准备exe文件时,我无法启动exe并启动进程。我尝试过以下:

public WebApplication CreateHost()
 {

     var builder = WebApplication.CreateBuilder();
         builder.Services.AddServiceModelServices();
         builder.Services.AddServiceModelMetadata();
         builder.Services.AddSingleton<IServiceBehavior,           UseRequestHeadersForMetadataAddressBehavior>();
        
         var app = builder.Build();
 
         app.UseServiceModel(serviceBuilder =>
         {
            
             serviceBuilder.AddService<MaintenanceService>();
             serviceBuilder.AddServiceEndpoint<TestService, ITestService>(TestSettings.BINDING, TestSettings.ADDRESS);
             serviceBuilder.ConfigureServiceHostBase<ITestService>(serviceHost =>
             {
                 var behavior = new ServiceBehaviorAttribute();
                 behavior.InstanceContextMode = InstanceContextMode.Single;
                 behavior.ConcurrencyMode = ConcurrencyMode.Multiple;
                 serviceHost.Description.Behaviors.Add(behavior);
                 
             });

         });
   
         app.StartAsync();
         return app;
}
public static class TestSettings
{
    public const string SERVICE_NAME = "Test";
    public const string ADDRESS = "net.pipe://localhost/test/maintenance";
    public static readonly Binding BINDING = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { ReceiveTimeout = TimeSpan.MaxValue, CloseTimeout = TimeSpan.MaxValue, SendTimeout = TimeSpan.MaxValue };
    
}

此 TestSettings 也用于使用 ServiceModel 的客户端。现在的问题是,如果我为服务器创建一个 exe 文件并安装它,它不会作为服务安装和运行。它不会启动。我在下面使用过:

var path = @"Dir"
var p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.WorkingDirectory = path;
p.StartInfo.FileName = path + "\Test.exe";
p.StartInfo.Verb = "runas";
p.StartInfo.Arguments = "-install";
await Task.Run(() =>
{ 
p.Start();
p.WaitForExit();
});

如果我使用 SC Create 命令手动安装,它可以安装但无法启动。

给出以下错误:

Windows 无法在本地计算机上启动测试服务。 错误1053:服务没有及时响应启动或控制请求

我见过类似的问题,但没有提到他是如何解决的。

我已经检查过,如果我创建 exe 并安装为服务,旧的 WCF(System.ServiceModel) 工作正常。它可以轻松启动。

wcf corewcf
1个回答
0
投票

我可以通过在调用之前添加以下代码来解决此问题

.Build();

builder.Services.AddWindowsService(options =>
{
     options.ServiceName = "Service Name";
});

例如

var builder = WebApplication.CreateBuilder();
builder.Services.AddServiceModelServices();
builder.Services.AddServiceModelMetadata();
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
    
builder.Services.AddWindowsService(options =>
{
     options.ServiceName = "Service Name";
});

var app = builder.Build();

我从 this Microsoft 页面获得有关使用 dotnet core 创建 Windows 服务的信息。

© www.soinside.com 2019 - 2024. All rights reserved.