Thursday, November 19, 2009

WCF used in Silverlight,

WCF client in Silverlight, has for me two main differences comparing to client implemented in ASP.NET:

- Silverlight works asynchronous with services
- can't use .NET assemblies, Silverlight can use only assemblies compiled as Silverlight assemblies or use libraries from the Silverlight, not from .NET



How to write and consume WCF services in Silverlight is very good described in this article:
http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2#WCFSilverlightServiceAccess

more important parts of mentioned article, that focus on implementation for Silverlight:

- Asynch Pattern, asynchronous calls to the WCF:



   1:  public void LoadBerechnung(RequestDO requestDO)
   2:  {
   3:      AsyncCallback asyncCallback = delegate(IAsyncResult result)
   4:      {
   5:          var item = ((IAsyncBerechnung)result.AsyncState).EndBerechne(result);
   6:          Control.Dispatcher.BeginInvoke(() => SetStoffe(item));
   7:      };
   8:   
   9:      Berechnung.BeginBerechne(stoffRequestDO, asyncCallback, stoffBerechnung);
  10:  }
  11:   
  12:  private void SetStoffe(StoffResultDO stoffResultDO)
  13:  {
  14:      if (stoffResultDO!=null)
  15:      {
  16:          Produkt.prop1.DataSource = stoffResultDO.Text;
  17:          Produkt.prop2.Description = stoffResultDO.ErrorText;
  18:      }
  19:  }

This part i find very important:"There's absolutely no technical reason why Silverlight can't use .NET assemblies." in this articel is detailed description to this topic:
http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2#WCFSilverlightServiceAccess WWith "File-Level Technique" described here I can compile files from .NET assembly as Silverlight assembly.

What can I do, when I have something .NET specific in my data contract file?
I use "conditional compilation symbols" from visual studio project:



#if NETCLR [System.Serializable]#endif
This way its possible, to use logic implemeted for .NET in Silverlight application / very easy :)



No comments:

Post a Comment