0

我正在尝试将我的搜索结果归档为一个字词

  1. 在异步控制器中使用 Bing API
  2. 使用实体框架将它们插入数据库

使用 Bing API 并将它们插入到使用实体框架的数据库中。无论出于何种原因,它都会返回 50 个结果,但随后会将 100 个结果输入数据库。

我的控制器代码:

public class DHWebServicesController : AsyncController
    {
        //
        // GET: /WebService/

        private DHContext context = new DHContext();


        [HttpPost]
        public void RunReportSetAsync(int id)
        {
            int iTotalCount = 1;
            AsyncManager.OutstandingOperations.Increment(iTotalCount);
            if (!context.DHSearchResults.Any(xx => xx.CityMarketComboRunID == id))
            {

                string strBingSearchUri = @ConfigurationManager.AppSettings["BingSearchURI"];
                string strBingAccountKey = @ConfigurationManager.AppSettings["BingAccountKey"];
                string strBingUserAccountKey = @ConfigurationManager.AppSettings["BingUserAccountKey"];
                CityMarketComboRun cityMarketComboRun = context.CityMarketComboRuns.Include(xx => xx.CityMarketCombo).Include(xx => xx.CityMarketCombo.City).First(xx => xx.CityMarketComboRunID == id);

                var bingContainer = new Bing.BingSearchContainer(new Uri(strBingSearchUri));
                bingContainer.Credentials = new NetworkCredential(strBingUserAccountKey, strBingAccountKey);
                // now we can build the query

                Keyword keyword = context.Keywords.First();
                var bingWebQuery = bingContainer.Web(keyword.Name, "en-US", "Moderate", cityMarketComboRun.CityMarketCombo.City.Latitude, cityMarketComboRun.CityMarketCombo.City.Longitude, null, null, null);
                var bingWebResults = bingWebQuery.Execute();

                context.Configuration.AutoDetectChangesEnabled = false;

                int i = 1;
                DHSearchResult dhSearchResult = new DHSearchResult();
                List<DHSearchResult> lst = new  List<DHSearchResult>();
                var webResults = bingWebResults.ToList();
                foreach (var  result in webResults)
                {
                    dhSearchResult = new DHSearchResult();
                    dhSearchResult.BingID = result.ID;
                    dhSearchResult.CityMarketComboRunID = id;
                    dhSearchResult.Description = result.Description;
                    dhSearchResult.DisplayUrl = result.DisplayUrl;
                    dhSearchResult.KeywordID = keyword.KeywordID;
                    dhSearchResult.Created = DateTime.Now;
                    dhSearchResult.Modified = DateTime.Now;
                    dhSearchResult.Title = result.Title;
                    dhSearchResult.Url = result.Url;
                    dhSearchResult.Ordinal = i;
                    lst.Add(dhSearchResult);
                    i++;
                }

                foreach (DHSearchResult c in lst)
                {
                    context.DHSearchResults.Add(c);
                    context.SaveChanges();

                }
                AsyncManager.Parameters["message"] = "The total number of results was "+lst.Count+".  And there are " + context.DHSearchResults.Count().ToString();
            }
            else
            {
                AsyncManager.Parameters["message"] = "You have already run this report";
            }
            AsyncManager.OutstandingOperations.Decrement(iTotalCount);
        }
        public string RunReportSetCompleted(string message)
        {
            string str = message;
            return str;
        }

    }

这是我从我的 asp.net mvc 4 页面调用它的方式。

@Ajax.ActionLink("Run Report", "GatherKeywordsFromBing", "DHWebServices", 
                new { id=item.CityMarketComboRunID}, 
                new AjaxOptions { OnSuccess = "ShowNotifier();", UpdateTargetId = "TopNotifierMessage", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, LoadingElementId = strCityMarketComboProgressID, LoadingElementDuration = 1000 }, 
                new { @class = "ViewLink" }) 
            <span class="ProgressIndicator" id="@strCityMarketComboProgressID"><img src="@Url.Content("~/Content/img/SmallBall.gif")" alt="loading" /></span>

不管出于什么原因

4

1 回答 1

0

尝试只保存一次:

foreach (DHSearchResult c in lst)
{
    context.DHSearchResults.Add(c);
}
context.SaveChanges();

此外,您的代码中没有任何异步内容,因此没有必要使用异步控制器。它不仅不会改善任何事情,而且可能会使事情变得更糟。

于 2012-08-15T15:49:53.167 回答