开发者

FatSecret C# wrapped api returns null response in WPF

I have an interesting problem with the FatSecret C# api. It works fine in a C# Console application, but just will not work in a WPF or C# Forms application. The response from the server is always null. I've triple checked the security keys, compiled it for different versions of the .Net framework, and nothing really helped.

Has someone encountered similar problems?

My alternative solution would be to make a DLL from the console application (since I need just a few functions from the API), and reference it from my WPF project, but I'm not quite sure what should I do to make it work.

Here is a example of the code in C# forms, made just for this.

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.GetRe开发者_如何学编程sponseSynchronously(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());
            }

        }
    }
}

Thank you in advance


Is there an app.config that is generated? That needs to be at the exe level of your application.


Using FatSecretSharp into a WinForm/WPF App You must put the request into a BackgroundWorker's DoWork(object sender, DoWorkEventArgs e) method

e.g.:

  1. Create a new winform/wpf app and add the FatSecretSharp.Services to the refs
  2. Add a button with its click listener and a BackgroundWorker with its DoWork() listener to the form
  3. Encapsulate the FoodSearchExample() method from the "FatSecretSharp.Examples.ConsoleApp" into the DoWork(object sender, DoWorkEventArgs e) listener of the backgroundworker component:

.

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: I changed the Console.WriteLine to Debug.WriteLine


i faced a same problem with the FatSecret API. for this i just add the FatsecretSharp.win dll which is availabe in "platform.fatsecret.com" and just create a web service using the same code as written in console app. By creating the same i solved my problem.

below is the code which is written inthe web service. this is used to search the food items. you just need to add the fatsecretsharp.win.dll which will you get from Fatsecretapis sample code on web site.

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


        }

           }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜