Skip to main content
Home Forums Silverlight Programming Programming with JavaScript Javascript unable to get custom object from ASMX through Silverlight using DLL I created
7 replies. Latest Post by lanfong on June 8, 2009.
(0)
lanfong
Member
14 points
7 Posts
06-01-2009 12:21 AM |
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 wecreated [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 SilverlightASPX3 {4 [ScriptableType]5 public class Student6 {7 [ScriptableMember]8 public string Name { get; set; }9 [ScriptableMember]10 public int Age { get; set; }11 }12 }13
1 using System.Windows.Browser;2 namespace LanfLibrary3 {4 [ScriptableType]5 public class Student6 {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 classOne of my WebMethod returns a dummy Student data.Here's the flow of my testing project.
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
All-Star
16276 points
2,479 Posts
06-01-2009 8:56 AM |
Did you register the asmx with the scriptmanager on the web form?
06-01-2009 10:51 AM |
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>
06-04-2009 1:45 AM |
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...
24939 points
2,425 Posts
06-05-2009 7:41 AM |
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
06-05-2009 11:27 AM |
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
06-07-2009 11:06 PM |
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?
06-08-2009 6:33 PM |
Thanks for your reply, Jonathan. The below are the reprosSilverlight Project Name: SilverlightASPXC# Class Library Name: LanfLibraryWebService: 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
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 }
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 webservice28 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 else37 {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