No matter how frumpy SAP may look most of the time, nevertheless you can build such modern things like web services inside the SAP system. Even the WSDL (Web Service Description File) file can be generated in SAP. I read about this feature (WSDL generation) just at the right time, because recent days I should write a web service in SAP which should be triggered by a C# application.
In .NET web services can easily integrated via their WSDL file by using the WebReference feature of Visual Studio. Therefore Visual Studio generates a so-called proxy class from the given WSDL file which can be used to call all functions of the webservice without thinking too much.
As far as in theory. In practice I had to choose the security mode for the web service in SAP. This mode affects how a user/client/consumer has to authenticate himself against the web service. For my web service I had choosen BasicAuth as security mode. (BasicAuth is a simple authentification model which is based on username and password.)
Unfortunately Visual Studio refuses to do a good job when generating the proxy class from SAP’s WSDL file. Namely it generates the proxy class but however it deranges the authentification code. So if you create an object of the proxy class and try to call any of the web service’s methods you will get an error message like this one:
“The HTTP request is unauthorized with client authentication scheme ‘Anonymous’. The authentication header received from the server was ‘Basic realm=”SAP Web Application Server XYZ”‘.”
SAPwebservice.YS_TEST_SERVICE sapClient = new SAPwebservice.YS_TEST_SERVICE(); sapClient.ClientCredentials.UserName.UserName = "my_username"; sapClient.ClientCredentials.UserName.Password = "my_password"; int result = sapClient.TestMethod("1234", "My test data.");
And I even had passed my username and my password in foresight. So how to fix this?
In order to “teach” the generated proxy class to use BasicAuth with credentials in the right way to communicate with the SAP web service, it is necessary to pass the following parameters to the constructor of the proxy class.
using System.ServiceModel; [...] //Manually reconfigure the binding for SAP-BasicAuth BasicHttpBinding basicAuthBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); basicAuthBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; EndpointAddress basicAuthEndpoint = new EndpointAddress("http://my_sap_server:mein_port/sap/bc/srt/rfc/sap/my_webservice?sap-client=my_client&wsdl=1.1"); SAPwebservice.YS_TEST_SERVICE sapClient = new SAPwebservice.YS_TEST_SERVICE(basicAuthBinding, basicAuthEndpoint);
If you create the binding by hand, you can specify the authentication method. In this way you can persuade the proxy class to use the credentials (my_username and my_password) we set up in the code above.
That’s the way to use SAP web services in combination with C#/.NET clients.