SAP PO SOAP Web Service with .NET Core client
Dear SAP PI/PO consultants,
It has been a while(3 years) since I wrote “.Net C# SOAP Web Service Client Example for SAP PI/PO Services” .NET Core has gained popularity since then.
There is also a tendency to request REST services. However, I still think there are many valid use cases where SOAP is the better option. In order to speed up things, you can pass this blog post when you are working with .NET developers.
- .NET Core SDK is installed
Generate Web Service Client Reference Classes & add libraries
Java wsimport equivalent of .NET Core is dotnet-svcutil
dotnet tool install --global dotnet-svcutil dotnet tool list -g
Usually it adds the tools to the PATH. You may need to close & reopen the terminal. If that doesn’t work you should add this directory to Environment variable PATH:
Generate client classes with a WSDL:
Generate classes with namespace:
dotnet-svcutil SI_SEND_ORG_DATA.wsdl -n *,MDP.MyAwesomePO.WSClient
This command will create a Service Reference. You can copy the class to your project. By default, it doesn’t create sync methods but there is an option for that(–sync). You can use –help to see options.
You have to add these packages to your project:
dotnet add package System.ServiceModel.Primitives dotnet add package System.ServiceModel.Http
Don’t forget to run “dotnet restore”.
Use the Reference Classes
I like to set endpoint, username, and password programmatically. Binding configuration helps if you have to use basic authentication over HTTP.
String endpointurl = "http://hostname:port/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_SENDER_SYSTEM0&receiverParty=&receiverService=&interface=SI_SEND_ORG_DATA&interfaceNamespace=http://example.com/HR/OrganisationData"; BasicHttpBinding binding = new BasicHttpBinding(); //If you need HTTP with Basic Auth for internal network or dev environments. Otherwise remove these two lines: binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; EndpointAddress endpoint = new EndpointAddress(endpointurl); var wsclient = new SI_SEND_ORG_DATAClient(binding, endpoint); wsclient.ClientCredentials.UserName = "username"; wsclient.ClientCredential.Password = "password"; //Here you can use client var request = new SI_SEND_ORG_DATA(); request.field="value" var response = await wsclient.SI_SEND_ORG_DATAAsync(request);
Beware of the integer/double
Coming from Java one of the things that bit me was setting an integer value for a SOAP request.
If you have a field like xsd:integer or xsd:double that maps to .Net built-in types which don’t have null, you have to set Specified value to true.
request.FirstNumber = 2; request.FirstNumberSpecified=true;
This is a solution to differentiate between an element with 0 and no element. I’m sure you experienced this behavior while trying to send an integer with 0 value using ABAP Proxy. Because in ABAP, integer initial value is 0 and regarded as null for outbound XML processing. 🙂
See you in next blog posts!
thanks for the blog. As a person with a C# background I really appreciate the blogs where SAP and .NET go hand-in-hand. One question: Why do you use the commandline util svcutil to generate the classes? On your first/old article you used the "Service Definition" gui from Visual Studio. As far as I know this is still available, even for .NET Core applications. Any specific reasons why you switched to the console? 🙂
That is a good question. Yes, it may work with the GUI also!
I have a few reasons:
-I don't use C# and Visual Studio daily. Since .Net Core came out, I prefer terminal + VS Code to poke around. So I haven't checked Visual Studio GUI, to be honest.
-As a reader I prefer command-line tutorials. I find them expressive, concise, and easy to reproduce.
-This solution also works on Linux/Mac as a side benefit 🙂
Thanks for your contribution.
thanks for your response. I would say these are valid points you listed. No further questions. 😉
I have successfully run the project, but the following error is returned from the service.
System.ServiceModel.FaultException: Web service processing error; more details in the web service error log on provider side (UTC timestamp 20230321182957; Transaction ID 5D16C8ED31E9F1508197005056885816)
How can I solve this error, can you help?