【问题标题】:FatSecret C# wrapped api returns null response in WPFFatSecret C# 包装的 api 在 WPF 中返回空响应
【发布时间】:2011-05-22 09:52:36
【问题描述】:

我对@9​​87654321@ 有一个有趣的问题。它在 C# Console 应用程序中工作正常,但在 WPF 或 C# Forms 应用程序中不起作用。来自服务器的响应始终为空。我对安全密钥进行了三次检查,针对不同版本的 .Net 框架进行了编译,但没有任何帮助。

有人遇到过类似的问题吗?

我的替代解决方案是从控制台应用程序创建一个 DLL(因为我只需要 API 中的一些函数),并从我的 WPF 项目中引用它,但我不太确定我应该做什么它工作。

这是一个 C# 形式的代码示例,专门为此而制作。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string consumerKey = "hidden";
                string consumerSecret = "hidden";

                FoodSearch fs = new FoodSearch(consumerKey, consumerSecret);

                var response = fs.GetResponseSynchronously(new FoodSearchRequest()
                {
                    SearchExpression = this.textBox1.Text
                });

                if (response.HasResults)
                {
                    foreach (var food in response.foods.food)
                    {
                        string name = food.food_name;

                        listBox1.Items.Add(name);
                    }

                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }

        }
    }
}

提前谢谢你

【问题讨论】:

    标签: c# wpf dll console response


    【解决方案1】:

    是否有生成的 app.config?这需要在您的应用程序的 exe 级别。

    【讨论】:

    • 是的。我开始怀疑这是一个与线程相关的问题,因为我想这个 API 会启动一个新线程。在黑暗中打...
    • @Aleksandar:它是开源的,你为什么不直接进入代码?
    【解决方案2】:

    在 WinForm/WPF 应用程序中使用 FatSecretSharp 您必须将请求放入 BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) 方法中

    例如:

    1. 创建一个新的 winform/wpf 应用并将 FatSecretSharp.Services 添加到引用中
    2. 向表单添加一个带有其点击监听器的按钮和一个带有DoWork() 监听器的BackgroundWorker
    3. 将“FatSecretSharp.Examples.ConsoleApp”中的FoodSearchExample()方法封装到 backgroundworker 组件的DoWork(object sender, DoWorkEventArgs e) 监听器:

    .

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    {
            var searchTerm = "apple";
    
            if (foodSearch == null) {
                foodSearch = new FoodSearch(consumerKey, consumerSecret);
            }
    
            var response = foodSearch.GetResponseSynchronously(new FoodSearchRequest()
            {
                SearchExpression = searchTerm
            });
    
            if (response.HasResults) {
                Debug.WriteLine("Got " + response.foods.food.Count + " Results: \n\n");
                var form = "id: {0}, \n - type: {1}, \n - name: {2}, \n - description: {3}";
                foreach (var food in response.foods.food) {
                    Debug.WriteLine(String.Format(form, food.food_id, food.food_type, food.food_name, food.food_description));
                }
            } else {
                Debug.WriteLine("No results from term: " + searchTerm);
            }
    }
    

    PS:我把Console.WriteLine改成了Debug.WriteLine

    【讨论】:

      【解决方案3】:

      我在使用 FatSecret API 时遇到了同样的问题。为此,我只需添加在“platform.fatsecret.com”中可用的 FatsecretSharp.win dll,并使用与控制台应用程序中编写的相同代码创建 Web 服务。 通过创建相同的内容,我解决了我的问题。

      下面是写在网络服务中的代码。这用于搜索食物。您只需添加您从网站上的 Fatsecretapis 示例代码中获得的 fatsecretsharp.win.dll。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Services;
      using FatSecretSharp.Services;
      using FatSecretSharp.Services.Requests;
      using System.Web.Script.Services;
      using System.Web.Script.Serialization;
      
      namespace WebService1
      {
          /// <summary>
          /// Summary description for Service1
          /// </summary>
          [WebService(Namespace = "http://tempuri.org/")]
          [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
          [System.ComponentModel.ToolboxItem(false)]
          // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
          // [System.Web.Script.Services.ScriptService]
          public class Service1 : System.Web.Services.WebService
          {
              private static string consumerKey = string.Empty;
              private static string consumerSecret = string.Empty;
      
              private static FoodSearch foodSearch;
      
      
              public Service1()
              {
      
                  //Uncomment the following line if using designed components 
                  //InitializeComponent(); 
              }
      
              [WebMethod]
              [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
              public string SearchFood(string searchExpression)
              {
                  try
                  {
                      consumerKey = "your consumer Key";
      
                      consumerSecret = "your consumer Secret ";
      
                      var searchTerm = searchExpression;
      
                      foodSearch = new FoodSearch(consumerKey, consumerSecret);
      
                      var response = foodSearch.GetResponseSynchronously(new FoodSearchRequest()
                                          {
                                              SearchExpression = searchTerm,
      
                                              MaxResults = 50,
                                              PageNumber = 0
                                          });
      
      
      
                      if (response != null && response.HasResults)
                      {
      
                          return new JavaScriptSerializer().Serialize(response);
                      }
                      else
                      {
      
                          return null;
                      }
                  }
                  catch (Exception ex)
                  {
                      return ex.Message;
                  }
      
      
              }
      
                 }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-02-13
        • 2018-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 2018-07-09
        • 1970-01-01
        相关资源
        最近更新 更多