简体   繁体   English

使用Visual Studio 2012从C#中的DLL消耗WSDL WebService

[英]Consuming a WSDL WebService from a DLL in C# using Visual Studio 2012

Is there a way to make this possible? 有没有办法使之成为可能?

I have created a simple WSDL web service with only one method, I have tested it and it is working. 我仅使用一种方法创建了一个简单的WSDL Web服务,我已经对其进行了测试并且可以正常工作。

Now I am creating a DLL and I'm adding the web reference, but when I try to use it on my code I get the following error: 现在,我正在创建一个DLL,并添加了Web参考,但是当我尝试在我的代码中使用它时,出现以下错误:

The "name of the webservice" does not exist in the current context 当前上下文中不存在“ Web服务名称”

Other weird thing it looks like the web service files are not loading and I don't see anything else than the web service folder (announcement service) 其他奇怪的事情似乎没有加载Web服务文件,除了Web服务文件夹(公告服务),我看不到其他任何东西

在此处输入图片说明

Checking on the folder I only have 3 files: a .wsdl file, a reference.cs file and a .map file 检查该文件夹,我只有3个文件: .wsdl文件, reference.cs文件和.map文件

I don't see the other .xsd and .disco files. 我看不到其他.xsd.disco文件。

Am I doing something wrong, or is it different to add a web service to a dll than a normal ASP.NET application ? 我是在做错什么,还是将Web服务添加到dll而不是普通的ASP.NET应用程序?

Thank you 谢谢

As an FYI to anyone finding this question via Google, like I did, this page is good reference: https://www.sitepoint.com/net-web-services-5-steps/ 仅供参考,就像我一样,通过Google查找此问题的任何人都可以参考此页面: https : //www.sitepoint.com/net-web-services-5-steps/

You can skip part 1, since you won't need to create a web service. 您可以跳过第1部分,因为您无需创建Web服务。 (They built a method that accepts a string and inserts it into a SQL query and returns a DataTable – a pretty common web service method) (他们建立了一个接受字符串并将其插入SQL查询并返回DataTable的方法-一种非常常见的Web服务方法)

Part 2: Create a proxy class 第2部分:创建代理类

  1. Append ?WSDL to the Web services URL, ie http://localhost/suppliers.asmx ?WSDL ?WSDL附加到Web服务URL,即http://localhost/suppliers.asmx ?WSDL
  2. Open Notepad. 打开记事本。 Put the URL from Step 1 on the end: 将步骤1中的URL放在最后:

    wsdl.exe /l:CS /n:WService /out:GetSuppliers.cs http://localhost/suppliers.asmx?WSDL

Save the file as makeWS.bat. 将文件另存为makeWS.bat。 If you ran it, it would use the wsdl utility to create a proxy class (.cs) file. 如果运行它,它将使用wsdl实用程序来创建代理类(.cs)文件。 Hold off for now. 现在暂缓。

Part 3: Build our DLL 第3部分:构建我们的DLL

  1. Add the following to use the csc utility to a single line in the batch file to create a DLL: 添加以下内容以将csc实用程序用于批处理文件中的一行以创建DLL:

    csc /t:library /out:GetSuppliers.dll GetSuppliers.cs /reference:System.dll,System.Data.dll,System.Web.dll, System.Web.Services.dll,System.XML.dll /optimize

    Now save and run makeWS.bat. 现在保存并运行makeWS.bat。

Part 4: Incorporate our DLL into our project that uses the web service 第4部分:将DLL合并到使用Web服务的项目中

  1. Add a Service Reference to the URL of the web service. 将服务引用添加到Web服务的URL。
  2. Add the DLL to your project's bin folder. 将DLL添加到项目的bin文件夹中。 Add a generic Reference (NOT Service Reference) in your project to that file. 在您的项目中将通用参考(NOT服务参考)添加到该文件。
  3. Add a “using WService;” declaration at the top of the class where you will call the web service, because that was the namespace added with /n:WService by the wsdl utility (you can change this if you like, but must be done throughout). 在将调用Web服务的类的顶部添加“ using WService;”声明,因为这是wsdl实用程序与/n:WService一起添加的名称空间(您可以根据需要更改此名称,但必须这样做始终)。

  4. Add a line like this to your class: WService mySvc = new WService(); 在类中添加如下代码: WService mySvc = new WService();

  5. You should be able to do a call on the WebMethod within the Web Service: DataTable dt = mySvc.GetDataFromWebMethodX(); 您应该能够在Web服务内的WebMethod上进行调用: DataTable dt = mySvc.GetDataFromWebMethodX();

    where GetDataFromWebMethodX() is the WebMethod you are trying to use. 其中GetDataFromWebMethodX()是您尝试使用的WebMethod。

Part 5: Some useful information to allow you to make calls to the web service asynchronously (code can proceed without having to wait for a response, and perform a callback function later), that you can look at, but usually isn't really required. 第5部分:您可以查看一些有用的信息,这些信息可以使您异步调用Web服务(无需等待响应就可以继续执行代码,以后再执行回调函数),但是通常并不需要。

It seems there was an issue, according to the question comments, with #4, which can be done independently of, and before, creating the DLL. 根据问题注释,似乎#4存在一个问题,该问题可以独立于创建DLL进行,也可以在创建DLL之前进行。 If you have problems referencing the web service URL using Service References, it has to be discoverable, and its name can't conflict with others in the same environment/domain. 如果您在使用“服务引用”引用Web服务URL时遇到问题,则它必须是可发现的,并且其名称不得与相同环境/域中的其他名称冲突。 As the question's author found, it cannot have an endpoint (address, port) defined in its configurations that conflicts with another site/web service in the same domain. 正如问题的作者所发现的,它的配置中不能定义与同一域中的另一个站点/ Web服务冲突的端点(地址,端口)。 Seems this question resulted in an answer that was more about how to set up the web service, than on how to perform the process to connect to it, but thought I'd post about both aspects, since both are important in order to consume the web service into a project and use it. 似乎这个问题导致的答案更多是关于如何设置Web服务,而不是关于如何执行连接到Web服务的过程,但我认为我会发布这两个方面的信息,因为这两个方面对于使用Web服务都非常重要。 Web服务放入一个项目并使用它。

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

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