0% found this document useful (0 votes)
60 views17 pages

Integrating With SOAP Web Services in .NET Core - by Magnus Stuhr - Compendium - Medium

Uploaded by

muthu.parvathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views17 pages

Integrating With SOAP Web Services in .NET Core - by Magnus Stuhr - Compendium - Medium

Uploaded by

muthu.parvathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

9/21/23, 11:33 PM Integrating with SOAP web services in .

NET Core | by Magnus Stuhr | Compendium | Medium

This member-only story is on us. Upgrade to access all of Medium.

Member-only story

Integrating with SOAP web services


in .NET Core
Magnus Stuhr · Follow
Published in Compendium · 5 min read · Apr 2, 2019

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

Search Medium Write

SOAP (Simple Object Access Protocol) is a messaging protocol specification


for exchanging structured information in the implementation of web
services, by using the XML Information Set as its message format (source:
https://en.wikipedia.org/wiki/SOAP). The messages can go through a variety
of application layer protocols, for instance HTTP(s).

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

1 Install Microsoft WCF Web Service Reference Provider in


Visual Studio
The first thing we need to do is to make sure the Microsoft WCF Web Service
Reference Provider is added as an extension. It should be installed by default
in Visual Studio 2017, and should leave you with an option of “Connected
Service” when right-clicking on your project and hovering over the “Add”
option drop-down.

2 Generate code from a WSDL file


2.1 Right-click on your project and click the option “Add” and “Connected
Service”

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:

3 Set up endpoint and user credentials


If we want to integrate with an HTTPS endpoint with basic-auth, we have to
do some modification to the generated code.

3.1 Configure the endpoint-configuration code

Replace all of the following code:


https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 5/17
9/21/23, 11:33 PM Integrating with SOAP web services in .NET Core | by Magnus Stuhr | Compendium | Medium

..with this code:

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

1 private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(TimeSpan timeout)


2 {
3 var httpsBinding = new BasicHttpsBinding();
4 httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
5 httpsBinding.Security.Mode = BasicHttpsSecurityMode.Transport;
6
7 var integerMaxValue = int.MaxValue;
8 httpsBinding.MaxBufferSize = integerMaxValue;
9 httpsBinding.MaxReceivedMessageSize = integerMaxValue;
10 httpsBinding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
11 httpsBinding.AllowCookies = true;
12
13 httpsBinding.ReceiveTimeout = timeout;
14 httpsBinding.SendTimeout = timeout;
15 httpsBinding.OpenTimeout = timeout;
16 httpsBinding.CloseTimeout = timeout;
17
18 return httpsBinding;
19 }
20
21 private static System.ServiceModel.EndpointAddress GetEndpointAddress(string endpointUrl)
22 {
23 if (!endpointUrl.StartsWith("https://"))
24 {
25 throw new UriFormatException("The endpoint URL must start with https://.");
26 }
27 return new System.ServiceModel.EndpointAddress(endpointUrl);
28 }

Reference.cs hosted with ❤ by GitHub view raw

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.

3.2 Configure the constructors of your web service client

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

Replace all of the following code:

..with this code:

1 public MyServicePortTypeClient(string endpointUrl, TimeSpan timeout, string username, string pass


2 base(MyServicePortTypeClient.GetBindingForEndpoint(timeout), MyServicePortTypeClient.GetEndpoint
3 {
4 this.ChannelFactory.Credentials.UserName.UserName = username;
5 this.ChannelFactory.Credentials.UserName.Password = password;
6 ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
7 }
8
9 public MyServicePortTypeClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel
10 base(binding, remoteAddress)
11 {
12 }

Reference.cs hosted with ❤ by GitHub view raw

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.

4 Integrating with the generated code for the web services


You can now have an interface called “MyServicePortType” with its
implementation class called “MyServicePortTypeClient”. This class can be
used to execute requests to the web service.

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.

5.1 The specified SOAP Action is incorrect

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 <binding name="CreatePersonBinding" type="ws:CreatePersonPortType">


2 <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
3 <operation name="CreatePerson">
4 <soap:operation soapAction="urn:processDocument" style="document" />
5 <input>
6 <soap:body use="literal" />
7 </input>
8 <output>
9 <soap:body use="literal" />
10 </output>
11 </operation>
12 </binding>

MyWsdl.wsdl hosted with ❤ by GitHub view raw

Incorrect specified actions will probably lead to mysterious processing


errors, or even request timeouts. Make sure you have specified the correct
soapAction that the endpoint is set up with. The binding to this in the
generated code will look something like this:

1 [System.ServiceModel.OperationContractAttribute(Action="urn:processDocument", ReplyAction="*")]

Reference.cs hosted with ❤ by GitHub view raw

5.2 Elements have xsi:nil=”true” attributes

Some web services do not support xsi:nil=”true” for marking null values, like
this:

1 <SOMELEMENT xsi:nil="true" />

MyWsdl.wsdl hosted with ❤ by GitHub view raw

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)]

Reference.cs hosted with ❤ by GitHub view raw

Simply by removing the IsNullable attribute, the “xsi:nil=true” attribute will


not occur on the element in the XML if the value of it is null — rather the
XML serializer will remove this element altogether. To achieve this, the
XmlElement declaration should look like this:

1 [System.Xml.Serialization.XmlElementAttribute()]

Reference.cs hosted with ❤ by GitHub view raw

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>

MyWsdl.wsdl hosted with ❤ by GitHub view raw

In order to achieve inheritance from parents, we should declare in the code


that the parent is a wrapper element, and a root node like this:

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 [System.ServiceModel.MessageContractAttribute(IsWrapped=true, WrapperName = "Parent", WrapperNames


2 [System.Xml.Serialization.XmlRoot(Namespace = "http://myschema.org")]
3 public partial class ParentWrapper
4 {
5 }

Reference.cs hosted with ❤ by GitHub view raw

When we do this, the XML will be serialized like this instead:

1 <test:Parent xmlns:test="http://myschema.org">
2 <test:Child1>Test</test:Child1>
3 <test:Child2>50</test:Child2>
4 </test:Parent>

MyWsdl.wsdl hosted with ❤ by GitHub view raw

..or like this (depending on the XML serializer):

1 <test:Parent xmlns:test="http://myschema.org">
2 <Child1>This is a value</Child1>
3 <Child2>50</Child2>
4 </test:Parent>

MyWsdl.wsdl hosted with ❤ by GitHub view raw

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

When I was starting implementing this integration with nothing to go by, I


found it challenging to get everything configured correctly, and knowing
what I did wrong when the integrations did not work properly. I hope this
blog post can help those of you who are in the same situation as I was back
then. Also, any feedback on errors or missing pieces are warmly welcome.

Programming Dotnet Coding Technology Web Services

Written by Magnus Stuhr Follow

230 Followers · Writer for Compendium

Systems architect & DevOps

More from Magnus Stuhr and Compendium

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

Magnus Stuhr in Compendium Erik Tallang in Compendium

Integrating applications with Azure Dynamic themes in Angular


Active Directory in .NET Core Material
Connect, authenticate, and authorize users in How to change theme in Angular Material
Active Directory via .NET Core / C#. with the click of a button

· 9 min read · Jun 11, 2018 9 min read · Jun 19, 2018

29 2 604 14

Mads Opheim in Compendium Magnus Stuhr in Compendium

Modern Java in the Cloud, Part 1: Self-contained “real” integration


Introducing MicroProfile tests — with example in .NET Core
This is the first post in a series where we walk Integration tests often require time
through everything you need to do to have a… consuming setups, but there are ways to…

4 min read · Aug 22 · 6 min read · May 15, 2019

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

See all from Magnus Stuhr See all from Compendium

Recommended from Medium

Santiago Robert Virnau

Microsoft identity platform Add Microsoft Identity to an


The Microsoft identity platform allows Existing Blazor WASM App
developers to create apps where users can… This article covers setting up the Microsoft
Identity Framework and implementing…

10 min read · Sep 13 6 min read · Aug 5

1 1

Lists

General Coding Knowledge It's never too late or early to


20 stories · 356 saves start something
15 stories · 128 saves

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

ChatGPT prompts Stories to Help You Grow as a


24 stories · 404 saves Software Developer
19 stories · 384 saves

Adam Dewberry Mukesh Kumar

Automate migrating thousands of Configuring Spring Cloud


table schemas from SQL Server to… Gateway…
How to approach automating the conversion Spring cloud gateway has become the default
and migration of relational database table… api gateway within the Spring community for…

15 min read · 6 days ago 4 min read · Jun 18

1 8

Ritika adequate Jaydeep Patil

How do you handle .NET Rudderstack Introduction and


Handling .NET REST services architecture Implementation using .NET Core …
involves designing, developing, and… In this article, we learn about Rudderstack
and an example using a.NET Core Web API…

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

2 min read · Aug 17 8 min read · Mar 25

See more recommendations

https://medium.com/compendium/integrating-with-soap-web-services-in-net-core-adebfad173fb#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjZmNzI1… 17/17

You might also like