FatSecret C#包装的api在WPF中返回null响应

本文关键字:返回 null 响应 WPF 包装 api FatSecret | 更新日期: 2023-09-27 17:57:34

我有一个关于FatSecret C#api的有趣问题。它在C#控制台应用程序中运行良好,但在WPF或C#窗体应用程序中不起作用。来自服务器的响应始终为null。我已经三次检查了安全密钥,为不同版本的.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());
            }
        }
    }
}

提前感谢

FatSecret C#包装的api在WPF中返回null响应

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

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

例如:

  1. 创建一个新的winform/wpf应用程序,并将FatSecretSharp.Services添加到refs
  2. 在表单中添加一个带有单击侦听器的按钮和一个带有DoWork()侦听器的BackgroundWorker
  3. 将"FatSecretSharp.Examples.ConsoleApp"中的FoodSearchExample()方法封装到后台工作者组件的CCD_ 7侦听器:

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

我在FatSecret API中也遇到了同样的问题。为此,我只需添加"platform.fatsecret.com"中提供的FatsecretSharp.win dll,并使用控制台应用程序中编写的相同代码创建一个web服务。通过创造同样的东西,我解决了我的问题。

下面是在web服务中编写的代码。这是用来搜索食品的。您只需要添加fatsecretsharp.win.dll,它将从网站上的Fatsecretapis示例代码中获得。

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;
            }

        }
           }
}