[英]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.