简体   繁体   中英

Generated self-contained executable file failed to run after I close Visual Studio

I came across a problem when I run my generated self-contained executable file. The .exe file runs fine when I have Visual Studio open. However, it fails once I close Visual Studio. I think this is because my WCF service is not self-hosted, but how to do it? Can anyone tell me?

Results when I have Visual Studio open:

Uploading...
1
2
Upload Finished!

Result when I closed visual studio:

Uploading...
1

Unhandled Exception: System.AggregateException: One or more errors occurred. (No connection could be made because the target machine actively refused it) ---> System.ServiceModel.CommunicationException: No connection could be made because the target machine actively refused it ---> System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it

at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask 1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask
1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask
1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask
1.get_Result()

at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask 1 creationTask) at System.Threading.Tasks.ValueTask 1.get_Result() at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task 1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.ServiceModel.Channels.HttpChannelFactory 1.HttpClientRequestChannel.H ttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper) --- End of inner exception stack trace --- at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass2_0.b__0(IAsyncResult asyncResult) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.Wait() at sharepoint.Program.Main() in C:\\Temp\\tmp\\fire\\SFFD\\sharepoint\\WebService\\sharepoint\\Program.cs:line 16

Here is my code:

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();
                }
            }
        }
    }

For some reason, ServiceHost is not recognized.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM