为什么我尝试从另一个类调用Async方法时遇到异常

时间:2017-04-13 16:12:30

标签: c# exception asynchronous

我对使用异步并不感兴趣,但我发现我需要它来阅读网页,如果我将异步方法放入我的按钮处理程序方法中,我可以完美地运行这些功能用户界面,但如果我尝试将异步功能移入其自己的类并从按钮处理程序方法访问它,我会得到模糊的异常。

示例:

在此课程中打包我的网页抓图代码:

public class WebReader
{
    public string completedSearchURL;
    public string searchPageOutput;
    public string completedProductPageURL;
    public string productPageOutput;



     public async Task QueryProductPage(string sku)
    {
        HttpClient httpClient = new HttpClient();
        Uri searchURL = new Uri(("google.com/search" + SKU + "**additional URL content**"));
        completedSearchURL = searchURL.ToString();

        string productPageOutput;

        try
        {
            searchPageOutput = await httpClient.GetStringAsync(searchURL);

        }
        catch (Exception)
        {
            throw;
        }

        searchPageOutput = searchPageOutput.Replace("</", "</" + System.Environment.NewLine);
        Uri productPageUrl = new Uri(("www.google.com/" + BetweenTwoStrings(searchPageOutput)));
        completedProductPageURL = productPageUrl.ToString();

        try
        {
            productPageOutput = await httpClient.GetStringAsync(productPageUrl);
        }
        catch (Exception)
        {

            throw;
        }


    }


    public string BetweenTwoStrings(string fullString)
    {
        string STR = fullString;
        string firstString = "<a href=\"/****/*****/****/****/";
        string secondString = "\" pid";
        string finalString;

        int Pos1 = STR.IndexOf(firstString) + firstString.Length;

        int Pos2 = STR.IndexOf(secondString);

        finalString = STR.Substring(Pos1, Pos2 - Pos1);

        return finalString;

    }
}

并试图像这样访问它:

private async void _testButtonQueryWeb_Click(object sender, RoutedEventArgs e)
    {
        WebReader wr = new WebReader();

        await wr.QueryProductPage(_testTextInputBoxWebAddress.Text);
        _testLinkDisplayPostSearch.Text = wr.searchPageOutput;
        _testFullyConstructedLink.Text = wr.completedSearchURL;
        _testTextBoxWeb.Text = wr.productPageOutput;

    }

不起作用。

但是使用这样的代码会:

private async void _testButtonQueryWeb_Click(object sender, RoutedEventArgs e)
    {
        string completedSearchURL;
        string searchPageOutput;
        string completedProductPageURL;
        string productPageOutput;

    HttpClient httpClient = new HttpClient();
        Uri searchURL = new Uri(("google.com/search" + _testTextInputBoxWebAddress + "**additional URL content**"));
        completedSearchURL = searchURL.ToString();



        try
        {
            searchPageOutput = await httpClient.GetStringAsync(searchURL);

        }
        catch (Exception)
        {
            throw;
        }

        searchPageOutput = searchPageOutput.Replace("</", "</" + System.Environment.NewLine);
        Uri productPageUrl = new Uri(("www.google.com/" + BetweenTwoStrings(searchPageOutput)));
        completedProductPageURL = productPageUrl.ToString();

        try
        {
            productPageOutput = await httpClient.GetStringAsync(productPageUrl);
        }
        catch (Exception)
        {

            throw;
        }



        _testLinkDisplayPostSearch.Text = searchPageOutput;
        _testFullyConstructedLink.Text = completedSearchURL;
        _testTextBoxWeb.Text = productPageOutput;

    }

应用程序运行正常,直到我点击Query Web按钮然后它抛出此异常。 Exception

这是来自日志文件的错误消息:抛出异常:&#39; System.IO.FileLoadException&#39;在System.Private.CoreLib.ni.dll

毋庸置疑,我很少失去和沮丧,我真的很欣赏我可能做错的一点指导,更多的是为什么技术,从我迄今为止所学到的关于编程应该在这种情况下工作不要。

提前致谢!

0 个答案:

没有答案