《Windows Azure Platform 系列文章目錄》html
這幾天工做上的內容,把項目文件和源代碼拿出來給你們分享下。web
源代碼下載:Part1 Part2 Part3windows
咱們在寫WEB服務的時候,常常須要把本地的文件上傳至服務器端進行保存,相似於上傳附件的功能。瀏覽器
在本章中,咱們將新建WCF服務並上傳至Azure雲端,實現把本地的圖片經過WCF保存至Azure Storage。服務器
本章須要掌握的技術點:app
1.WCF服務負載均衡
2.Azure Storagepost
本章將首先介紹服務器端程序代碼。ui
1.首先咱們用管理員身份,運行VS2013。this
2.新建Windows Azure Cloud Project,並命名爲LeiAzureService
3.添加"WCF Service Web Role",並重命名爲LeiWCFService。
4.打開IE瀏覽器,登陸http://manage.windowsazure.com,在Storage欄,新建storage name爲leiwcfstorage
5.咱們回到VS2013,選擇Project LeiAzureService,展開Roles->LeiWCFService,右鍵屬性
6.在彈出的窗口裏,Configuration欄,將Instance count設置成2, VM Size選擇Small
這樣,就同時有2臺 small size(1core/1.75GB)的Cloud Service自動作負載均衡了。
7.在Settings欄,選擇Add Setting,設置Name爲StorageConnectionString,Type選擇Connection String,而後點擊Value欄的按鈕
在Create Storage Connection String中,選擇Account Name爲咱們在步驟4中建立的leiwcfstorage
8.再次點擊Add Setting,設置Name爲ContainerName,Type選擇String,Value設置爲photos。切記Value的值必須是小寫
9.在Project LeiWCFService中,修改IService1.cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace LeiWCFService { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/UploadPic", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string UploadPic(Stream ImageStream); } }
10.修改Service1.svc
這個類的主要功能是讀取Azure配置文件cscfg的節點信息,並根據storage connection string,建立Container,並將本地的圖片文件名重命名爲GUID,並保存至Container
項目中須要添加相關的引用,筆者就不詳述了。
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; namespace LeiWCFService { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 : IService1 { public string UploadPic(Stream ImageStream) { try { //獲取ServiceConfiguration.cscfg配置文件的信息 var account = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); var client = account.CreateCloudBlobClient(); //得到BlobContainer對象 CloudBlobContainer blobContainer = client.GetContainerReference(CloudConfigurationManager.GetSetting("ContainerName")); if (!blobContainer.Exists()) { // 檢查container是否被建立,若是沒有,建立container blobContainer.CreateIfNotExists(); var permissions = blobContainer.GetPermissions(); //對Storage的訪問權限是能夠瀏覽Container permissions.PublicAccess = BlobContainerPublicAccessType.Container; blobContainer.SetPermissions(permissions); } Guid guid = Guid.NewGuid(); CloudBlockBlob blob = blobContainer.GetBlockBlobReference(guid.ToString() + ".jpg"); blob.Properties.ContentType = "image/jpeg"; //request.Content.Headers.ContentType.MediaType //blob.UploadFromByteArray(content, 0, content.Length); blob.UploadFromStream(ImageStream); blob.SetProperties(); return guid.ToString(); } catch (Exception ex) { return ex.InnerException.ToString(); } } } }
以上代碼和配置,實現瞭如下功能點:
1.在Windows Azure Storage裏建立名爲photos的Container,而且設置Storage的訪問權限
2.設置上傳的BlockbName爲GUID
3.經過UploadFromStream,將客戶端POST的Stream保存到Azure Storage裏
11.修改Web.config的 <system.serviceModel>節點。設置WCF的相關配置。
<system.serviceModel> <bindings> <!--<webHttpBinding></webHttpBinding>--> <webHttpBinding> <binding name="WCFServiceBinding" maxReceivedMessageSize="10485760" maxBufferSize="10485760" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"> <security mode="None"/> </binding> </webHttpBinding> </bindings> <services> <service name="LeiWCFService.Service1" behaviorConfiguration="ServiceBehaviour"> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="LeiWCFService.IService1"></endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name="web"> <webHttp helpEnabled="true" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="ServiceBehaviour"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel>
12.在VS2013離,點擊Save All,並將項目發佈至Windows Azure。
DNS咱們設置爲LeiAzureService
13.等待項目發佈結束後,在IE瀏覽器中輸入http://leiazureservice.cloudapp.net/service1.svc/help,看到以下的圖片,就證實發布成功了