简体   繁体   中英

How to consume web service with WSDL file?

have been given a URL to a WSDL, a piece of it is here..not sure if this is a pertinent piece or not. I know how to add the web reference and import the namespace in Visual Studio 2005.

Questions...how do I pass my values to it? How do I encode for binary-64? I have the methodname (GETP)...not sure how to invoke it as a function to pass parameters, or if that is even the correct way to do it with this type of reference. Have only consumed .asmx files previously.

  <wsdl:message name="GETPResponse">
      <wsdl:part name="GETPReturn" type="xsd:string" /> 
  </wsdl:message>
  <wsdl:message name="GETPRequest">
      <wsdl:part name="BASE64DATA" type="xsd:base64Binary" /> 
  </wsdl:message>
  <wsdl:portType name="Dist">
     <wsdl:operation name="GETP" parameterOrder="BASE64DATA">
         <wsdl:input message="impl:GETPRequest" name="GETPRequest" /> 
         <wsdl:output message="impl:GETPResponse" name="GETPResponse" /> 
     </wsdl:operation>
  </wsdl:portType>

Just use "Add Service Refrence" and point it to the WSDL. See " How to Consume a Web Service ".

This site has how to create a WSDL class and use that, or how to reference the web service using a Visual Studio web reference: https://msdn.microsoft.com/en-us/library/ms155134.aspx

Neither option worked for me. I needed to create the WSDL class like at that site, but the web reference didn't work for me, and neither did just adding that class to my App_Code folder and trying to instantiate it. So I opted to create an assembly out of the WSDL class, then reference that assembly in my project. This was the only way I could get any useful classes/methods available to me. Here are my general steps, from start to finish, that worked for me.

  1. Ensure you have the right URL at the top in your ASMX.cs file of your web service: [WebService(Namespace = "http://localhost:99999/WebService1.asmx")]

    Leaving that as the default "tempuri.org" will cause you unbearable grief.

  2. Get the Windows SDK for your workstation/server & install.

  3. On a command line, navigate to WSDL, which for me on Server 2012 was: cd C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools

  4. Then, use this: wsdl /l:CS /protocol:SOAP http://localhost:99999/WebService1.asmx?wsdl

You can then do one of 2 things:

  • The proxy class will be generated in that same folder where you navigated on the command line. Go to it in Explorer, copy it into a new Class Library Visual Studio project, at the root of the solution. (For details, go to https://msdn.microsoft.com/en-us/library/cc175801(v=vs.90).aspx and skip to "Creating a Project for the Proxy Assembly".) Sign the assembly (details on that same site, section "Signing and Building the Proxy Assembly"). (I didn't bother with the serialization exceptions or struct stuff - just those 2 sections, only.)

-OR- (easier)

  • Just use CSC to build your assembly:

    • Move your proxy class to the C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 folder.

    • Launch a command line and cd to that folder.

    • Run this command:

      csc /t:library MyWebServiceProxyClassFile.cs /reference:System.Web.Services.dll /optimize

Obviously, replace "MyWebServiceProxyClassFile" with the name of your proxy class file. It will give you a DLL file with the same name as your proxy class.

  1. Import the assembly in your client project by going to Add Reference and browsing to the assembly in that proxy project you just built in step 4. It should be in the MyServiceProxy\\bin\\Debug folder. (Or browse to C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 if you left it there, from using CSC.)
  2. Instantiate using the class name of your web service: YourClass service = new YourClass(); You should see the webmethods available when you do service. with intellisense.
  3. You'll likely need to add these parameters:

     service.Url = "http://localhost:99999/WebService1.asmx"; service.Credentials = CredentialCache.DefaultCredentials; 

(Add using System.Net; to the top of your class to use that last one.)

For those saying this is the "old" or "obsolete" way of doing it, I say this worked for me, and would say to tell Microsoft to make it work using the web reference and we wouldn't have to do it this way. Besides, this is largely their documented process, after all, with some of my own findings put in. And I noticed something else - svcutil generates a totally different, WCF-compliant proxy class, which WSDL does not, so be careful what utility you are actually using - they are NOT created equal.

Also, I made my own batch file for creating a proxy class from a web service, then building an assembly from it using CSC, like I have above:

echo off
cls
cls
set /P svc="Input the URL to your web service's Service.asmx file: "
cd \
cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
wsdl /l:CS /protocol:SOAP %svc%?WSDL
set /P name="Type the name of the service's class' name (no .cs on end): "
move %name%.cs C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\%name%.cs
cd \
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
csc /t:library %name%.cs /reference:System.Web.Services.dll /optimize
move %name%.dll C:\%name%.dll
move %name%.cs C:\%name%.cs
echo Your DLL and Proxy Class are waiting for you at
echo.
echo   C:\%name%.dll
echo.
echo. and
echo.
echo.  C:\%name%.cs
echo.
explorer.exe C:\
pause
cls
exit

This is set up to run on Server 2012 with the Windows SDK installed. You could run it on any platform if you change the place wsdl is found to point to Visual Studio's SDK folder, if you install that, ex:

cd "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin" 

instead of that NETFX Tools folder.

You would use it by first entering the URL of the web service, ex. http://localhost:99999/WebService1.asmx

When the class is generated, you give this program the name of the class file, minus the .cs . It should also be the same as the default class name at the top of WebService1.asmx.cs, in my example.

It will then leave the proxy class and assembly file at the root of C: for you.

For those wishing to pursue converting the ASMX into a WCF interface, I found this: https://msdn.microsoft.com/en-us/library/vstudio/ms751529%28v=VS.100%29.aspx that looked promising, which has you use svcutil to produce the proxy, I'm assuming add it to an ASP.NET App_Code folder (it doesn't say) and add a service reference to the ASMX web service URL, but not a lot of detail/guidance on how to name the contract and where to put that <client> snippet in the web.config, though I believe it goes in <system.ServiceModel> .

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