简体   繁体   中英

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.

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:

The "name of the webservice" does not exist in the current context

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)

在此处输入图片说明

Checking on the folder I only have 3 files: a .wsdl file, a reference.cs file and a .map file

I don't see the other .xsd and .disco files.

Am I doing something wrong, or is it different to add a web service to a dll than a normal ASP.NET application ?

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/

You can skip part 1, since you won't need to create a web service. (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)

Part 2: Create a proxy class

  1. Append ?WSDL to the Web services URL, ie http://localhost/suppliers.asmx ?WSDL
  2. Open Notepad. Put the URL from Step 1 on the end:

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

Save the file as makeWS.bat. If you ran it, it would use the wsdl utility to create a proxy class (.cs) file. Hold off for now.

Part 3: Build our DLL

  1. Add the following to use the csc utility to a single line in the batch file to create a 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.

Part 4: Incorporate our DLL into our project that uses the web service

  1. Add a Service Reference to the URL of the web service.
  2. Add the DLL to your project's bin folder. Add a generic Reference (NOT Service Reference) in your project to that file.
  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).

  4. Add a line like this to your class: WService mySvc = new WService();

  5. You should be able to do a call on the WebMethod within the Web Service: DataTable dt = mySvc.GetDataFromWebMethodX();

    where GetDataFromWebMethodX() is the WebMethod you are trying to use.

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.

It seems there was an issue, according to the question comments, with #4, which can be done independently of, and before, creating the 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. 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. 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.

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