[英]Generated self-contained executable file failed to run after I close Visual Studio
我在運行生成的自包含可執行文件時遇到了一個問題。 當我打開 Visual Studio 時, .exe
文件運行良好。 但是,一旦我關閉 Visual Studio,它就會失敗。 我想這是因為我的 WCF 服務不是自托管的,但是怎么做呢? 誰能告訴我?
打開 Visual Studio 時的結果:
Uploading...
1
2
Upload Finished!
關閉visual studio時的結果:
Uploading...
1
未處理的異常:System.AggregateException:發生一個或多個錯誤。 (無法建立連接,因為目標機器主動拒絕它)---> System.ServiceModel.CommunicationException:無法建立連接,因為目標機器主動拒絕它---> System.Net.Http.HttpRequestException:沒有連接可以建立因為目標機器主動拒絕它---> System.Net.Sockets.SocketException: 由於目標機器主動拒絕它,無法建立連接
在 System.Net.Http.ConnectHelper.ConnectAsync(字符串主機,Int32 端口,CancellationToken 取消令牌)
--- 內部異常堆棧跟蹤結束 ---
在 System.Net.Http.ConnectHelper.ConnectAsync(字符串主機,Int32 端口,CancellationToken 取消令牌)
在 System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask1.get_Result()
1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask
在 System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask1 creationTask) at System.Threading.Tasks.ValueTask
1.get_Result() 在 System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancelationToken) 在 System .Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage 請求,Uri authUri,ICredentials 憑證,布爾 preAuthenticate,布爾 isProxyAuth,布爾 doRequestAuth,HttpConnectionPool 池,CancellationToken 取消令牌) 在 System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage 請求,CancellationToken 取消令牌)在 System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.ServiceModel.Channels.HttpChannelFactory
) 在 System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at System.ServiceModel.Channels.HttpChannelFactory
1 .HttpClientRequestChannel.H ttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper) --- End of internal exception stack trace --- at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result) at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End( SendAsyncResult 結果) 在 System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result) 在 System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass2_0.b__0(IAsyncResult asyncResult) --- 結束內部異常堆棧跟蹤 --- 在 System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancelationToken) at System.Threading.Tasks.Task.Wait() at sharepoint.Program.Main() in C:\\Temp\\ tmp\\fire\\SFFD\\sharepoint\\WebService\\sharepoint\\Program.cs:line 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。
是的,伙計。 我們需要在使用它之前托管服務。 ServiceClient 是客戶端代理類的實例。 它是通過添加服務引用自動生成的。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
在您的項目中,服務器和客戶端在同一台機器上。 我們可以將客戶端拆分成一個單獨的項目,通過添加服務引用來生成客戶端代理類。
在服務器端,您可以參考以下代碼。 它也是自托管的。
using (ServiceHost sh = new ServiceHost(typeof(MyService)))
{
sh.Open();
Console.WriteLine("Service is ready....");
Console.ReadLine();
sh.Close();
}
在使用 WCF 服務之前,我們應該首先啟動服務器項目。
如果有什么我可以幫忙的,請隨時告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.