简体   繁体   中英

Creating and consuming a web service to call a method with Visual Studio 2015 and c#

I am new to web service developing. I am experimenting on creating a backend for an app (which by the way is a Windows 10 Universal App). What I am trying to do is very simple: I want my app to ask the service to do something, after which the service runs some code and returns something. In other words I want to call a method defined in the service, from my app.

I have found tons of guides on this but they all date back to the early 2000's and use technologies that are now deemed obsolete.

What I managed to do so far

I created a new Web Site (File->New->New Web Site) using the WCF service template (Visual C#, .Net framework 4), added a Web service ASMX to it (right click on project->Add->Add New Item->Web Service (ASMX) ), then I added some webmethods to the .cs file associated to the webservice.

[WebMethod]
public void MyVoidFunction() { //Do something }

Then, in my app, I added a service reference to the webservice (right click on references -> Add service reference -> insert the localhost address up to .asmx -> click Go -> assign a namespace -> click ok). Now I can do something in my app with my webservice:

ServiceReference1.WebServiceSoapClient obj = new ServiceReference1.WebServiceSoapClient();
await obj.MyVoidFunctionAsync();

Notice that ServiceReference1 is the namespace I assigned to the webservice when I added the reference in my app.

This actually runs MyVoidFunction() . However I cannot directly call the method obj.MyVoidFunction() and I cannot call non void functions and get their return values.

The questions

  • How should have I created the web service? Is asmx the right template for what I need to do? If not, can someone provide some documentation on how to do this, because everything I found refers to asmx and dates back to before 2010.
  • Is there any way to directly call the methods defined in my webservice without using await and the autogenerated FunctionNameAsync() method?

You are finding so old documentation because web services implementations are really old and there's no news on this. If you are making a Windows 10 app, you should stick with newer technologies and it should be an JSON API. Try to find documentation about how to create an API with WebApi, Azure has an product for that ( http://azure.microsoft.com/en-us/services/api-management/ ). In your Windows 10 app use HttpClient to consume this API with NewtonSoft.Json to serialize and deserialize your objects.

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