This is one more advanced WCF service than other samples. Because it’s meant to be used by Windows Phone and Windows 8 clients.
The main idea behind this service is that it can be used by anyone easily by supporting their own Windows Azure Storage account information. This way you can easily store and retrieve your images without creating any Windows Azure back-end. You just need to create a own Windows Azure Storage account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace WcfImageBlobSample | |
{ | |
using System.ServiceModel; | |
[ServiceContract] | |
public interface IService1 | |
{ | |
[OperationContract] | |
bool SavePicture(byte[] picture, string pictureName, string folderName, string storageConnectionString); | |
[OperationContract] | |
byte[] RetrievePicture(string pictureName, string folderName, string storageConnectionString); | |
[OperationContract] | |
string FormStorageConnectionString(string accountName, string accountKey); | |
[OperationContract] | |
Dictionary<string, byte[]>RetrievePictures(List<string> pictureNames, string folderName, string storageConnectionString ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
namespace WcfImageBlobSample | |
{ | |
using System.IO; | |
using Microsoft.WindowsAzure; | |
using Microsoft.WindowsAzure.StorageClient; | |
public class Service1 : IService1 | |
{ | |
public bool SavePicture(byte[] picture, string pictureName, | |
string folderName, string storageConnectionString) | |
{ | |
var storageAccount = | |
CloudStorageAccount.Parse(storageConnectionString); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
// Retrieve a reference to a container | |
var container = | |
blobClient.GetContainerReference(folderName); | |
container.CreateIfNotExist(); | |
// Retrieve reference to a blob | |
var blob = container.GetBlobReference(pictureName); | |
// Create or overwrite the blob with content | |
var ms = new MemoryStream(picture); | |
blob.UploadFromStream(ms); | |
return true; | |
} | |
public byte[] RetrievePicture(string pictureName, string folderName, | |
string storageConnectionString) | |
{ | |
var storageAccount = | |
CloudStorageAccount.Parse(storageConnectionString); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
// Retrieve a reference to a container | |
var container = | |
blobClient.GetContainerReference(folderName); | |
container.CreateIfNotExist(); | |
// Retrieve reference to a blob | |
var blob = container.GetBlobReference(pictureName); | |
return ReadFully(blob.OpenRead()); | |
} | |
private static byte[] ReadFully(Stream input) | |
{ | |
var buffer = new byte[16 * 1024]; | |
using (var ms = new MemoryStream()) | |
{ | |
int read; | |
while ((read = input.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
ms.Write(buffer, 0, read); | |
} | |
return ms.ToArray(); | |
} | |
} | |
public string FormStorageConnectionString(string accountName, | |
string accountKey) | |
{ | |
return | |
"DefaultEndpointsProtocol=https;" + | |
"AccountName=" + accountName + | |
";AccountKey=" + accountKey; | |
} | |
public Dictionary<string, byte[]> RetrievePictures(List<string> pictureNames, string folderName, string storageConnectionString) | |
{ | |
var storageAccount = | |
CloudStorageAccount.Parse(storageConnectionString); | |
var blobClient = storageAccount.CreateCloudBlobClient(); | |
// Retrieve a reference to a container | |
var container = | |
blobClient.GetContainerReference(folderName); | |
container.CreateIfNotExist(); | |
var result = new Dictionary<string, byte[]>(); | |
//Go thru all of the values in list | |
foreach (var pictureName in pictureNames) | |
{ | |
// Retrieve reference to a blob | |
var blob = container.GetBlobReference(pictureName); | |
// ADD result to dictionary | |
result.Add(pictureName, ReadFully(blob.OpenRead())); | |
} | |
//return the dictionary | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<configuration> | |
<system.web> | |
<compilation debug="true" targetFramework="4.0" /> | |
<httpRuntime maxRequestLength="2097151000" executionTimeout="3600"/> | |
</system.web> | |
<system.serviceModel> | |
<bindings> | |
<basicHttpBinding> | |
<binding maxReceivedMessageSize="2147483647"> | |
<readerQuotas | |
maxDepth="64" | |
maxStringContentLength="2147483647" | |
maxArrayLength="2147483647" | |
maxBytesPerRead="4096" | |
maxNameTableCharCount="16384"/> | |
</binding> | |
</basicHttpBinding> | |
</bindings> | |
<behaviors> | |
<serviceBehaviors> | |
<behavior> | |
<!-- To avoid disclosing metadata information, set the value below to false before deployment --> | |
<serviceMetadata httpGetEnabled="true" /> | |
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> | |
<serviceDebug includeExceptionDetailInFaults="true" /> | |
<dataContractSerializer maxItemsInObjectGraph="1048576" /> | |
</behavior> | |
</serviceBehaviors> | |
</behaviors> | |
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> | |
</system.serviceModel> | |
<system.webServer> | |
<modules runAllManagedModulesForAllRequests="true" /> | |
<!-- | |
To browse web app root directory during debugging, set the value below to true. | |
Set to false before deployment to avoid disclosing web app folder information. | |
--> | |
<directoryBrowse enabled="true" /> | |
</system.webServer> | |
</configuration> |
If you just want to use the service you can find it hosted here.