简体   繁体   中英

windows phone 8 (connectivity with web api) issue

i am asking the general question here. i really don't know what to do here. i have developed a windows phone 8 mobile app with back end as the web service which is a web API service. it has around 4 to 5 screens.

the problem: when i first load my app and let everything load(not INTERRUPTING the fetching from the webapi operation by going to the second window or the third window or the 4th windows by the application bar and starting another fetching of the records). it works fine.

but if i once let the first loading run and go for the second fetching of records. it creates huge problems. (delay , returning null etc). any idea how to overcome this problem with the second fetching while the first fetching is running. is this a general problem? or only i am having this problem?

this is my code which i am using

 private static readonly HttpClient client;

    public static Uri ServerBaseUri
    {
        get { return new Uri("http://169.254.80.80:30134/api/"); }
    }

    static PhoneClient()
    {        
       client =new HttpClient();
       client.MaxResponseContentBufferSize = 256000;
       client.Timeout = TimeSpan.FromSeconds(100);
       client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    }      

    public async static Task<List<Categories>> GetDefaultCategories()
    {      
        HttpResponseMessage getresponse = await client.GetAsync(ServerBaseUri + "Categorys");                      
        string json = await getresponse.Content.ReadAsStringAsync();         
        json = json.Replace("<br>", Environment.NewLine);
        var categories = JsonConvert.DeserializeObject<List<Categories>>(json);
        return categories.ToList();
    }

There are two main problems as Vladimir points out in his comment. You need to create a new instance of HttpClient for each request and I'd also recommend not using statics.

public class DataService
{
  public HttpClient CreateHttpClient()
  {
       var client = new HttpClient();
       client.MaxResponseContentBufferSize = 256000;
       client.Timeout = TimeSpan.FromSeconds(100);
       // I'm not sure why you're adding this, I wouldn't
       client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
       return client;
  }

  public async Task<List<Categories>> GetDefaultCategories()
  {
        var client = CreateHttpClient();
        HttpResponseMessage getresponse = await client.GetAsync(ServerBaseUri + "Categorys");                      
        string json = await getresponse.Content.ReadAsStringAsync();         
        json = json.Replace("<br>", Environment.NewLine);
        var categories = JsonConvert.DeserializeObject<List<Categories>>(json);
        return categories.ToList();
  }
}

If you absolutely must you statics, and I'm not recommending this technically, but, you can have an instance of this service statically accessible off your app class to get it up and running easily. I'd prefer a dependency injection technique. The important part is that you limit your static instances. If I have any in my code I tend to hang them off of the main App class.

public class App
{
  public static DataService DataService { get; set; }

  static App()
  {
    DataService = new DataService();
  }
  // other app.xaml.cs stuff
}

Then anywhere in your code you could call:

var categories = await App.DataService.GetDefaultCategories();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM