C# – Accessing the Response object from the WCF service

asp.netc++wcf

In my WCF service class which is in the same project as my WebSite I inherit from System.Web.UI.Page so that I have access to the Page.Response object but when I use it I get the error:
"Response is not available in this context."

I want to save to a file the results passes in to my WCF service like so:

            public void SendList(List<ListType> listTypes)
            {        
                Response.Clear(); 
                Response.ContentType = "text/plain";
                Response.AddHeader("content-disposition", "attachment; filename=filew.csv");
  //etc
            }

How can I do this if I can't access the Response object?

Best Solution

WCF should not be treated as simply http - i.e. it won't normally make sense to do what you have suggested above. You can do something comparable by writing a "message inspector" that adds a WCF header. A WCF service is not a Page, and can't be treated as one.

For the request, there is as as ASP.NET compatibility flag that can be enabled (providing access to HttpContext.Current), but the general advice is: don't.

So: what is it you are actually trying to do? What is your objective here?

Related Question