gfole
gfole

Reputation: 75

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

Upvotes: 1

Views: 7637

Answers (1)

Luiz
Luiz

Reputation: 542

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.

Upvotes: 1

Related Questions