c# - WinForms - 当应用程序启动时没有可用的 Internet 连接时 mscorlib.dll 中的 System.Reflection.TargetInvocationException
问题描述
我制作了一个简单的桌面应用程序(Winform,在 Visual Studio 2015 中,.NET 版本 4.5.2),它监听传入的 pusher.com 通知,并根据通知包含的内容执行各种操作。但是,有一些关于 Internet 连接的问题 - 如果启动时没有连接,应用程序会由于未处理的异常而崩溃:
An unhandled exception of type System.Reflection.TargetInvocationException occurred in in mscorlib.dll
InnerException
是,这No such host is known
是可以理解的,因为没有互联网连接。
如果StartPushing()
在应用程序运行时连接中断,则代码将继续尝试重新连接,并且不稳定的连接不是(最大的)问题。在应用程序运行之前它没有连接。
我也将它StartPushing()
从其中删除public Form1()1
并放置在其中Form1_Shown(...)
(认为它可能在一切设置之前被调用),但这并没有解决问题 - 应用程序在启动时不断崩溃。
这是我的代码。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using Newtonsoft.Json;
using PusherClient;
namespace PusherWebApps
{
public partial class Form1 : Form
{
private static Pusher _pusher;
public string channel, key;
public string event1, event2;
public string iniName = "PusherWebApps.ini";
public string type;
WebClient client = new WebClient();
public Form1()
{
InitializeComponent();
// https://stackoverflow.com/a/39308022
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
StartPushing();
}
void _pusher_ConnectionStateChanged(object sender, PusherClient.ConnectionState state)
{
fileLogging("Connection state: " + state.ToString());
}
void _pusher_Error(object sender, PusherException error)
{
fileLogging("Pusher Channels Error: " + error.ToString());
}
// Start
public async void StartPushing()
{
iniParams();
_pusher = new Pusher(key);
_pusher.ConnectionStateChanged += _pusher_ConnectionStateChanged;
_pusher.Error += _pusher_Error;
PusherClient.ConnectionState connectionState = await _pusher.ConnectAsync();
Channel _myChannel = await _pusher.SubscribeAsync(channel);
_myChannel.Bind("myevent", (dynamic incoming) =>
{
fileLogging(incoming.ToString());
dynamic tmp = JsonConvert.DeserializeObject(incoming.ToString());
dynamic data = JsonConvert.DeserializeObject(tmp.data.ToString());
dynamic message = JsonConvert.DeserializeObject(data.message.ToString());
dynamic header = JsonConvert.DeserializeObject(message.header.ToString());
type = header.type.ToString();
if (type.ToLower() == event1)
{
// do stuff
}
if (type.ToLower() == event2)
{
// do stuff 2
}
});
}
void _myChannel_Subscribed(object sender)
{
fileLogging("Subscribed!");
}
public void iniParams()
{
bool checkIni = File.Exists(Application.StartupPath + "\\" + iniName);
if(!checkIni)
{
fileLogging("INI missing!");
MessageBox.Show("INI missing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
var MyIniFile = new IniFile(Application.StartupPath + "\\" + iniName);
channel = MyIniFile.Read("channel", "Main").ToString();
key = MyIniFile.Read("key", "Main").ToString();
event1 = MyIniFile.Read("event1", "Main").ToString();
event2 = MyIniFile.Read("event2", "Main").ToString();
}
/************ BEGIN logging **********/
public void fileLogging(string text)
{
// do logging stuff
}
/********** END logging *********/
}
}
最后,我的问题。
我该如何处理这个错误?
我知道我可能会在启动时通知用户没有互联网连接,然后退出应用程序,但我想让它继续运行,并让它继续尝试在后台连接。我还想避免在后台使用单独的线程和计时器来检查互联网连接。
编辑 1
这是调试器指向的部分——而不是指向内部的某个地方Form1.cs
,它突出显示了这部分Program.cs
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // <== this is the one which the debugger highlights as the error
}
}
解决方案
推荐阅读
- django - 'Request' 对象没有'accepted_renderer',djangorestframework-datatables
- python - Python嵌套列表替换值
- node.js - 过滤节点 mongodb 中的对象数组
- reactjs - 无法使用序列化程序将 POST 请求从 React 前端发送到 Django Rest Framework?
- java - 并行流()和流()?
- java - Java Hadoop 生成没有结果
- javascript - 防止触发模糊事件的 Mousedown 事件处理程序
- java - javax.net.ssl.SSLPeerUnverifiedException:peer not authenticated for Demo Enviornment Only
- xml - 从项目中的另一个文件引用资源字典
- angular - 如何使用 restanular 以角度显示 api 数据