Windows Azure Platform 系列文章目录

  这几天工作上的内容,把项目文件和源代码拿出来给大家分享下。

  源代码下载:Part1    Part2    Part3

  我们在写WEB服务的时候,经常需要把本地的文件上传至服务器端进行保存,类似于上传附件的功能。

  在本章中,我们将新建WCF服务并上传至Azure云端,实现把本地的图片通过WCF保存至Azure Storage。

  本章需要掌握的技术点:

  1.WCF服务

  2.Azure Storage

  本章将首先介绍服务器端程序代码。

  1.首先我们用管理员身份,运行VS2013。

  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看到如下的图片,就证明发布成功了

  

  

[SDK2.2]Windows Azure Storage (15) 使用WCF服务,将本地图片上传至Azure Storage (上) 服务器端代码的更多相关文章

  1. [SDK2.2]Windows Azure Storage (16) 使用WCF服务,将本地图片上传至Azure Storage (上) 客户端代码

    <Windows Azure Platform 系列文章目录> 前一章我们完成了服务器端的代码,并且已经发布到了Windows Azure云端. 本章我们将实现客户端的代码,客户端这里我们 ...

  2. [New Portal]Windows Azure Virtual Machine (15) 在本地制作数据文件VHD并上传至Azure(2)

    <Windows Azure Platform 系列文章目录> 在上一章内容里,我们已经将包含有OFFICE2013 ISO安装文件的VHD上传至Azure Blob Storage中了. ...

  3. [New Portal]Windows Azure Virtual Machine (11) 在本地使用Hyper-V制作虚拟机模板,并上传至Azure (1)

    <Windows Azure Platform 系列文章目录> 本章介绍的内容是将本地Hyper-V的VHD,上传到Azure数据中心,作为自定义的虚拟机模板. 注意:因为在制作VHD的最 ...

  4. [New Portal]Windows Azure Virtual Machine (12) 在本地使用Hyper-V制作虚拟机模板,并上传至Azure (2)

    <Windows Azure Platform 系列文章目录> 本章介绍的内容是将本地Hyper-V的VHD,上传到Azure数据中心,作为自定义的虚拟机模板. 注意:因为在制作VHD的最 ...

  5. [New Portal]Windows Azure Virtual Machine (13) 在本地使用Hyper-V制作虚拟机模板,并上传至Azure (3)

    <Windows Azure Platform 系列文章目录> 本章介绍的内容是将本地Hyper-V的VHD,上传到Azure数据中心,作为自定义的虚拟机模板. 注意:因为在制作VHD的最 ...

  6. [New Portal]Windows Azure Virtual Machine (14) 在本地制作数据文件VHD并上传至Azure(1)

    <Windows Azure Platform 系列文章目录> 之前的内容里,我介绍了如何将本地的Server 2012中文版 VHD上传至Windows Azure,并创建基于该Serv ...

  7. Azure开发者任务之七:在Azure托管服务中托管WCF服务角色

    在一个托管服务中托管一个WCF服务角色和托管一个ASP.Net Web Role基本类似. 在上一篇文章中,我们学习了如何使用WCF Service Web Role. 在本文中,我会对上一篇文章进行 ...

  8. 准备好要上传到 Azure 的 Windows VHD 或 VHDX

    在将 Windows 虚拟机 (VM) 从本地上传到 Azure 之前,必须准备好虚拟硬盘(VHD 或 VHDX). Azure 仅支持采用 VHD 文件格式且具有固定大小磁盘的第 1 代 VM. V ...

  9. 超大文件上传到Azure Linux虚拟机最佳实践

    客户在实际进行迁移的时候,往往碰到需要将本地数据中心的超大文件,比如单个200GB的文件,或者总共1TB的无数文件上传到Azure上的情况,尤其是传到Azure的Linux虚拟机的场景,这种场景包括: ...

随机推荐

  1. android开发读书笔记

    第九章心得: HAL ( Hardware Abstraction Layer,硬件抽象腔,〉是建立在Linux驱动之上的一套翻字库.这套程序 j率并不属于 Linux 内核, 而是属于 Linux ...

  2. weibform中Application、ViewState对象和分页

    Application: 全局公共变量组 存放位置:服务器 特点:所有访问用户都是访问同一个变量,但只要服务器不停机,变量一直存在于服务器的内存中,不要使用循环大量的创建Application对象,可 ...

  3. linux 常用命令

    //创建目录mkdir//创建中间没有路径的文件夹mkdir -p //删除文件rm//强制删除文件rm -f//删除目录rmdir//删除多个目录rmdir -p //输出当前环境根路径echo $ ...

  4. linux 负载均衡

    [博文推荐]关于负载均衡技术使用的一些误区 如今,负载均衡已经不是一个新鲜的词,也不是什么新技术,主要用于解决单机负载能力的局限性,但问题是你的应用真的到了单 机的负载上限了吗,未必,很多不知道如何推 ...

  5. 20145223《信息安全系统设计基础》 GDB调试汇编堆栈过程分析

    20145223<信息安全系统设计基础> GDB调试汇编堆栈过程分析 分析的c语言源码 生成汇编代码--命令:gcc -g example.c -o example -m32 进入gdb调 ...

  6. android studio乱码

    http://www.cnblogs.com/Kennytian/p/4449878.html Android Studio中的乱码分好几种,一是IDE的不同窗口里显示乱码,如:logcat筛选框,S ...

  7. bzoj 3202: [Sdoi2013]项链

    Description 项链是人体的装饰品之一,是最早出现的首饰.项链除了具有装饰功能之外,有些项 链还具有特殊显示作用,如天主教徒的十字架链和佛教徒的念珠. 从古至今人们为了美化人体本身,也美 化环 ...

  8. jQuery中prop() , attr() ,css() 的区别

    1.  HTML属性是指页面标记中放在引号中的值,而DOM属性则是指通过JavaScript能够存取的值. (1)在jQuery中,prop()是操作DOM属性,attr()是操作HTML属性. HT ...

  9. WINFORM 输出txt文件

    SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.Filter = "文本文件(.txt)|*.txt"; sa ...

  10. 自定义 TableViewCell 的分割线

    刚开始自定义 tableViewCell 的时候,用的是直接在 cell 上加一张 imageView 的方法,如果在点击 cell 的时候有页面的跳转,这样做没什么问题,但是,如果在点击 cell ...