Can’t host a WCF Service

I have requirement that I want a WCF service library that provide a PDF via REST.
For example I have an url like this: localhost:8732/service1/reports/ok
And I get a PDF as response. It is a fixed file in local filesystem.
Here it is my current code:

Service.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 WcfJsonRestService
{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
    public class Service1 : IService1
    {

        public Stream GetReport(string value)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            FileStream f = new FileStream("C:\\invoice.pdf", FileMode.Open);
            int length = (int)f.Length;
            WebOperationContext.Current.OutgoingResponse.ContentLength = length;
            byte[] buffer = new byte[length];
            int sum = 0;
            int count;
            while ((count = f.Read(buffer, sum, length - sum)) > 0)
            {
                sum += count;
            }
            f.Close();
            return new MemoryStream(buffer); 
        }

    }
}

read more

Can’t host a WCF Service

I have requirement that I want a WCF service library that provide a PDF via REST.
For example I have an url like this: localhost:8732/service1/reports/ok
And I get a PDF as response. It is a fixed file in local filesystem.
Here it is my current code:

Service.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 WcfJsonRestService
{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
    public class Service1 : IService1
    {

        public Stream GetReport(string value)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            FileStream f = new FileStream("C:\\invoice.pdf", FileMode.Open);
            int length = (int)f.Length;
            WebOperationContext.Current.OutgoingResponse.ContentLength = length;
            byte[] buffer = new byte[length];
            int sum = 0;
            int count;
            while ((count = f.Read(buffer, sum, length - sum)) > 0)
            {
                sum += count;
            }
            f.Close();
            return new MemoryStream(buffer); 
        }

    }
}

read more