Adding a PDF formatter to WCF REST

Topics: Web Api
May 14, 2011 at 2:54 PM

Apologies - I know this type of question has been asked many times, but I'm still not able to get my stuff to work.

Having defined a new formatter to return PDF so a client could specify Accept = application/pdf before accessing a resource, I want to do something (in glabal.asax.cs I assume) as simple as :

    HttpHostConfiguration.Create().AddFormatters(new PdfFormatter());

I don't want to write reams of code involving MEF, containers, logging channels etc. - I just want a simple way to include the new formatter along side Json and Xml which seem to work out of the box for me.

I have seen other posts that refer to HttpHostConfiguration.Build().AddFormatters but cant seem to find that in preview 4.

Can anyone help, please. I am going slightly nuts over this.

Thanks. Simon.

May 14, 2011 at 3:10 PM

What you are suggesting should work.  Here is a sample that works for me and is equivalent,

 	    var serviceUri = new Uri("http://localhost:1001/");
            var config = HttpHostConfiguration.Create()
                .AddFormatters(new PlainTextFormatter());
            
            var host = new HttpConfigurableServiceHost<FooService>(config, new[] {serviceUri});
            host.Open();

            var client = new HttpClient(serviceUri);

            //Act
            var response = client.Get("foo");