简体   繁体   中英

UWP (Universal Windows Platform) Web Service in Visual Studio 2015 C#

I'm pretty much new at StackOverflow and UWP. Ive being programming in C# for like 2 years almost but never did UWP before.

Since UWP doesn't admit database direct connection, I was trying to create a web service. Now, I being playing with the "Hello World" by default in Visual Studio´s web service file and trying to make a reference in the UWP app but I cant make a simple textblock to display what the method does in the web service (returns a "Hello World"). So here is the code:

1) Web Service by default

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService
{
    [WebService(Namespace = "http://tempuri.org/",Name ="Transfer")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

public class WebService1 : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}
}

2) UWP App

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


namespace UWPWSApp
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            WebServiceTransfer.TransferSoapClient tsc = new WebServiceTransfer.TransferSoapClient();
        }
    }
}

So the problem gets here, because I see a "HelloWorldAsync()", here is a screen:

ScreenCapture

I've already tried with the Async method but doesn't seem to be the one that displays "Hello World".

First of all, tsc.HelloWorldAsync() method is a asynchronous method:

在此输入图像描述

When you use this method, you will need use await operator, and you must include the "async" keyword in the method declaration in which you use this await operator.

public MainPage()
{
    this.InitializeComponent();
}

This is constructor of MainPage, it initializes a new instance of the MainPage class. It's not a method and constructor can't not be used with await operator, so you can for example using the Loaded event here.

Secondly, I saw your code like this:

textBlock.Text = tsc.HelloWorldAsync();

The Text property of a TextBlock should be a string, but tsc.HelloWorldAsync() returns a HelloWorldResponse type as you can see in my picture up, the type here is not match, so you can code it like this:

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += MainPage_Loaded;
}

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    WebServiceTransfer.TransferSoapClient tsc = new WebServiceTransfer.TransferSoapClient();
    var response = await tsc.HelloWorldAsync();
    textBlock.Text = response.Body.HelloWorldResult;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM