using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Activation;
using System.Runtime.Serialization;
using Telerik.Reporting.Service;
using System.Web;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Web;
using Telerik.Reporting;
using Telerik.Reporting.Examples.CSharp;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new System.ServiceModel.ServiceHost(typeof(ReportService));
            host.Open();

            // Block the Main() method until the user presses a key; then close the host and exit the program
            Console.WriteLine("Ready...");
            Console.ReadLine();

            host.Close();
        }
    }
    
    [ServiceContract]
    public interface IClientAccessPolicy
    {
        [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetPolicy();
    }

    [KnownType(typeof(object[]))]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ReportService : ReportServiceBase, IClientAccessPolicy
    {
        static readonly Uri baseUri = new Uri("http://localhost:1817/reportservice");

        protected override Uri BaseAddress
        {
            get { return baseUri; }
        }

        protected override string ApplicationPath
        {
            get
            {
                return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\";
            }
        }

        public Stream GetPolicy()        
        {
            var fs = new FileStream(Path.Combine(this.ApplicationPath, "clientaccesspolicy.xml"), FileMode.Open);
            var bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            
            if (WebOperationContext.Current != null)
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
            }

            return new MemoryStream(bytes);
        }


    }

   
}