简体   繁体   English

创建和使用Web服务以使用Visual Studio 2015和C#调用方法

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

I am new to web service developing. 我是Web服务开发的新手。 I am experimenting on creating a backend for an app (which by the way is a Windows 10 Universal App). 我正在尝试为应用程序创建一个后端(顺便说一句,它是Windows 10通用应用程序)。 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. 我已经找到了大量关于此的指南,但是它们全都可以追溯到2000年代初期,并且使用的技术现在已经过时了。

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. 我使用WCF服务模板(Visual C#、. Net框架4)创建了一个新网站(文件->新建->新网站),并向其中添加了一个Web服务ASMX(右键单击project-> Add-> Add。 New Item-> Web Service(ASMX)),然后向与该Webservice关联的.cs文件中添加了一些webmethod。

[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). 然后,在我的应用程序中,我向Web服务添加了一个服务引用(右键单击引用->添加服务引用->插入本地主机地址,直至.asmx->单击执行->分配名称空间->单击确定)。 Now I can do something in my app with my webservice: 现在,我可以使用Web服务在应用程序中执行以下操作:

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. 请注意,当我在应用程序中添加引用时, ServiceReference1是我分配给Web ServiceReference1的名称空间。

This actually runs MyVoidFunction() . 这实际上运行MyVoidFunction() However I cannot directly call the method obj.MyVoidFunction() and I cannot call non void functions and get their return values. 但是,我不能直接调用obj.MyVoidFunction()方法,也不能调用非void函数并获取它们的返回值。

The questions 问题

  • How should have I created the web service? 我应该如何创建Web服务? Is asmx the right template for what I need to do? asmx是我需要做的正确模板吗? 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. 如果没有,有人可以提供有关此操作方法的文档,因为我发现的所有内容都涉及asmx,并且可以追溯到2010年之前。
  • Is there any way to directly call the methods defined in my webservice without using await and the autogenerated FunctionNameAsync() method? 有什么方法可以直接调用我的Web服务中定义的方法,而无需使用await和自动生成的FunctionNameAsync()方法?

You are finding so old documentation because web services implementations are really old and there's no news on this. 您发现的文档太旧了,因为Web服务实现的确很旧,并且没有任何新闻。 If you are making a Windows 10 app, you should stick with newer technologies and it should be an JSON API. 如果要制作Windows 10应用程序,则应坚持使用新技术,并且应使用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/ ). 尝试查找有关如何使用WebApi创建API的文档,Azure提供了针对该产品的产品( http://azure.microsoft.com/zh-cn/services/api-management/ )。 In your Windows 10 app use HttpClient to consume this API with NewtonSoft.Json to serialize and deserialize your objects. 在Windows 10应用中,使用HttpClient与NewtonSoft.Json一起使用此API来序列化和反序列化对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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