0

用語の検索結果をアーカイブしようとしています

  1. 非同期コントローラーでの Bing API の使用
  2. Entity Frameworkを使用してそれらをデータベースに挿入する

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 に答える