我遇到了一个奇怪的错误。 这里是回顾。 我有一个util
类存储我所有的好东西,在一个应用程序(是不是太亮,我存储无关的东西,在一个类,代码礼仪等),它看起来是这样的:C#Selenium FirefoxDriver破坏我的代码我猜?
public static class util
{
public static IWebDriver driver = new PhantomJSDriver();
public static string spreadsheetId = "I won't show you my pantsu, senpai!";
//more definitions and some methods here....
}
我主要方法几乎是谷歌的快速入门应用程序的直接副本,看起来像这样:
static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "I";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Sheets API service.
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
string spreadsheetId = util.spreadsheetId;
//BELOW IS STUFF THAT DOESN'T GET EXECUTED DUE TO EXCEPTION, READ ON!
}
所以!事实上,从技术上来说,这很好。但是,由于一些测试相关的东西,我需要将我的util.driver
切换到new FirefoxDriver()
。并猜测是什么,当我这样做时,异常System.TypeInitializationException
“类型.util的初始化程序返回异常”由string spreadsheetId = util.spreadsheetId;
行引发。 这怎么可能? 此外,firefoxdriver所需的geckodriver存在于文件夹中,但控制台确实对“远程主机返回错误:404找不到”大惊小怪,不知道该怎么说 - PhantomJS没有发生。
是啊,谢谢!事实证明,我在util中的一个静态成员初始化是行为不端 - 将它们逐个移到方法中,并找出了这个bug。 –