This is a mirror of official site: http://jasper-net.blogspot.com/

WCF Extensibility – WebHttpBehavior

| Wednesday, August 17, 2011
This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the index page.

In the last post about QueryStringConverter I mentioned that the way to replace the converter used by some operations in WCF WebHttp endpoints (they’re more widely known as WCF REST endpoints, although the purists may disagree with this name) is to create a class derived from WebHttpBehavior and override a virtual method from that base class. In this post I’ll go over the other virtual methods in that class, which makes extending WCF WebHttp endpoints (a little) easier than the general WCF endpoints.

Public implementations in WCF

The WebHttpBehavior class is concrete, so it can be used directly. There is one public subclass in WCF, WebScriptEnablingBehavior, which is the behavior used in endpoints compatible with the ASP.NET AJAX library--LINK. That behavior modifies the base with the following changes:

    Changes the DefaultBodyStyle property to WrappedRequest (and throws if one tries to set a different value)
    Changes the DefaultOutgoingRequestFormat and DefaultOutgoingResponseFormat to WebMessageFormat.Json (instead of Xml)
    Changes the properties AutomaticFormatSelectionEnabled, FaultExceptionEnabled and HelpEnabled to false (and throws if one tries to set them to true).
    Adds additional validation for features not supported by the ASP.NET AJAX library
    Changes the default QueryStringConverter to use the JsonQueryStringConverter, which allows the passing of complex types (encoded in JSON) in the query string for GET requests as well)
    Adds a new “metadata” endpoint which returns a JavaScript proxy which uses the ASP.NET AJAX library, and applications using that library can call the endpoints by using the "method” syntax (instead of having to handcraft the requests in the JavaScript code)
    Adds a new IErrorHandler in the server which changes exceptions in a JSON format understood by the ASP.NET AJAX library
    Adds a new IClientMessageInspector in the client, for the (not so common) case where this behavior is used at the client side so it will be able to understand the JSON format emitted by the error handler mentioned above

So that class changed a lot of the behavior. And the good thing is that all those changes were made via public virtual method, so it’s possible to recreate something similar with “normal” user code as well.
Class definition

    public class WebHttpBehavior : IEndpointBehavior
    {
        // Properties
        public virtual bool AutomaticFormatSelectionEnabled { get; set; }
        public virtual WebMessageBodyStyle DefaultBodyStyle { get; set; }
        public virtual WebMessageFormat DefaultOutgoingRequestFormat { get; set; }
        public virtual WebMessageFormat DefaultOutgoingResponseFormat { get; set; }
        public virtual bool FaultExceptionEnabled { get; set; }
        public virtual bool HelpEnabled { get; set; }
        protected internal string JavascriptCallbackParameterName { get; set; }
    
        // Methods
        public virtual void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters);
        protected virtual void AddClientErrorInspector(ServiceEndpoint endpoint, ClientRuntime clientRuntime);


Read more: Carlos' blog
QR: wcf-extensibility-webhttpbehavior.aspx

Posted via email from Jasper-net

0 comments: