none
Calling web services in a windows 8 phone app RRS feed

  • Question

  • I have a asmx web service which i am successfully returning data from in a windows RT application. The code i use looks like this;

    Resp = Await Mob.GetUserInfoAsync("", txtSitename.Text, App.Name, txtUsername.Text, txtPassword.Password)

    I added the service reference in my windows 8 phone app, its all looking good, but when i go to call the web service function it does not return anything. All the functions which return values are now declared as Sub's. What is going on here? Why do these web service functions not return values in windows 8 phone app when they do in a standard windows 8 RT app?

    Tuesday, December 11, 2012 12:40 AM

Answers

  • Try it like this:

    MyWebserviceSoapClient MyClient = new MyWebserviceSoapClient (); MyClient.MyMethodCompleted += MyClient_MyMethodCompleted; MyClient.MyMethodAsync(); oid MyClient_MyMethodCompleted(object sender, MyMethodCompletedEventArgs e) { // cast e.results to your expected return type }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Wednesday, December 12, 2012 5:53 PM

All replies

  • I see that the web service auto generated file has included methods 'BeginGetUserInfo' and 'EndGetUserInfo'. Please tell me we don't need to do this using the old school method using callbacks? Why does it not work the same as in normal RT?
    Tuesday, December 11, 2012 11:00 PM
  • Are you running the service locally on the machine where the emulator is running?
    Wednesday, December 12, 2012 1:29 AM
  • Are you running the service locally on the machine where the emulator is running?
    No, it is running on our servers and is accessed over the internet as it would be for clients running it. How would that make any difference to the way it is used in code?
    Wednesday, December 12, 2012 2:16 AM
  • It takes some extra configuration to use local services. It can be an issue.

    Should work. Your response isn't typed. Check the proxy class. Check your capabilities.

    Wednesday, December 12, 2012 2:12 PM
  • I'm seeing the same thing when I call my known-good web service from C#.  I'm checking into this and will update when I know more.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, December 12, 2012 4:24 PM
  • Try it like this:

    MyWebserviceSoapClient MyClient = new MyWebserviceSoapClient (); MyClient.MyMethodCompleted += MyClient_MyMethodCompleted; MyClient.MyMethodAsync(); oid MyClient_MyMethodCompleted(object sender, MyMethodCompletedEventArgs e) { // cast e.results to your expected return type }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Wednesday, December 12, 2012 5:53 PM
  • I just don't understand why the web service methods do not return a task(of x) and therefor can be awaited?

    This is exactly the same web service i use in my RT app, added exactly the same. Everything is the same, but the web service calling code is generated differently. Why is that?

    The other problem with using the callback is that the callback runs on the background thread. How do i invoke it back onto the main thread so i can made UI changes? This is just a nightmare, i thought this was part of the attraction of the new windows runtime. I cant even make use of the async and await methods i have to use the convoluted old school method.

    eschumac

    Are you able to maybe explain further what you mean? What capabilities would be required?

    Wednesday, December 12, 2012 9:17 PM
  • Sure, ID_CAP_NETWORKING.

    Read down the page for System.ServiceModel.BasicHttpBinding in the API tree:

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.servicemodel.basichttpbinding%28v=vs.105%29.aspx

    Its also mentioned at the top of the System.ServiceModel page.

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.servicemodel%28v=vs.105%29.aspx

    Thursday, December 13, 2012 1:26 AM
  • I have ID_CAP_NETWORKING included already.

    Its probably just me, but i am unsure how those links help me... I can connect to my web service, but my problem is that i need to go through this convoluted process involving callbacks and marshalling back to the main thread after the call completes.

    All i want to do is add a service reference and have it act the same as in a normal WinRT application. When i add the service reference in WinRT, this is the code for a function im trying to use that is generated;

    Public Function GetUserInfoAsync(ByVal AppleDeviceToken As String, ByVal Sitename As String, ByVal CallingApplication As String, ByVal Login As String, ByVal Password As String) As System.Threading.Tasks.Task(Of MobileServices.GetUserInfoResponse)

    When i add it in a windows phone app, in exactly the same way using 'add service reference', this is what is generated;

    Public Overloads Sub GetUserInfoAsync(ByVal AppleDeviceToken As String, ByVal Sitename As String, ByVal CallingApplication As String, ByVal Login As String, ByVal Password As String)

    See the difference?

    In WinRT, the method returns a task which i can 'Await' inside an async method and get the return value and its easy as. With windows phone, it does not even provide a return value at all. Instead it provides me with BeginGetUserInfo and EndGetUserInfo functions which are a pain to implement and seem to be a step backwards from the new way of doing things using async and await.




    • Edited by luker459 Thursday, December 13, 2012 1:41 AM
    Thursday, December 13, 2012 1:38 AM
  • The code I posted works.  Why don't you try it? 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, December 13, 2012 2:43 PM
  • The code I posted works.  Why don't you try it? 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Matt,

    You are suggesting this as a work around and acknowledging that the generated code is not what the OP should be expecting, correct?

    Thursday, December 13, 2012 2:55 PM
  • No, I am suggesting that the code that I posted is the correct way in which to call the webservice. It does not involve BeginGetUserInfo and EndGetUserInfo functions.  It involves a simple Completed function which is called when the response is delivered to the client application.

    I would prefer it acted as the OP wants it to.  However, I spoke with the development team about this yesterday, and this was the proper approach to obtain the webservice results.  It does not work like Windows RT with nice async/await operators, but arguing about why it's wrong/right doesn't get anyone anywhere.  BTW, I specifically asked about this construct - it's the traditional asynchronous method of calling a webservice since 2006 and appeared in previous versions of .NET. 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, December 13, 2012 3:10 PM
  • Thanks for the insight. That sort of information is really helpful in forum posts. Thank you.

    Out of curiosity did the dev team indicate that they would be taking any action moving forward to align the constructs between the platforms to enable what we all expect (like the OP expected) that the code should be the same on both platforms for such a core function?

    Thursday, December 13, 2012 3:41 PM
  • We have no comments about future releases.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, December 13, 2012 4:00 PM
  • Always so warm and fuzzy, Matt ;)

    Can we request from the team that someone create a nice code sample and paper in the documentation so that people don't need to go hunting through forum posts etc to decipher the proper and prescribed approach? I am sure many more will struggle and like the OP become frustrated because things aren't as expected.

    Thursday, December 13, 2012 4:21 PM
  • Always so warm and fuzzy, Matt ;)

    Can we request from the team that someone create a nice code sample and paper in the documentation so that people don't need to go hunting through forum posts etc to decipher the proper and prescribed approach? I am sure many more will struggle and like the OP become frustrated because things aren't as expected.

    Completely agree.

    Thank you matt for giving us the answer. I find it very helpful to understand why something doesn't work, rather than just being given some other way that it does work. Im having the same problem right now with a databinding post on here.

    Thursday, December 13, 2012 8:21 PM
  • I'm going to write a blog post about this on the Windows Store Developer Solutions blog: http://blogs.msdn.com/b/wsdevsol/

    Store and Phone support teams have joined together as the APIs and experiences are so similar (that's why I'm in these forums now...)  I think I'll get it done this week.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, December 17, 2012 4:55 PM
  • Matt, that's great. Thanks for the update and efforts. I'm sure this will go a long way to build confidence in the platform and community.
    Monday, December 17, 2012 10:04 PM
  • It may unrelated, I am new to win phone development, but I have seen similar issues with other platforms.

    It may be grasping at straws or may help someone out in the future.

    Check that the code you are using is not require a wsdl, if it is, try to access the wsdl from the phone browser. Many companies now turn off the wsdl for security reasons. If you just get a blank page test on another we-bservice you can access the wsdl on. You would either have to figure a way around that or have them modify the configuration on the server.

    The other potential is a long time ago with flash apps if you had the flash file on another server, you had to place a config file in the web-service directory if they were cross domain. That may not be the case in Flash anymore and may not be a security feature implemented, but it appears it could be based on this article.

    http://msdn.microsoft.com/en-us/library/cc838154(v=vs.95).aspx

    Wednesday, December 19, 2012 12:26 AM