none
How to call a web service from Silverlight 2.0 project

    Question

  • I'm trying to  call web service from Silverlight project.

    public partial class Page : UserControl

    {

    public Page()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, RoutedEventArgs e)

    {

    Service1SoapClient c = new Service1SoapClient();c.HelloWorldCompleted += new EventHandler<HelloWorldCompletedEventArgs>(c_HelloWorldCompleted);

    c.HelloWorldAsync();

    }

    void c_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)

    {

    testBox1.Text = e.Result;

    }

    }

     I've got following exception:

    Microsoft JScript runtime error: Sys.InvalidOperationException: ManagedRuntimeError error #4002 in control 'Xaml1': System.Exception: [AsyncCallbackException]

    Can anyone help me?

    Thanks

    Friday, March 07, 2008 5:10 AM

All replies

  • I had problems until I explicitly set the url of the service in the client code. Mine was a WCF service in the same project, so I just did:

       BasicHttpBinding b = new BasicHttpBinding();
       ServiceReference1.MyServiceClient cl = new SilverlightApplication1.ServiceReference1.MyServiceClient(b, new EndpointAddress("http://localhost:1891/SilverlightApplication1_Web/MyService.svc"));

    ..and it worked fine.

    Friday, March 07, 2008 6:35 AM
  • Just wanted to ask both of you: How are you adding webservices (that are not in the same project) at all??
    Whenever I try to add some service reference and follow the wizard, it generates client code that cannot be compiled. Tongue Tied

    Do you have any quick hints for me? Maybe it's also some conflict among my system assemblies; especially System.Xml is available in different versions, and it looks like it generates a webservice for common .net and not for that silverlight project I am currently using... 

    Friday, March 07, 2008 7:47 AM
  • In addition: When I try to build some example, let's say I want to add a service reference to http://soap.amazon.com/schemas2/AmazonWebServices.wsdl, it generates the code and the first of 96 errors is: The type or namespace name 'SoapTypeAttribute' does not exist in the namespace 'System.Xml.Serialization' (are you missing an assembly reference?) The following errors are also unknown types.

    The assembly is referenced, at least it is listed under references. :(
    What to do? Does it generate something wrong, or is (more likely) me the error-source?

    Any help would be helpful!
     

    Friday, March 07, 2008 8:01 AM
  • I've managed to do this to a Web service of mine using the "Add service reference" functionality. I believe the Web service needs to support SOAP 1.1 Basic Profile.

    Friday, March 07, 2008 8:10 AM
  • Thanks for your reply, Craig. But is the Amazon Webservice really that special that it does not conform to SOAP 1.1 BP?

    Maybe you have some public available webservice (wsdl) you can add into your silverlight project successfully...? So I could try if it would build with that. 

    Friday, March 07, 2008 9:03 AM
  • Well I tried it out my side and got the same result as you. It seems the Web service proxy code generator for Silverlight projects doesn't match the Silverlight framework. I've tried adding every logical assembly reference but still no luck. :(

    Looking at the WSDL file I notice that the soap:address refers to "soap2" which makes me think it might not be SOAP 1.1 BP. Anyone who knows more about SOAP/WSDL able to comment?

    Friday, March 07, 2008 9:33 AM
  • Hm, must realy depend on the kind of webservice (profile, version, types, whatever...)

    Using an alternative amazon webservice for my testing purposes went well. Compiles and works fine. The used wsdl: http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl?

    So I would be really interested in the extaxct requirements for webservices in Silverlight. Is it only the previously mentioned SOAP 1.1 basic profile, or are there any further hurdles? Is there any official statement?

    But whatever the concrete restrictions/limiations are, right now I cannot promote Silverlight for projects at my company, because associated wsdls we rely on are also generating incompatible source code. :(

    Friday, March 07, 2008 4:35 PM
  • Well, it looks like this thread confirms all my findings so far.  Hopefully the silverlight team will have this fixed up soon...

    Monday, March 10, 2008 11:31 PM
  • I haven't tried with Amazon webservice. but I have tested with normal asp.net web service. It's working fine. Please take a look this post.
    Silverlight 2 (beta1) - Database Operations with ASP.NET Web Service in Silverlight 2 

    Monday, March 10, 2008 11:37 PM
  • I'm glad somebody's making progress!  Yes, the asmx service you have set up does work, but my specific problem involves consuming a php web service that is configured with WSDL 1.1.  It seems like we still have the problem of consuming WSDL 1.1 type services. 

    Monday, March 10, 2008 11:59 PM
  • Hi!

    I'm getting the same problem as you. I have generated the WSDL with gSOAP. I have found this on gSOAP docs: "The toolkit follows the WS-I Basic Profile 1.0a compliance recommendations."

    Anybody knows how make it run? :S

    Thanks for your help!

     
    Jordi
     

     

    Thursday, April 24, 2008 4:40 AM
  • Hi!

    I don't get the errors, but I get two warnings. I get them with any webservice, and that didn't happen with WPF projects.

    Warning 1
    =================== 

    Custom tool warning: Removing unsupported type System.Windows.Controls.OpenFileDialog
    System.TypeLoadException: Could not load type 'System.Security.SecuritySafeCriticalAttribute' from assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
       at System.ModuleHandle.ResolveType(Int32 typeToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)
       at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
       at System.Reflection.Module.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
       at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, RuntimeMethodHandle& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
       at System.Reflection.CustomAttribute.IsCustomAttributeDefined(Module decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)
       at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
       at System.RuntimeType.IsDefined(Type attributeType, Boolean inherit)
       at Microsoft.VisualStudio.ServicesProxy.WcfSilverlightCodeGenerationExtension.WsdlImporting(WsdlImporter importer)

    Warning 2
    ===================

    Custom tool warning: Removing unsupported type System.Windows.Browser.HttpUtility
    System.TypeLoadException: Could not load type 'System.Security.SecuritySafeCriticalAttribute' from assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
       at System.ModuleHandle.ResolveType(Int32 typeToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)
       at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
       at System.Reflection.Module.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
       at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, RuntimeMethodHandle& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
       at System.Reflection.CustomAttribute.IsCustomAttributeDefined(Module decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)
       at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
       at System.RuntimeType.IsDefined(Type attributeType, Boolean inherit)
       at Microsoft.VisualStudio.ServicesProxy.WcfSilverlightCodeGenerationExtension.WsdlImporting(WsdlImporter importer)

    Thursday, April 24, 2008 5:04 AM
  • I notice that three people (including you) reported about this warning in this forum.. I would like to take a look at this. Could you please sample project and send it to me? You can reach me with this email mchlsync AT gmail DOT com.

    The only one that I noticed is that the version of mscorlib seems wrong. It should be 2.0.5.0. That dll is located under this directory  C:\Program Files\Microsoft Silverlight\2.0.30226.2\

    Could you please check whether you are referencing the wrong one or not? Go to Solution Explorer. Open "Reference"  node of Silverlight project. Select "mscorlib" library. Choose "Open in Object Browser". Please take a look at the selection in Object browser..

    Thursday, April 24, 2008 5:25 AM
  • Hi Michael, thanks for your help.

    Yes, mscorlib has thi path: C:\Program Files\Microsoft Silverlight\2.0.30226.2\
    So, it seems to be ok.

    I send to your mail a sample project ;)

    Jordi
     

    Thursday, April 24, 2008 5:36 AM
  •  You can ignore those warnings, they shouldn't be getting generated at all.  I am working on getting rid of them right now, actually... Wink

     

    Yes, there are some known issues in Beta 1 consuming amazon and other internet web services, and sharepoint web services for that matter.  Believe me, we are well aware of the issue. :)  For Beta 1 your best bet to get started is to use a WCF or asmx web service hosted in the same project that is hosting the silverlight control.  This stuff is def. better in Beta 2.

    Thanks

    Thursday, April 24, 2008 7:25 PM
  • Thanks for your reply, Alex. It's really great to get the comment from MS.

    I have a few mail-conversations with him yesterday.. It's strange that we won't get no warning if we re-open the project. I opened the sample that he sent. I got no warning. So, I took the screenshot and sent them back to him. He said that he doesn't get any warning now. (maybe, VS is counting how many time we open VS 2008  :D ) So, he tried to remove the reference the re-add the web-service to his project. then, he got the warning again. He also said that he is using a SOAP web service implemented with gSOAP (that generate Profile 1.0a )

    Anyway, As you said, we should probably ignore those warnings. :)

    Thanks. 

     

    Thursday, April 24, 2008 11:05 PM
  • Hi Alex,

    Thanks for your help ;) As Michael tells, I use a SOAP web service, basic profile 1.0a, implemented with gSOAP (for some reasons we must use it). We are very interested on implementing some stuff with Silverlight, but our currently web service must be the same. Do you know whether in Beta 2, Silverlight will support more SOAP profiles appart from 1.1? This would be nice :)

    Thanks again, and thanks Michael too!!

     
    Jordi
     

    Friday, April 25, 2008 3:38 AM
  • Our goal with Beta 2 is to support all WS-I Basic Profile compliant services out there.  note one potentially significant "gotcha": the BP prohibits use="encoded" services and Silverlight 2 will not support them.

    If you could send me a pointer to the service you're using I could try it out with the latest Beta 2 bits and make sure it works..

    thanks

    Friday, April 25, 2008 11:32 AM
  •  Hi Alex,

    If you could give me an email I would send you the WSDL. I have checked it and it had use="encoded". I have removed all of them and I get no error, but the service does not run properly. My email is jtost [at] activasoft [dot] com.

    Thanks for your help!

    Jordi
     

    Monday, April 28, 2008 3:25 AM
  • I replied off thread as well.  Just removing use="encoded" attribute from the WSDL won't do it; if the service describes itself as use="encoded" it won't be supported in Silverlight (except of course you can always handcraft the SOAP messages).  Often internet web service will provide more than one version of the same service; perhaps there's a newer version that doesn't use use="encoded"?

    Monday, April 28, 2008 11:11 AM