Using restsharp in c# to consume last.fm services - restsharp

I'm trying to connect to the last.fm rest services using restsharp. I can deserialize the simple data found at the example: http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Cher&api_key=xxxxxxxxxxxxxxxxxxxxxxx
however, when i reach the images section for the artist:
<artist>
<name>Cher</name>
<mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
<url>http://www.last.fm/music/Cher</url>
<image size="small">http://userserve-ak.last.fm/serve/34/48511693.png</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/48511693.png</image>
<image size="large">http://userserve-ak.last.fm/serve/126/48511693.png</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/252/48511693.png</image>
<image size="mega">http://userserve-ak.last.fm/serve/500/48511693/Cher+Tess.png</image>
i am struggling to get the library to map the data. Here is the code I have so far:
namespace *******.Core.LastFm
{
using System.Xml.Serialization;
using System.Collections.Generic;
using System;
public class image
{
[XmlAttribute]
public string Size { get; set; }
public string Value { get; set; }
}
public class ArtistImageCollection : List<image> { }
public class Artist
{
public string Name { get; set; }
public string Mbid { get; set; }
public string Url { get; set; }
[XmlArray]
public ArtistImageCollection Image;
}
}
this doesnt work. does anyone know how to bind this?
[i updated it to reflect nics suggestion - this doesn't work still]
i got the basis for this code from: http://www.aaronstannard.com/post/2010/06/14/How-to-Parse-a-Users-Delicious-Feed-with-RestSharp.aspx
w://

there was no get/set on Image
doh

Dont you need to annotate the Size with [Attribute] and Image with [XmlArray] or something weird like that?

Related

ASPNET Eager Loading doesn't work for HashSet

I'm working on a project which has Database-First approach.
In the said project, I have Offer.OfferPlaces and OfferPlaces.Offer relation.
The first one doesn't seem to work, loading it via Include(x => x.OfferPlaces) yields empty collection, while loading Include(x => x.Offer) works fine and loads related Offer.
Is that a common issue in EFCore, do I have to implement a workaround of sorts or perhaps add something to OnModelCreating of the DbContext?
Here's the Offer model:
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
namespace MarketplaceOffe.DAL.Model
{
[Table("Offers")]
public partial class Offer
{
public Offer()
{
OfferPlaces = new HashSet<OfferPlace>();
}
public long Id { get; set; }
public long CompanyId { get; set; }
public virtual ICollection<OfferPlace> OfferPlaces { get; set; }
}
}
And the OfferPlace model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace MarketplaceOffe.DAL.Model
{
[Table("OfferPlaces")]
public partial class OfferPlace
{
public long Id { get; set; }
public long OfferId { get; set; }
public virtual Offer Offer { get; set; }
}
}

Exception while retrieving data using Entity framework (Code first)

I am not sure what the protocol is w.r.t. to changing your question but I guess it would be better if I change it as there is a lot of information and the original intent of asking the question has been lost.
Original question:
I was originally having an issue while generating a database using EF (Code first approach / POCO classes).
One of the issues was that I was making use of Constructors in my entity classes to initialize the members. After #RickStahl suggested that it is not required, I changed my implementation.
For the sake of readers, I didn't want the information to be lost as some of the old comments intend to adress that issue.
However, during the course of time since this thread was initially created, the situation has changed. I have been able to get over some of the issues.
Current Issue:
I am having problem retrieving the content from the underlying database tables.
Exception:
Object reference not set to an instance of an object.
The exception occurs at the following statement of Program.cs (Line 21):
foreach(PhoneNumber p in cd.Phones)
For the sake of ease in understanding the issue, I am pasting the whole source code.
Here's the source code of my Entity class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ConsoleApplication1
{
public class ContactData
{
[Key]
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<PhoneNumber> Phones { get; set; }
public IList<EmailAddress> Emails { get; set; }
}
public class PhoneNumber
{
[Key]
public int PhoneId { get; set; }
public int ContactId { get; set; }
public string Phone { get; set; }
public ContactData ContactData { get; set; }
public PhoneNumber()
{
}
}
public class EmailAddress
{
[Key]
public int EmailId { get; set; }
public int ContactId { get; set; }
public string Email { get; set; }
public ContactData ContactData { get; set; }
public EmailAddress()
{
}
}
}
ContactContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace ConsoleApplication1
{
public class ContactContext : DbContext
{
public ContactContext()
: base("ContactDBContext")
{
Database.SetInitializer<ContactContext>(new DropCreateDatabaseIfModelChanges<ContactContext>());
}
public DbSet<ContactData> Contacts { get; set; }
}
}
ContactManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class ContactManager
{
ContactContext cDbContext = new ContactContext();
public IList<ContactData> GetContactList()
{
IQueryable<ContactData> cContactList = cDbContext.Contacts;
IList<ContactData> cListData = new List<ContactData>();
cListData = cContactList.ToList();
return cListData;
}
}
}
Program.cs (Entry point)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
ContactManager cMgr = new ContactManager();
foreach (ContactData cd in cMgr.GetContactList())
{
Console.WriteLine(cd.ContactId);
Console.WriteLine(cd.FirstName);
Console.WriteLine(cd.LastName);
foreach(PhoneNumber p in cd.Phones)
{
Console.WriteLine(p.Phone);
}
foreach (EmailAddress e in cd.Emails)
{
Console.WriteLine(e.Email);
}
}
Console.ReadKey();
}
I finally managed to find a solution to my own problem. But I want to extend my 'Thanks' to both #SteveGreene and #RickStahl for their valuable inputs.
#RickStahl gave a good input or recommendation of not making use of parameterized constructors in my Entity classes. Per Rick, the parameterized constructors do not work with EF.
#SteveGreene - I finally realized that you were pointing me into right direction. I was not able to get it at that time due to my lack of understanding of EF. However, after reading about the EF in detail and in particular about Eager loading and Lazy loading helped me finally.
The error related to 'Object reference not set to an instance of an object' was easy to resolve after I figured out that I had to instantiate the collection properties for 'Phones' and 'Emails' in the ContactData class constructor.
Please refer to the code change below.
public class ContactData
{
[Key]
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string BusinessName { get; set; }
public IList<PhoneNumber> Phones { get; set; }
public IList<EmailAddress> Emails { get; set; }
public ContactData()
{
//By instantiating Phones and Emails member collections below, resolved 'Object reference not set to an instance of an object' exception
Phones = new List<PhoneNumber>();
Emails = new List<EmailAddress>();
}
}
The next step was to populate data into related Entities as I was only able to populate the parent Entity Contact up to this point.
For that I had to discover from my research and understand about different approaches such as Eager loading, Lazy loading etc. towards loading data into related entities.
That is done by using the following statements in the ContactManager.GetContactList() method. I have used eager loading method as far as I understand. For lazy loading, you have to use virtual members for child classes such as Phone and Email in this case.
var phones = cDbContext.Contacts.Include("Phones").ToList();
var emails = cDbContext.Contacts.Include("Emails").ToList();
Here's a complete code of ContactManager class.
class ContactManager
{
ContactContext cDbContext = new ContactContext();
public IList<ContactData> GetContactList()
{
ContactData cd = new ContactData();
IQueryable<ContactData> cContactList = cDbContext.Contacts;
IList<ContactData> cListData = new List<ContactData>();
var phones = cDbContext.Contacts.Include("Phones").ToList();
var emails = cDbContext.Contacts.Include("Emails").ToList();
cListData = cContactList.ToList();
return cListData;
}
}
After learning about this I understood that #SteveGreene was pointing me into correct direction. Only issue that I ran into after all this was that I used a lambda expression with Include method. But I got a compile time exception 'Include method expects a string'. I tried using the following lambda expression with the include method.
var phones = cDbContext.Contacts.Include(b => b.Phones).ToList(); // this was causing a compile time error 'Include method expects a string'.
After passing (as below) the name of the parameter as string with the name of 'Phones' and 'Emails' collection members of ContactData class, it worked fine.
var emails = cDbContext.Contacts.Include("Emails").ToList();
If any queries then please post a comment.

show image on GridView win 8 metro style?

I follow step by step this article(http://mikaelkoskinen.net/winrt-step-by-step-tutorial-mvvm-gridview-semanticzoom/) to add Images to GridView of my project, but when I finish adding GridView to my project following this article. I had a problem with showing image is that the image( in the project folder) don't show when the app is running but the app shows images from internet(the code from article has run well and loaded the image from project folder), I don't know how to show this problem,
this is some code:
public class Movie
{
public string Title { get; set; }
public string Subtitle { get; set; }
public string Image { get; set; }
public string Category { get; set; }
}
public class MovieCategory
{
public string Title { get; set; }
public List<Movie> Items { get; set; }
}
My code to load image from project folder:
MovieList.Add(new Movie { Title = "The Ewok Adventure", Category = "Adventure", Subtitle = "The Towani family civilian shuttlecraft crashes on the forest moon of Endor.", Image = "duy.jpg" });
(I've create MovieList before, and image "duy.jpg" is in the folder of project)
I don't why I try above code with source from article,i've checked image source string carefully by debug tool, it worked well(image from project folder was showed in source of article) but when i added to my project,the image from internet link worked well but the image from project folder didn't load.
Please help me.Thank in advance.
Change class definition like this
public class Movie
{
public string Title { get; set; }
public string Subtitle { get; set; }
public string Image { get; set; }
public string Category { get; set; }
public ImageSource ImgSource
{
get
{
return new BitmapImage(new Uri("ms-appx:///" + this.Image));
}
}
}
And bind Image tag with ImgSource property not with Image property.
<Image Source="{Binding ImgSource}" ........./>
is the image in project root ? did you set to Content and Always copy to output folder ?

BreezeJS Get Metadata from the server without using Entity Framework for complex objects (nested objects)

This is my model on the server side. I don't want to use Entity Framework, how would I generate BreezeJS metadata from the server. Breeze Metadata Format found here http://www.breezejs.com/documentation/breeze-metadata-format doesn't work.
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public List<App> Apps { get; set; }
}
public class App
{
public string Id { get; set; }
public string Name { get; set; }
public Dictionary<string, string> Info { get; set; }
}
Did anyone try complex objects (nested object) similar to above one without using EF.
Using Breeze Metdata API or OData ?
Look at this example http://www.breezejs.com/samples/nodb it should give you a clue.

Json parsing in windows phone (C#)

I am new to Windows Phone development.I want to parse some data using json parsing.After
googling i find a number of example but I am not able to understand properly.I have follow
a link
http://dotnetbyexample.blogspot.in/2012/01/json-deserialization-with-jsonnet.html
to do json parsing But i am not able to show Storage ,Memory, ScreenSize in my List and my code is
private void Load_Click(object sender, RoutedEventArgs e)
{
var w = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<RootObject>>(r.EventArgs.Result);
PhoneList.ItemsSource = deserialized;
});
w.DownloadStringAsync(
new Uri("http://www.schaikweb.net/dotnetbyexample/JSONPhones1.txt"));
}
}
public class Specs
{
public string Storage { get; set; }
public string Memory { get; set; }
public string Screensize { get; set; }
}
public class RootObject
{
public string Brand { get; set; }
public string Type { get; set; }
public Specs Specs { get; set; }
}
}
Please Help me to solve this issue or any other sample for the same is also appreciated
Thanks
The code seems ok, so if you aren't running in to any errors there might be an issue with your UI code or UI to data coupling.
You need to post the UI code to get some help with this I suspect.