RIA Service et Serialisation
Bonjour,
J'ai commencé à développer une petite application Silverlight en utilisant la technologie RIA Services.
Tout fonctionne bien. Je me suis inspiré des tuto de David Rousset et de Brad Addams.
Pour aller un peu plus loin, j'ai voulu ajouter un bouton permettant de sérialiser mes objets (des Clients) dans un fichier XML. Le but est bien sur de proposer un bouton pour désérialiser par la suite.
Pour une raison inconnue, ça ne fonctionne pas. J'ai une erreur de type :
Citation:
There was an error reflecting type 'System.Collections.Generic.List' 1 [SerialisationRIA.Web.Client]'. There was an error reflecting type 'SerialisationRIA.Web.Client'.
Ma fonction qui permet de sérialiser fonctionnait parfaitement sur une autre application que j'avais développé (sans RIA Services).
Quelqu'un a t'il déjà eu ce genre de problème ?
Ma classe Client (très simple avec simplement 3 champs):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace SerialisationRIA.Web
{
public class Client
{
[Key]
public string ClientID { get; set; }
[Required(
ErrorMessage = "Le Nom du Client est obligatoire")]
public string Nom { get; set; }
[RegularExpression("^(?:m|M|f|F)$",
ErrorMessage = "Valeur invalide : inidquez m,M ou f,F")]
public string Sexe { get; set; }
}
} |
Ma Classe ClientList :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SerialisationRIA.Web
{
public class ClientList
{
List<Client> listClient = new List<Client>()
{
new Client() {ClientID="0001", Nom="George", Sexe="M"},
new Client() {ClientID="0002", Nom="Alain", Sexe="M"},
new Client() {ClientID="0003", Nom="Robert", Sexe="M"},
new Client() {ClientID="0004", Nom="Mauricette", Sexe="F"},
};
public IEnumerable<Client> GetClients()
{
return listClient.ToArray();
}
public void Add(Client emp)
{
listClient.Add(emp);
}
public void Update(Client current, Client orginal)
{
if (listClient.Contains(orginal)) listClient.Remove(orginal);
Add(current);
}
}
} |
Ma Classe Domaine Service (ClientDomainService) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| namespace SerialisationRIA.Web
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Ria;
using System.Web.Ria.Data;
using System.Web.DomainServices;
using System.Data;
[EnableClientAccess()]
public class ClientDomainService : DomainService
{
ClientList Context = new ClientList();
public IQueryable<Client> GetAllClients()
{
return Context.GetClients().AsQueryable();
}
public IQueryable<Client> GetUnClient(string clientID)
{
return Context.GetClients().ToList()
.Where(cli => cli.ClientID == clientID).AsQueryable();
}
public void InsertClient(Client unClient)
{
Context.Add(unClient);
}
public void UpdateClient(Client monClient)
{
Context.Update(monClient, this.ChangeSet.GetOriginal(monClient));
}
public override void Submit(ChangeSet changeSet)
{
base.Submit(changeSet);
//todo: Submit changes to the store.. (for example, save to a file, etc
}
}
} |
Dans la code XAML de Home, j'ai un Domain Data Source :
Code:
1 2 3 4 5 6 7 8
| <riaControls:DomainDataSource x:Name="source"
AutoLoad="True"
QueryName="GetAllClientsQuery"
LoadSize="20">
<riaControls:DomainDataSource.DomainContext>
<domain:ClientDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource> |
Les données apparaissent bien dans la grille, tout est ok de ce côté.
Dans le Code Behind de Home, je fais donc appel à une méthode qui sérialise :
Appel de la méthode pour sérialiser quand on clique :
Code:
1 2 3 4 5 6
| private void BtnSerialiser_Click(object sender, RoutedEventArgs e)
{
var context = source.DomainContext as ClientDomainContext;
MessageBox.Show(context.Clients[0].Nom); // Test pour voir si les données apparaissent
Util.SerialiserLst<Client>(context.Clients.ToList());
} |
Méthode pour Sérialiser :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public static void SerialiserLst<T>(List<T> monObjet)
{
try
{
XmlSerializer serializer = new XmlSerializer(monObjet.GetType());
System.IO.StringWriter sw = new System.IO.StringWriter();
serializer.Serialize(sw, monObjet);
string monXml = sw.ToString();
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = "xml";
sf.Filter = "Xml files (*.xml)|*.xml";
if (sf.ShowDialog().Value)
{
Stream stream = sf.OpenFile();
StreamWriter textWriter = new StreamWriter(stream);
textWriter.Write(monXml);
textWriter.Close();
stream.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + " " + ex.InnerException.Message);
}
} |
Pour télécharger l'application complète (format RAR - 2.5Mo)
http://www.filzup.com/download.php?id=E7B4F6A31