首页 > 解决方案 > 关闭 Visual Studio 后生成的自包含可执行文件无法运行

问题描述

我在运行生成的自包含可执行文件时遇到了一个问题。.exe当我打开 Visual Studio 时,该文件运行良好。但是,一旦我关闭 Visual Studio,它就会失败。我认为这是因为我的 WCF 服务不是自托管的,但是该怎么做呢?谁能告诉我?

打开 Visual Studio 时的结果:

Uploading...
1
2
Upload Finished!

我关闭视觉工作室时的结果:

Uploading...
1

未处理的异常:System.AggregateException:发生一个或多个错误。(无法建立连接,因为目标机器主动拒绝它)---> System.ServiceModel.CommunicationException:无法建立连接,因为目标机器主动拒绝它---> System.Net.Http.HttpRequestException:没有连接可以因为目标机器主动拒绝它而建立---> System.Net.Sockets.SocketException: No connection could be made because the target machine主动拒绝它

在 System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancelToken)
--- 内部异常堆栈跟踪结束 ---
在 System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancelToken )
在 System.Threading.Tasks.ValueTask 1.get_Result() 在 System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask

1 creationTask) at System.Threading.Tasks.ValueTask1.get_Result() 在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancelToken) 在 System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth , Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancelToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancelToken) at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancelToken) at System.Net.Http .HttpClient.FinishSendAsyncUnbuffered(任务1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.ServiceModel.Channels.HttpChannelFactory1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.SendRequestAsync(消息消息,TimeoutHelper timeoutHelper)---内部异常堆栈跟踪结束---在System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult结果)在System.ServiceModel.Channels.ServiceChannel。在 System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass2_0.b__0(IAsyncResult asyncResult) - System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) 处的 SendAsyncResult.End(SendAsyncResult 结果) - -- 内部异常堆栈跟踪结束 --- 在 System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancelToken) at System.Threading.Tasks.Task.Wait() at sharepoint.Program.Main() 在 C :\Temp\tmp\fire\SFFD\sharepoint\WebService\sharepoint\Program.cs:第 16 行

这是我的代码:

using ServiceReference1;
using System;
using System.Threading.Tasks;
using System.ServiceModel;

    namespace sharepoint
    {
        class Program
        {
            static void Main()
            {

                using (ServiceHost host = new ServiceHost(typeof(WCFService2.Service)))
                {
                    host.open();
                    Console.WriteLine("Uploading...");
                    //ServiceReference1.ServiceClient ws = new ServiceReference1.ServiceClient();
                    ServiceClient client = new ServiceClient();
                    Console.WriteLine("1");
                    Task.Run(() => client.start_processAsync()).Wait();
                    Console.WriteLine("2");
                    Console.WriteLine("Upload Finished!");
                    Console.ReadLine();
                }
            }
        }
    }

由于某种原因,无法识别 ServiceHost。

标签: c#wcf.net-corevisual-studio-2019

解决方案


Yes, buddy. We need to host the service before consuming it. The ServiceClient is the instance of the client proxy class. It is auto-generated by adding service reference.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
In you project, the server and the client is on the same machine. We could separate the client-side into an individual project, generate the client proxy class by adding service reference.
On the server-side, you could refer to the below code. It also is self-hosted.

using (ServiceHost sh = new ServiceHost(typeof(MyService)))
            {
                sh.Open();
                Console.WriteLine("Service is ready....");

                Console.ReadLine();
                sh.Close();
            }

Before consuming the WCF service, we should start the server project at first.
Feel free to let me know if there is anything I can help with.


推荐阅读