Web服务软件工厂(WSSF)演练之五:创建简单的客户端,测试呼叫服务

Web Services Software Factory

Web服务软件工厂(WSSF)演练之四:创建简单的客户端,测试呼叫服务

关键字:Web Services Software Factory, Simple Client, Call Service

这是本系列教程的第五部分。如果您需要阅读之前的部分可以单击这里。我们已经建立了一个Web服务,它通过散列对字符串进行一些简单的加密和解密。在开始这部分的演练之前,请在IIS中创建一个虚拟目录,指向我们在第四部分创建的Web服务的项目,这里命名为虚拟目录CryptService ,因此我可以浏览到http://localhost/CryptService/CryptService.svc和看到的服务。

创建一个新的C #控制台应用程序。我本人命名的应用TestCryptService 。右键单击您的项目在解决方案资源管理器,然后选择“Add Service Reference”(添加服务引用) 。在添加服务引用窗口中,将您的服务地址添加到地址栏中,本演练中我添加的是http://localhost/CryptService/CryptService.svc并单击Go(前往)按钮 。过了几分钟,你应该看看发现您服务。改变命名空间CryptService ,然后单击确定。

clip_image001

请注意,所有的绑定信息已添加到您的app.config文件中 。如果你查看一下这个文件,Endpoint节中,您将看binding="basicHttpBinding" indingConfiguration="Crypt" 。我提到的最后一次,您可以创建多个Endpoints(端点)使用不同的绑定方式进行绑定。

1. <?xml version="1.0" encoding="utf-8" ?>

2. <CONFIGURATION>

3. <SYSTEM.SERVICEMODEL>

4. <BINDINGS>

5. <BASICHTTPBINDING>

6. <BINDING name="Crypt" useDefaultWebProxy="true" transferMode="Buffered" textEncoding="utf-8" messageEncoding="Text" maxReceivedMessageSize="65536" maxBufferPoolSize="524288" maxBufferSize="65536" hostNameComparisonMode="StrongWildcard" bypassProxyOnLocal="false" allowCookies="false" sendTimeout="00:01:00" receiveTimeout="00:10:00" openTimeout="00:01:00" closeTimeout="00:01:00">

7. <READERQUOTAS maxNameTableCharCount="16384" maxBytesPerRead="4096" maxArrayLength="16384" maxStringContentLength="8192" maxDepth="32" />

8. <SECURITY mode="None">

9. <TRANSPORT realm="" proxyCredentialType="None" clientCredentialType="None" />

10. <MESSAGE clientCredentialType="UserName" algorithmSuite="Default" />

11. </SECURITY>

12. </BINDING>

13. </BASICHTTPBINDING>

14. </BINDINGS>

15. <CLIENT>

16. <ENDPOINT name="Crypt" contract="CryptService.CryptService" bindingConfiguration="Crypt" binding="basicHttpBinding" address="http://localhost/CryptService/CryptService.svc/basic" />

17. </CLIENT>

18. </SYSTEM.SERVICEMODEL>

19. </CONFIGURATION>

现在,打开Program.cs文件,输入以下代码:

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();

}

}

}

当我运行此代码,看到如下的输出:

clip_image003

如果您已经看到了输出结果,那么恭喜你,您已经掌握了使用WSSF创建Web Service的一般过程和步骤,您已经创建了一个可以使用的Web服务软件工厂,并且可以在此基础上进一步扩展此服务的功能。如果您没有看到正确的输出结果,请仔细检查您的步骤,如果您仍遇到问题,可以在这里提出您的问题,我将尽力为您提供帮助。

以上所有代码为了演练而被写为一次性代码,所以它们应该使用一些重构和一些优化才完美。本教程是您用WSSF创建“Hello World”的一种方式,欢迎大家提出您的观点,我们一起讨论。

你可能感兴趣的:(Web)