1

How to use C# Web Service Client with BasicAuth (for SAP)

SAP Webservice with .NET Webservice Client and BasicAuthNo 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.

0

How to use alias for using-directives and namespaces in C#

Anyone who has already written one or the other program in Visual Studio with C# probably knows the following problem. Sometimes you use different libraries (Dlls) from different developers/vendors. Partly also libraries that fulfill similar purposes. This might go well, but it doesn’t have to.

Often you will get confronted with an error message like this: “‘XYZ’ is an ambiguous reference between ‘LibraryA.XYZ’ and ‘LibraryB.XYZ'”

What next? In the following I’ll explain how to solve this kind of error by taking the example of the AForge library. For instance we profess that we included the following using-directives into our program.

using System.Drawing;
using AForge.Imaging;

So now, if we use the class “Image” in our program, we will see the following error message, which result from the fact that there are two “Image” classes referenced. One in the System.Drawing namespace as also one in the AForge.Drawing namespace. Visual Studio (and later on it’s compiler) doesn’t know which Image class to use.

Visual Studio ambiguous reference error

 

One way to solve this would be to weigh which one of the both classes is used more frequently and then remove the using-directive for the namespace which contains the implementation which is used less frequently. But then you had to specify the full path everytime you want to use the class which isn’t referenced as using directive.

Image img = AForge.Imaging.Image.FromFile(imagePath);

However this is (depending on the number of calls of this class and the length and nesting of the namespaces) really awkward.

Much simpler and more elegant (atleast to my mind) is to make an alias for a namespace. Setting an alias can be done directly when setting up the using directive. On the basis of our example, we could rewrite the Aforge using directive as follows.

using System.Drawing;
using AF = AForge.Imaging;

From now on we always can write a simple “AF.” instead of the much longer “AForge.Imaging.”. On the one hand this solves the problem of the ambiguous references and on the other hand it helps us to easily distinguish between the different references.

Image img = AF.Image.FromFile(imagePath);

This was easy, wasn’t it? Now let me know if you already used namespace aliases or if this topic was news to you?

0

How to set default editor in WinSCP

When it comes to secure file transfer, one or the other of you should probably have heard of the SFTP-client named WinSCP. WinSCP enables the secure transfer of data between two computers based on either the SSH protocol or the SFTP protocol and provides, as also many of the normal FTP-clients do, the functionality of editing files directly on the server (respectively edit them direct out of the WinSCP interface without the premise to manually download the files before editing).

Unfortunately WinSCP will open all text files in his own simple style editor. But if you want to edit script files like PHP scripts, which is what I want to do most times, you will miss the comfort of a good text editor like Notepad++ or Sublime Text. But don’t worry – there’s a solution also for this problem, so that you don’t have to give up your beloved features like syntax highlighting and autocomplete. How to connect WinSCP with the editor of you choice is what I’ll show to you in the following senteces. (For this article we pretend Notepad++ is our favourite text editor.)

Step 1

how to setup a custom text editor in WinSCP - step 1  how to setup a custom text editor in WinSCP - step 2

After you have opened WinSCP you will see the login screen with a tree menu at the left hand. Click on the “Preferences” menu item and then on the “Preferences…” button on the right side.

Step 2

how to setup a custom text editor in WinSCP - step 3  how to setup a custom text editor in WinSCP - step 4

Now switch to the subitem named “Editors”. At this point you can add, edit and remove editors for different file formats. So if you have configured different editors for different file types WinSCP will open your preferred editor everytime you click on the “Edit” button in the WinSCP file manager. To add a new editor simply click on the “Add …” button.

Step 3

how to setup a custom text editor in WinSCP - step 5  how to setup a custom text editor in WinSCP - step 6

Choose the option “External editor:”, click on the “Browse…” button and select the path to the Notepad++ executable file. Now you have to define for which file types Notepad++ should be opened. If you want to use Notepad++ for all kind of file types just enter “*.*”. The asterisks are acting as wildcards. That means “*.*” is equally to “any-filename.any-filetype”. If you want to use Notepad++ only for PHP files you should enter this “*.php”.

Step 4

If you’ve done everything allright, Notepad++ should be available out of WinSCP from now on. To proof if everything is ok, open WinSCP and log into one of your servers. Now rightclick on any (text-)file and click on the “Edit” menu entry and WinSCP should automatically download the file and open it in editmode in Notepad++. If you’ve done your changes, save the file in Notepad++ and WinSCP will automatically upload it to the server. So that’s how coding makes fun!

I hope the article was understandable. Nevertheless, if you have any questions, just write me a comment below this post.

0

Win8 BTD – [B]oot[T]o[D]esktop

Win8 BTD LogoI already wrote about how you can boot directly into the classic desktop in Windows 8. However those, who do not like to get their hands dirty while working with the Windows registry or just want quickly switch back and forth, should pay me their close attention for a moment. I developed a tiny tool which is called Win8 BTD where BTD stands for Boot To Desktop.

Win8 BTD is a small tool written by me, which does not more or less than to set the appropriate settings in the registry, so that Windows 8 launches directly into the classic desktop. Of course, any changes made can also be undone with a single click, if you would rather want to start again directly into the new tile view.

Download: Win8 BTD – Version 1.0.0.0

Screenshots

Win8 BTD - [B]oot[T]o[D]esktop  1.0.0.0 Screenshot 1  Win8 BTD - [B]oot[T]o[D]esktop  1.0.0.0 Screenshot 2

The tool requires .NET framework 4.5, which is, however, included in Windows 8. About feedback or requests for changes I would be very happy as always.

0

How to boot directly to desktop in Windows 8

Meanwhile everybody should have seen or atleast heard of the new user interface which comes with Windows 8. One of the big changes is that your computer will directly boot into the new tile-(start-)menu. I personally do not mind about that, but I’ve heard of many people who would like to boot directly into the (classic) desktop view without beeing confronted with the metro/tile-view at first. Nothing easier than that.

To boot directly into the classic desktop, only one value needs to be adjusted in the registry. What it is and how it needs to be changed, I want to show you briefly below.

Step 1:

windows_8_direkt_in_den_desktop_booten_1

At first you have to open the registry. (Therefore either search for “regedit” in the apps menu or start the “Run as”-dialog by pressing Win+R and then enter “regedit”.)

Step 2:

windows_8_direkt_in_den_desktop_booten_2

Navigate to the following key (as shown in the screenshot above):

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

There you will find a key-value-pair with the name “Shell”.

Step 3:

windows_8_direkt_in_den_desktop_booten_3  windows_8_direkt_in_den_desktop_booten_4

Change the value of the Shell entry from “explorer.exe” to “explorer.exe /select,explorer.exe” and confirm the changes by clicking “OK”. From the next reboot on, you will boot directly into the classic desktop. If you want to reverse your changes and boot back into the ModernUI tile view, you only need to change the value of Shell back to “explorer.exe” and reboot once again.

Page 1 of 28