Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I serve a CSS file from a WCF Service?
    text
    copied!<p>I'm working on a plug-in architecture for "widgets" on a website. I would like each individual widget to store its Javascript and CSS as resources in the assembly. When the page is rendered, I inject script tags that reference a WCF service that can inspect the widget, extract the desired file, and send it back to the browser.</p> <p>This is working fine for the Javascript files, but the browser can't handle the CSS file coming through as an octet stream. Normally, you can just change the content type of the response in HttpContext, but that's not available in a WCF service.</p> <p>What I need is to have the browser retrieve these files through a url like so:</p> <pre><code>&lt;link type="text/css" rel="stylesheet" href="/Controllers/WidgetFileService.svc/style/WidgetId"&gt; </code></pre> <p>I'd prefer not to implement a custom IHttpHandler, because, in my experience, those can be difficult to deploy, debug, and get working on different machines. This is the my first project at this company and I'd rather not complicate deployment and testing.</p> <p>Here is what I have for the interface:</p> <pre><code>[ServiceContract] public interface IWidgetFileService { [OperationContract] [WebGet(UriTemplate = "style/{widgetID}", BodyStyle = WebMessageBodyStyle.Bare)] System.IO.Stream GetStyleFile(String widgetID); } </code></pre> <p>And the implementation:</p> <pre><code>static private System.IO.Stream StreamBytes(byte[] data) { System.IO.Stream s = new System.IO.MemoryStream(data); s.Position = 0; return s; } public System.IO.Stream GetStyleFile(String widgetID) { IWidget w = GetWidget(widgetID); return StreamBytes(w.GetEditorStyleFile()); } </code></pre> <p>If I browse to the URL, the data comes back as expected. It seems to be the way the browser is handling the request that is causing the issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload