Note: All links current as of the time of this blog post
We have landed. This is tutorial part 5 of 5. In parts 1-4 (part 1 starts here) we created a web service that does some simple encryption, decryption, and hashing. Before beginning this part of the tutorial, please create a virtual directory in IIS pointing to the Web Service project that you set up in part 4 and make it a .Net application. I named the virtual directory CryptService and as such I can browse to http://localhost/CryptService/CryptService.svc and see the service.
Create a new C# Console Application. I named my application TestCryptService. Right click on your project in solution explorer and select “Add Service Reference”. Put your URL to your service in and click Go. After a few moments, you should see what was discovered about your service. Change the namespace to CryptService and click Ok.
Notice that all of the binding information has been added to your app.config. If you look at the endpoint section, you will see that /basic was added to the end of the CryptService.svc. I mentioned last time that that allows you to create multiple endpoints to the same point, and just use different bindings, etc.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="Crypt" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost/CryptService/CryptService.svc/basic" binding="basicHttpBinding" bindingConfiguration="Crypt" contract="CryptService.CryptService" name="Crypt" /> </client> </system.serviceModel> </configuration>
Now, within the Program.cs file that was created for you, enter in this code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using TestCryptService.CryptService; namespace TestCryptService { class Program { static void Main(string[] args) { TestDesEncryption(); TestRijndaelEncryption(); TestMd5Hash(); TestSha256Hash(); } /// <summary> /// This method is the only one that is commented. All of the other methods function /// in exactly the same way. The only reason they are included is for completeness, /// so that all of our server methods are hit. /// </summary> static void TestDesEncryption() { // The CryptServiceClient is the proxy created automatically for you. // It would be called CryptServiceClient whether you used svcutil.exe // yourself or you added a service reference. CryptServiceClient client = new CryptServiceClient(); EncryptionObject encryptionObject = new EncryptionObject(); encryptionObject.EncryptionAlgorithm = EncryptionAlgorithm.DES; encryptionObject.Text = "Testing our DES Encryption"; // Call the EncryptString method and get back our encrypted value string encryptedText = client.EncryptString(encryptionObject); encryptionObject = new EncryptionObject(); encryptionObject.EncryptionAlgorithm = EncryptionAlgorithm.DES; encryptionObject.Text = encryptedText; // Call the DecryptString method and get back our plain text string plainText = client.DecryptString(encryptionObject); // Output the values and see what we get. Console.WriteLine("-- DES --"); Console.WriteLine("Encrypted Text: {0}", encryptedText); Console.WriteLine("Plain Text: {0}", plainText); Console.WriteLine(); } static void TestRijndaelEncryption() { CryptServiceClient client = new CryptServiceClient(); EncryptionObject encryptionObject = new EncryptionObject(); encryptionObject.EncryptionAlgorithm = EncryptionAlgorithm.Rijndael; encryptionObject.Text = "Testing our Rijndael Encryption"; string encryptedText = client.EncryptString(encryptionObject); encryptionObject = new EncryptionObject(); encryptionObject.EncryptionAlgorithm = EncryptionAlgorithm.Rijndael; encryptionObject.Text = encryptedText; string plainText = client.DecryptString(encryptionObject); Console.WriteLine("-- Rijndael --"); Console.WriteLine("Encrypted Text: {0}", encryptedText); Console.WriteLine("Plain Text: {0}", plainText); Console.WriteLine(); } static void TestMd5Hash() { CryptServiceClient client = new CryptServiceClient(); HashObject hashObject = new HashObject(); hashObject.HashType = HashType.MD5; hashObject.StringToHash = "Some string to hash"; string md5Hash = client.HashString(hashObject); Console.WriteLine("-- MD5 Hash --"); Console.WriteLine("Original String: {0}", hashObject.StringToHash); Console.WriteLine("Hashed Value: {0}", md5Hash); Console.WriteLine(); } static void TestSha256Hash() { CryptServiceClient client = new CryptServiceClient(); HashObject hashObject = new HashObject(); hashObject.HashType = HashType.SHA256; hashObject.StringToHash = "Some string to hash"; string shaHash = client.HashString(hashObject); Console.WriteLine("-- Sha256 Hash --"); Console.WriteLine("Original String: {0}", hashObject.StringToHash); Console.WriteLine("Hashed Value: {0}", shaHash); Console.WriteLine(); } } }
When I run this code, I see this output:
That is IT! If you’ve made it this far and see that output, you are done. You are now ready to use the Web Services Software Factory to make web services yourself. As always, if you didn’t get these results, double check your steps and if you are still having problems, leave me a comment and I will try to help you.
All code was written as throwaway code, so I realize that it could use some refactoring and some optimization. The point of this tutorial was to expose you to using the WSSF in more than a “Hello World” kind of way and to at least mimic the real world with something a little bit fun.