Skip to main content

Microsoft Silverlight

Unanswered Question Javascript unable to get custom object from ASMX through Silverlight using DLL I createdRSS Feed

(0)

lanfong
lanfong

Member

Member

14 points

7 Posts

Javascript unable to get custom object from ASMX through Silverlight using DLL I created

 Hi all,

I know that if we want to return custom class object from Silverlight to Javascript. All we need to do is to make the class we
created [ScriptableType] ([ScriptableTypeAttribute]) to expose all the public methods and members or [ScriptableMembers]
to just expose a specific method, member.

I have tried and it works when the custom class is created in the same project or even in external DLL files I created.
 

The below is the School.cs class I created within the silverlight application
 

1    using System.Windows.Browser;
2 namespace SilverlightASPX
3 {
4 [ScriptableType]
5 public class Student
6 {
7 [ScriptableMember]
8 public string Name { get; set; }
9 [ScriptableMember]
10 public int Age { get; set; }
11 }
12 }
13
 



This has no problem to return to Javascript. Javascript recognized the object and I am able to use obj.Name to get the name of school.

I also tried creating a Silverlight Library Application that has the same implementation as the School.cs I created in the Silverlight project
 
1    using System.Windows.Browser;
2 namespace LanfLibrary
3 {
4 [ScriptableType]
5 public class Student
6 {
7 [ScriptableMember]
8 public string Name { get; set; }
9 [ScriptableMember]
10 public int Age { get; set; }
11 }
12 }
13

 
It works no problems for javascript to read the age and name. I copied the System.Windows.Browser.dll to the project so that I could make

[ScriptableMember].


OK enough for my testing. Here comes the problem
==========================================================



I created a simple ASMX web service and also add the DLL reference I created above "LanfLibrary", which includes Student class
One of my WebMethod returns a dummy Student data.

Here's the flow of my testing project.

  1. Javascript calls GetStudent() [a function from silverlight]
  2. Silverlight calls webService.GetStudentAsync() and registers GetStudentCompleted()
  3. webService returns back to Silverlight
  4. Silverlight calls HtmlPage.Windows.Invoke("functionName", StudentObjectFromWebService);

The testing result was failed. I use Firefox + Firebug to test it and I couldn't access studentObject.Name and studentObject.Age.
It's the same error I got if the class is not [ScriptableType] / [ScriptableMember]
Error setting property on scriptable plugin object! [plugin exception: Object doesn't support this property or method].

Is there anyway to accomplish this?? maybe just some more [] tags from the DLL?
That's say I can't change my Class Library, is there anyway to do it so that I can still access from javascript??

If changes is needed, what should I do.


Thanks,

Lanf

ken tucker
ken tucker

All-Star

All-Star

16276 points

2,479 Posts

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

 Did you register the asmx with the scriptmanager on the web form?

lanfong
lanfong

Member

Member

14 points

7 Posts

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

  No, I don't. Should I do that??? My web page is really simple that it only contains the Silverlight control creation tags and buttons and that's it. It's the silverlight control calls web service and returns data back to javascript / aspx page. However, It does have ScriptManager tags in aspx page.

I googled about ScriptManager + ASMX and most of them talks about Ajax-enabled Web Services with ScriptManager.

From my testing, my web service returns successfully. It's just Javascript doesn't understand the object.



The below is my aspx page

 

1    <body style="height:100%;margin:0;">
2 <div id="service" style="behavior: url(webservice.htc)">
3 </div>
4 <div id="load">
5 <img src="img/load.gif" />
6 </div>
7 <form id="form1" runat="server">
8 <asp:ScriptManager ID="ScriptManager1" runat="server">
9 </asp:ScriptManager>
10 <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightASPX.xap"
11 MinimumVersion="2.0.31005.0" Width="0" Height="0" OnPluginLoaded="OnPluginLoaded"
12 InitParameters="url=http://lanfong.no-ip.com:8989/LanfService/Service.asmx" />
13 </form>
14 <div id="content" style="display: none">
15 <button onclick="HelloWorldFromPageXaml()">
16 HelloWorld from Page.xml.cs</button>
17 <button onclick="GetHelloWorld()">
18 HelloWorld from web service</button><br />
19
20 <button onclick="GetStudentFromWS()">
21 GetStudentFromWS()</button>
22 <button onclick="GetFromPage_CS()">
23 GetFromPage_CS()</button>
24 <button onclick="GetFromLanfSvc_Student()">
25 GetFromLanfSvc_Student()</button>
26
27 </div>
28
29
30 </body>
 

lanfong
lanfong

Member

Member

14 points

7 Posts

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

 Is anyone encountered such problem??? If so, please spend some time helping me here. If there's documentation that explains all, give me the link and I'll go through it myself...

 

 

thanks..

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

Hi Lanfong,

Please make sure you have add

WebApplication.Current.RegisterScriptableObject("basic", this); on your Load event.   Based on your description, I suspect that the dynamically downed dll didn't generate the js code for you.  However, as a convenient, would you please share a tiny repro with us?  Thanks for your understanding.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

lanfong
lanfong

Member

Member

14 points

7 Posts

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

Jonathan Shen – MSFT:

Hi Lanfong,

Please make sure you have add

WebApplication.Current.RegisterScriptableObject("basic", this); on your Load event.   Based on your description, I suspect that the dynamically downed dll didn't generate the js code for you.  However, as a convenient, would you please share a tiny repro with us?  Thanks for your understanding.

Best regards,

Jonathan

 

Thanks for the reply, Johnathan.
I do have       HtmlPage.RegisterScriptableObject("Svc", this); in my silverlight application.But not in my DLL.
Or do you mean that I have to call HtmlPage.RegisterScriptableObject("myObject", DLL_FROM_WEBSERVICE ); in my silverlight application??

And sorry for my bad English I dont undestand what you mean by share a tiny repro. Do you mean report? or you want me show all the code.
I could past all the code, but I just thought too much code makes people hard to see.

 

Thanks,

Lanfong

 

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

Hi Lanfong,

lanfong:
do you mean that I have to call HtmlPage.RegisterScriptableObject("myObject", DLL_FROM_WEBSERVICE ); in my silverlight application??

I mean that you need to register the script on your silverlight application.   

lanfong:
And sorry for my bad English I dont undestand what you mean by share a tiny repro.

A tiny repro means a runnable sample code with the necessary code only.   Would you please upload it to some where and share the link here?  

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

lanfong
lanfong

Member

Member

14 points

7 Posts

Re: Javascript unable to get custom object from ASMX through Silverlight using DLL I created

 Thanks for your reply, Jonathan. The below are the repros

Silverlight Project Name: SilverlightASPX
C# Class Library Name: LanfLibrary
WebService: LanfService


my dll
 

1    using System.Windows.Browser;
2    namespace LanfLibrary
3    {
4      [ScriptableTypeAttribute]
5      public class Student
6      {
7        [ScriptableMemberAttribute]
8        public string Name { get; set; }
9    
10       [ScriptableMemberAttribute]
11       public int Age { get; set; }
12     }
13   }
14   
 

Both my silverlight project and web service project "Add Reference" LanfLibrary



my LanfService

 
1    using LanfLibrary;
2    
3      [WebMethod]
4      public LanfLibrary.Student GetStudent()
5      {
6        LanfLibrary.Student t = new LanfLibrary.Student();
7        t.Age = 10;
8        t.Name = "GetStudentFromWS";
9        return t;
10     }
 


my silverlight project

 
1        public string URL;
2 private LanfSvc.ServiceSoapClient client;
3 4 public Page()
5 {
6 InitializeComponent();
7 this.Loaded += new RoutedEventHandler(Page_Loaded);
8 HtmlPage.RegisterScriptableObject("Svc", this);
9 }
10 11 void Page_Loaded(object sender, RoutedEventArgs e)
12 {
13 BasicHttpBinding bind = new BasicHttpBinding();
14 EndpointAddress endpoint = new EndpointAddress(URL);
15 client = new SilverlightASPX.LanfSvc.ServiceSoapClient(bind, endpoint);
16 client.GetStudentCompleted += new EventHandler(client_GetStudentCompleted);
17 }
18
19
20 // This is called from aspx/html page to call the web service.
21 [ScriptableMember]
22 public void GetStudentFromWebService(string onSuccess)
23 {
24 client.GetStudentAsync(onSuccess);
25 }
26
27 // The callback method from webservice
28 void client_GetStudentCompleted(object sender, SilverlightASPX.LanfSvc.GetStudentCompletedEventArgs e)
29 {
30 string onSuccess = (string)e.UserState;
31
32 if (e.Error == null)
33 {
34 HtmlPage.Window.Invoke(onSuccess, e.Result);
35 }
36 else
37 {
38 HtmlPage.Window.Navigate(new Uri("http://localhost:8989/index.asp?abc=" + e.Error), "_blank");
39 //MessageBox.Show(e.Error.ToString());

40 }
41 }
42
43
44
45 //This was not working.
46 //Even though LanfService uses same LanfLibrary, javascript doesn't recognize it.

47 [ScriptableMember]
48 public LanfSvc.Student GetStudentFromWebService_Studen_DLL()
49 {
50 SilverlightASPX.LanfSvc.Student s = new SilverlightASPX.LanfSvc.Student();
51
52 s.Name = "
GetFromLanfSvc.Student";
53 s.Age = 12234;
54 return s;
55 }
56
57
58
59 //This works fine. Javascript is able to retrieve age and name.
60 [ScriptableMember]
61 public Student GestStudentFromSilverlight_DLL()
62 {
63 SilverlightASPX.Student s = new SilverlightASPX.Student();
64 s.Name ="
GestStudentFromSilverlight_DLL";
65 s.Age=123;
66 return s;
67 }




Line 48 and Line 28 is the place that's not working!
Getting student object directly from Silverlight is working, but getting student object returned by silverlight using web service call is NOT working.


thanks again,

Lanfong

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities