Web服務軟體工廠(WSSF)演練之五:建立簡單的用戶端,測試呼叫服務

來源:互聯網
上載者:User

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 ,然後單擊確定。

請注意,所有的綁定資訊已添加到您的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();

}

}

}

當我運行此代碼,看到如下的輸出:

如果您已經看到了輸出結果,那麼恭喜你,您已經掌握了使用WSSF建立Web Service的一般過程和步驟,您已經建立了一個可以使用的Web服務軟體工廠,並且可以在此基礎上進一步擴充此服務的功能。如果您沒有看到正確的輸出結果,請仔細檢查您的步驟,如果您仍遇到問題,可以在這裡提出您的問題,我將儘力為您提供協助。

以上所有代碼為了演練而被寫為一次性代碼,所以它們應該使用一些重構和一些最佳化才完美。本教程是您用WSSF建立“Hello World”的一種方式,歡迎大家提出您的觀點,我們一起討論。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.