Kerala Microsoft Users Group

Windows 7 Deployability

How to call a class library from silverlight

Latest post 02-04-2010 11:00 PM by Jinesh. 14 replies.
  • 01-20-2010 7:40 AM

    How to call a class library from silverlight

    Hai guys,

     I refenced my class library to my silverlight project, but i couldn't call it from my silverlight code page to create its object.

    Could you tell me how to consume a class library in silverlight.

     

    • Post Points: 45
  • 01-20-2010 7:57 AM In reply to

    Re: How to call a class library from silverlight

    Hello Jinesh,

    Have you created the Class library in Silverlight?  As you know Silverlight applications are executed at client side, and Microsoft is providing the silverlight runtime (which is subset of the CLR) for the browsers. If your class library is not meant for silverlight runtime, you cant use it.

    Regards

    Sreejumon [MVP]

    http://sreesharp.com/blog

    • Post Points: 5
  • 01-20-2010 8:24 AM In reply to

    Re: How to call a class library from silverlight

    You can't refer a normal .NET class (CLR) library from Silverlight. 

    Silverlight apps are built using a different (lightweight) version of the framework (SLR or Silverlight Run Time ;) ). If you'd want to reference normal libraries the compiler has to switch to different versions of the framework... and is not supported.

    If you are trying to access functionality from an existing library - The only solution here is to create a webservice which can handle the old library.

    If you are creating your own class library - Add a Silverlight Class Library to your solution, and write your code inside that instead of adding a C# Class library.

    Note: If your .NET CLR library don't have extra references, you can patch the old library using Reflexil or Mono Cecil to target it to Silverlight - But that is a different story

    Anoop Madhusudanan

    Yet another Spiritual Programmer: See my blog for Design Patterns, Neural Nets, .NET recipes and more - http://amazedsaint.blogspot.com 

    • Post Points: 25
  • 01-21-2010 12:18 AM In reply to

    Re: How to call a class library from silverlight

    Hai,

      I understood both of you said. Let me know how can i reference externel dll's like  system.Data, system.mysql.Data

    to my silverlight classlibrary.

     

    • Post Points: 25
  • 01-21-2010 1:04 AM In reply to

    Re: How to call a class library from silverlight

    You can't do it directly :)

    Create a WCF web service and access your data from there. Tie your Silverlight app to your WCF web service

     

    Anoop Madhusudanan

    Yet another Spiritual Programmer: See my blog for Design Patterns, Neural Nets, .NET recipes and more - http://amazedsaint.blogspot.com 

    • Post Points: 25
  • 01-22-2010 7:08 AM In reply to

    Re: How to call a class library from silverlight

    I believe anoop and sreejumon are both rite, you cannot access the dll's web services would be the best solution for the current requirement. http://msdn.microsoft.com/en-us/library/cc296254(VS.95).aspx Is the msdn on accessing externall dll's from silverlight.

    Regards

    Jairam Ramesh

    • Post Points: 25
  • 01-27-2010 11:04 PM In reply to

    Re: How to call a class library from silverlight

    Hai,

       It is too difficult to return the values in a datatable from the webservice to silverlight, we have to split and pass one by one to silverlight application,

    is there any solution for this ?

    and also know how to consume a 'session'  in silverlight?

    • Post Points: 25
  • 01-28-2010 2:41 AM In reply to

    Re: How to call a class library from silverlight

    • Post Points: 25
  • 02-01-2010 2:35 AM In reply to

    Re: How to call a class library from silverlight

    Thanks all of you for yours valuable response

    I created a webservice for handling database and worked successfully.

    One problem is that , I declared event handler and call the event handler function under the a button click. I get

    the answer after the second click.

    Let me know how is it work after the second click. Is it any logical error or any thing more things than i have not implented.

    • Post Points: 25
  • 02-01-2010 2:45 AM In reply to

    Re: How to call a class library from silverlight

    Can you share the code?

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 25
  • 02-01-2010 4:56 AM In reply to

    Re: How to call a class library from silverlight

    WebServiceSoapClient services = new ServiceReference1.WebServiceSoapClient();

     

     

    string

     

    username="", password="";

    private void b1_Click(object sender, RoutedEventArgs e)  { 

    services.getloginvalueCompleted += new System.EventHandler<getloginvalueCompletedEventArgs>(loginnamecompleted);

    services.getloginvalueAsync(txt1.Text, "username");

    services.getloginvalueCompleted += new System.EventHandler<getloginvalueCompletedEventArgs>(loginpasswordcompleted);

    services.getloginvalueAsync(pwd.Password, "password");

    if (txt1.Text != "" && pwd.Password != "")

    {

    if (txt1.Text == username && pwd.Password == password)

    {

    services.getuseridCompleted += new System.EventHandler<getuseridCompletedEventArgs>(getuseid_service);

    services.getuseridAsync(txt1.Text);

    services.getuseridCompleted += new System.EventHandler<getuseridCompletedEventArgs>(getuseid_service);

    services.getuseridAsync(txt1.Text);

    App appeg = (App)Application.Current;

    appeg.clientid = userid; // to store userid- like session

    Page1 p = new Page1();

    this.Content = p;

    }

    else

    {

    MessageBox.Show("Invalid User Credentials");

    clear();

    }

    }

    else { MessageBox.Show("Username and Password shouldnot be blank"); }

    }

    public void loginnamecompleted(object sender, getloginvalueCompletedEventArgs e){

    username = e.Result.ToString();

    }

    public void loginpasswordcompleted(object sender, getloginvalueCompletedEventArgs e){

    password = e.Result.ToString();

    }

    // note that above two handlers works four times

    public void clear(){

    txt1.Text = ""; pwd.Password = "";

    }

    • Post Points: 25
  • 02-01-2010 6:53 AM In reply to

    Re: How to call a class library from silverlight

    Jinesh,

    The web service calls in Silverlight are asynchronous, so definitely there will be a time delay to execute the call and to get the response. I think the Behavior you mentioned is because of this delay in the request-response.

    It is important to understand that execution will continue to the next line after making any Async method calls to the service - it won't wait at the location of the service method call, to get it finished (as in a seq scenario).

    For example - I assume in the logic you have there, you are assigning the appid to the client id like appeg.clientid = userid; expecting the getUserIdAsync() call to be synchronous, but this is not the case. To execute any logic that relies on the service call, you should do it inside the call back (eg. getuserid_complete)

     

    Hope this helps

     

    Anoop Madhusudanan

    Yet another Spiritual Programmer: See my blog for Design Patterns, Neural Nets, .NET recipes and more - http://amazedsaint.blogspot.com 

    • Post Points: 25
  • 02-02-2010 12:43 AM In reply to

    Re: How to call a class library from silverlight

    Hai,

        Let me know how can i use this code efficiently, means call three calls to the webservice in a single click event. And also know

    how the event execute 4 times!!

    • Post Points: 25
  • 02-02-2010 2:29 AM In reply to

    Re: How to call a class library from silverlight

    Reply |Contact |Answer

    If I understood you correctly, you want the three calls to execute synchronously (one after another), in your click event handler write?

    We can’t block the UI thread (i.e, directly inside your click handler) or the call will never be dispatched. But we can call it in a synchronous manner from a non-UI thread using a ChannelFactory. This is a bit indirect though. Assume you have two methods Method1 and Method2, in your service, to call them synchronously 

     

    ThreadPool.QueueUserWorkItem(delegate
    {
    	var channelFactory = new ChannelFactory<IYourService>("*");
    	var simpleService = channelFactory.CreateChannel();
    
    
            //First call
    	var asyncResult1 = simpleService.BeginMethod1("yourparam));
    	try
    	{
    		var result1 = simpleService.EndMethod1(asyncResult1);
    	}
    	catch (Exception ex)
    	{
    
    	}
    
    
           //Second call
    
    
    	var asyncResult2 = simpleService.BeginMethod2("yourparam));
    	try
    	{
    		var result2 = simpleService.EndMethod2(asyncResult2);
    	}
    	catch (Exception ex)
    	{
    
    	}
    
    });

     

     

    You can refer this article for details http://www.codeproject.com/KB/silverlight/SynchronousSilverlight.aspx

    Anoop Madhusudanan

    Yet another Spiritual Programmer: See my blog for Design Patterns, Neural Nets, .NET recipes and more - http://amazedsaint.blogspot.com 

    • Post Points: 25
  • 02-04-2010 11:00 PM In reply to

    Re: How to call a class library from silverlight

    Reply |Contact |Answer

    Thanks for your reply. I serialised it with the help of my senior programmer. Like this..

     

    WebServiceSoapClient services = new ServiceReference1.WebServiceSoapClient();

    private void b1_Click(object sender, RoutedEventArgs e)  {

     if (txt1.Text != "" && pwd.Password != "")
          {
          services.checkLoginCompleted += new System.EventHandler<checkLoginCompletedEventArgs>(logincheckcompleted);
          services.checkLoginAsync(txt1.Text, pwd.Password);
        }

     else { MessageBox.Show("Username and Password shouldnot be blank"); }

    public void logincheckcompleted(object sender, checkLoginCompletedEventArgs e)
     {
         if (e.Result != 0)
         {      
             App appeg = (App)Application.Current;
             appeg.clientid = Convert.ToInt32(e.Result);      
             Page1 p = new Page1();        
             this.Content = p;
         }
         else
         {
            MessageBox.Show("Invalid User Credentials");
         }
     }

     

    That is the code which we have to run synchronouisly wrote in tha event handler function. so it reduced my code also.

     

     

    • Post Points: 5
Page 1 of 1 (15 items) | RSS