I need to consume a Web Service. They sent me the WSDL file. What should I do to add it to my website and start using it as the proxy. ( If I put it on a Virtual Directory it can be discovered, but does it grant me the connection with the real web service?)
I would fire up Visual Studio, create a web project (or console app - doesn't matter).
For .Net Standard:
- I would right-click on the project and pick "Add Service Reference" from the Add context menu.
- I would click on Advanced, then click on Add Service Reference.
- I would get the complete file path of the wsdl and paste into the address bar. Then fire the Arrow (go button).
- If there is an error trying to load the file, then there must be a broken and unresolved url the file needs to resolve as shown below: Refer to this answer for information on how to fix: Stackoverflow answer to: Unable to create service reference for wsdl file
If there is no error, you should simply set the NameSpace you want to use to access the service and it'll be generated for you.
For .Net Core
- I would right click on the project and pick Connected Service from the Add context menu.
- I would select Microsoft WCF Web Service Reference Provider from the list.
- I would press browse and select the wsdl file straight away, Set the namespace and I am good to go. Refer to the error fix url above if you encounter any error.
Any of the methods above will generate a simple, very basic WCF client for you to use. You should find a "YourservicenameClient" class in the generated code.
For reference purpose, the generated cs file can be found in your Obj/debug(or release)/XsdGeneratedCode and you can still find the dlls in the TempPE folder.
The created Service(s) should have methods for each of the defined methods on the WSDL contract.
Instantiate the client and call the methods you want to call - that's all there is!
YourServiceClient client = new YourServiceClient();
client.SayHello("World!");
If you need to specify the remote URL (not using the one created by default), you can easily do this in the constructor of the proxy client:
YourServiceClient client = new YourServiceClient("configName", "remoteURL");
where configName
is the name of the endpoint to use (you will use all the settings except the URL), and the remoteURL
is a string representing the URL to connect to (instead of the one contained in the config).
In visual studio.
- Create or open a project.
- Right-click project from solution explorer.
- Select "Add service refernce"
- Paste the address with WSDL you received.
- Click OK.
If no errors, you should be able to see the service reference in the object browser and all related methods.
Use WSDL.EXE utility to generate a Web Service proxy from WSDL.
You'll get a long C# source file that contains a class that looks like this:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="MyService", Namespace="http://myservice.com/myservice")]
public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol {
...
}
In your client-side, Web-service-consuming code:
- instantiate MyService.
- set its Url property
- invoke Web methods
If you want to add wsdl reference in .Net Core project, there is no "Add web reference" option.
To add the wsdl reference go to Solution Explorer, right-click on the References project item and then click on the Add Connected Service option.
Then click 'Microsoft WCF Web Service Reference':
Enter the file path into URI text box and import the WSDL:
It will generate a simple, very basic WCF client and you to use it something like this:
YourServiceClient client = new YourServiceClient();
client.DoSomething();
© 2022 - 2024 — McMap. All rights reserved.