Integrating With SOAP Web Services in .NET Core - by Magnus Stuhr - Compendium - Medium
Integrating With SOAP Web Services in .NET Core - by Magnus Stuhr - Compendium - Medium
Member-only story
463 16
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 1/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
This blog post will have a look at how we can start of with a WSDL (Web
Services Description Language) file, generate C# code from it by importing it
into our .NET Core project, how to configure a basic-authentication over
HTTPS for the web service, and finally explore different pitfalls that can be
encountered when trying to integrate with the web service
programmatically.
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 2/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
After we have added a service, we should be left with a folder called “Connected Services”. If you already have
this, then you can right click on this folder instead.
2.2 Click the “Microsoft WCF Web Service Reference Provider” option
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 3/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
2.3 Click the “Browse” button and choose the WSDL file you want to
generate the code for
2.4 Specify the desired namespace where your generated code should be
located and click “Finish”
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 4/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
If the WSDL file is valid, the code will be generated and places in the
namespace under the “Connected Services” folder:
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 6/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
Since the client for operating on the web service is a partial class, in our case
the class “MyServicePortTypeClient”, you can also create a partial class and
add the methods above in this self-made class. This is a good solution if you
might have to re-generate the code from your WSDL file, because then you
do not have to replace the code all over again, since the code is placed into
your own partial class.
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 7/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 8/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
This way, you can specify the endpoint URL, the desired timeout of requests,
your username and your password when constructing a new instance of the
web service client.
As mentioned in step 3.1 you can also add the first constructor to your
partial class, so that you avoid loss of functionality if you have to re-generate
the code from the WSDL file.
5 Possible pitfalls
In my experience, there might occur errors when trying to execute requests
to the web service in your code. There may be many reasons for this, and
some of these aspects will be addressed here.
I have seen that WSDL files that have defined incorrect SOAP actions for
binding operations, for instance when the “urn:processDocument” action
was specified, although this was not the action configured in the web
service:
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 9/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
1 [System.ServiceModel.OperationContractAttribute(Action="urn:processDocument", ReplyAction="*")]
Some web services do not support xsi:nil=”true” for marking null values, like
this:
Instead the elements should just be removed altogether. This can be done by
removing the “IsNullable=true” attribute from the XmlElement declaration
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 10/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
in the generated WSDL code. The attribute declaration will look like this:
1 [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
1 [System.Xml.Serialization.XmlElementAttribute()]
5.3 Each child element has its own declaration of namespaces, instead of
inheriting from parent or root node
Some web services do not support that each element in the payload has its
own declared namespaces, rather than inheriting from its parent. An
example of such declared namespaces can look like this:
1 <test:Parent xmlns:test="http://myschema.org">
2 <test:Child1 xmlns:test="http://myschema.org">Test</test:Child1>
3 <test:Child2 xmlns:test="http://myschema.org">50</test:Child2>
4 </test:Parent>
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 11/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
1 <test:Parent xmlns:test="http://myschema.org">
2 <test:Child1>Test</test:Child1>
3 <test:Child2>50</test:Child2>
4 </test:Parent>
1 <test:Parent xmlns:test="http://myschema.org">
2 <Child1>This is a value</Child1>
3 <Child2>50</Child2>
4 </test:Parent>
6 Final thoughts
We have looked at how we can start off with a single WSDL file defining the
web service we want to integrate with, generating code from it via our .NET
Core project, configuring the endpoint with a HTTPS Basic-auth binding,
and finally looking at possible pitfalls when trying to execute requests to the
web service through our code.
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 12/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 13/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
· 9 min read · Jun 11, 2018 9 min read · Jun 19, 2018
29 2 604 14
8 215 2
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 14/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
1 1
Lists
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 15/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
1 8
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 16/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium
https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 17/17