using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ComponentModel.Composition;
using System.IO;
namespace WindowsPhone.Recipes.Push.Server.Services
{
///
/// Represents a tile image REST service.
///
[Export, PartCreationPolicy(CreationPolicy.Shared), ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class ImageService : IImageService
{
#region Constants
/// Url of the GetTileImage REST service.
public const string GetTileImageService = "http://localhost:8000/ImageService/GetTileImage?parameter={0}";
#endregion
#region Fields
private ServiceHost _serviceHost;
#endregion
#region Events
///
/// Raise when dynamic image creation is requested.
///
public event EventHandler ImageRequest;
#endregion
#region Operations
///
/// Host this service using WCF.
///
public void Host()
{
_serviceHost = new ServiceHost(this);
_serviceHost.Open();
}
///
/// Get a generated custom tile image stream for the given uri.
///
/// The tile image request parameter.
/// A stream of the custom tile image generated.
public Stream GetTileImage(string parameter)
{
if (ImageRequest != null)
{
var args = new ImageRequestEventArgs(parameter);
ImageRequest(this, args);
// Seek the stream back to the begining just in case.
args.ImageStream.Seek(0, SeekOrigin.Begin);
return args.ImageStream;
}
return Stream.Null;
}
#endregion
#region Privates Logic
private void OnImageRequest(ImageRequestEventArgs args)
{
if (ImageRequest != null)
{
ImageRequest(this, args);
}
}
#endregion
}
}