c# - 关闭 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.ValueTask1 creationTask) at System.Threading.Tasks.ValueTask
1.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.HttpChannelFactory
1.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。
解决方案
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.
推荐阅读
- java - 如何在 JSON 正文中传递单个参数,为什么我会出现异常?
- python - 至强处理器混淆的 CPU 计数
- html - 如何找出哪个 CSS 元素强制指定大小?
- typescript - 默认 vue 应用程序未在 localhost 上加载
- c# - 如何在 C# 中搜索标签标签属性?
- python-3.x - 将数据插入 CrateDb 时出现编译器状态错误
- c# - 如何拆分界面是每个界面有一个动作还是每个界面分组类似的动作?
- python - Python 以适当的格式将字典导出到 csv
- neo4j - 如何替换 neo4j 关系中的属性
- android - 您如何必须从 firebase 数据库中洗牌