开发者

Creating a custom .svc handler for WCF service hosted in IIS

开发者 https://www.devze.com 2023-04-06 10:57 出处:网络
I am hosting a wcf service in IIS 7. I was curious if it would be possible to create my own .svc HttpHandler mapping and class to handle service requests.

I am hosting a wcf service in IIS 7. I was curious if it would be possible to create my own .svc HttpHandler mapping and class to handle service requests.

For example, if I was to intercept any requests to files that ended with an extension of ".foo" I could add this to my web.config

<handlers>
     <add name="*.foo_**" path="*.foo" verb="*" type="MyServer.FooHttpHandler" />      
</handlers>

And I could have a class in my default root that did the following

public class FooHandler: IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // do stuff, validate?

        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("filename={0}", url));
        HttpContext.Current.Response.AddHeader("Content-Type", "fooMimeType");
        HttpContext.Current.Response.WriteFile(url);
        HttpContext.Current.Response.End();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

Is it possible to do something like this with wcf .svc requests? I'm not sure if it'd be the exact same thing or not, since I'm not necessary serving a file to respond with, I want to intercept and proxy the response开发者_运维问答.

Or is a better way to implement a service behavior that does my required pre-service logic?


What are you trying to achieve? Not sure if you can replace the existing *.svc http handler - but what you can do much more easily is create your own custom ServiceHostFactory for the WCF service. You basically add one attribute your *.svc file:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="YourNamespace.YourService" 
    Factory="YourNamespace2.YourServiceHostFactory" %>

Using this, IIS will now instantiate your own YourServiceHostFactory and ask you to create an instance of the YourService class. Maybe you can hook into the flow here and do what you need to do?


You could use ASP.NET Routing and an IRouteHandler that returns your own IHttpHandler implementation as well if you really want to as well
http://msdn.microsoft.com/en-us/library/system.web.routing.iroutehandler.gethttphandler.aspx http://msdn.microsoft.com/en-us/library/cc668201.aspx

I've done this in the past when setting up WCF Data Services and I don't see why it can't work here as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消