views:

85

answers:

0

I have a webHttp file upload/download service. The file name might have non-ascii characters. First, I tried our service on IIS. Upload/Download worked well for small files. To handle large files I needed wcf streaming which is no supported on IIS. So I moved to console host. Now there was a surprise. IIS sends HTTP-headers as they are in unicode, but WebServiceHost seems to convert them to ASCII. So file names with non-ASCII chars get corrupted. I didn't find any configuration option to solve this issue. Any ideas? Here is the code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace HttpHeaderTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new WebServiceHost(typeof (DownloadService), new Uri("http://localhost:8001/"));
            host.Open();

            Console.WriteLine("Ok");
            Console.Read();

            host.Close();
        }
    }

    [ServiceContract]
    public interface IDownLoadService
    {
        [OperationContract]
        [WebGet(UriTemplate = "/")]
        Stream Download();        
    }

    public class DownloadService : IDownLoadService
    {
        public Stream Download()
        {
            var fileName = "тест тест.txt";
            var resp = WebOperationContext.Current.OutgoingResponse;
            resp.ContentType = "application/octet-stream;";
            resp.Headers.Add(HttpResponseHeader.ContentEncoding, "utf-8");
            resp.Headers.Add("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));            
            return new MemoryStream(Encoding.UTF8.GetBytes("test"));
        }
    }
}

Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="webHttp">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="service" />
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <webHttpBinding>
                <binding name="webHttp" writeEncoding="utf-8" transferMode="Buffered">
                    <security>
                        <transport>
                            <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                    </security>
                </binding>
            </webHttpBinding>
        </bindings>
        <services>
            <service name="HttpHeaderTest.DownloadService">
                <endpoint address="" behaviorConfiguration="webHttp" binding="webHttpBinding"
                    bindingConfiguration="webHttp" name="web" contract="HttpHeaderTest.IDownLoadService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>