Feedback on json with padding implementation  <p>Hi all,</p> <p>I'm fairly new to WCF and this is my first post so please be gentle.  One of the attractive features of WCF to me is the ability to define different endpoints on the same service that return different encodings.  The ability to offer my customers a SOAP, POX, and JSON endpoint all with the same code is very cool.  In my particular case being able to offer JSONP would also be very useful as a lot of my customers want to be able to use javascript to call my services from different domains.  I tried to use the example described here:</p> <p><a href="http://msdn.microsoft.com/en-us/library/cc716898.aspx">http://msdn.microsoft.com/en-us/library/cc716898.aspx</a></p> <p>but that example is not available for whatever reason.  Since the description for the example does pretty much outline the approach used to return jsonp I decided to take a whack at my own implementation based on the description and other examples provided.  However, since I am very new to WCF I'm sure there is plenty of things that can be improved and I was hoping to get some feedback on what I've done and get suggestions for improvement.</p> <p>The basic approach was to write a custom MessageEncoder that would wrap the built-in JSON encoder with a javascript function name based on a passed in querystring parameter.</p> <p>The first thing we need is a MessageEncodingBindingElement and BindingElementExtensionElement so that we can define our own binding in the config file.  This code was based heavily on the GZip encoding example.  The MessageEncodingBindingElement wraps a WebMessageEncodingBindingElement which will be used to create the factory we need to get the built-in JSON encoder.<br><br></p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Xml;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Channels;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Description;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td></font><font style="color:blue">namespace</font><font style="font-size:11px"> JsonpEncoder {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">class</font><font style="font-size:11px"> JsonpMessageEncodingElement : BindingElementExtensionElement {  </font></td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> JsonpMessageEncodingElement() {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> Type BindingElementType {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">typeof</font><font style="font-size:11px">(JsonpMessageEncodingElement); }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> ApplyConfiguration(BindingElement bindingElement) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">base</font><font style="font-size:11px">.ApplyConfiguration(bindingElement);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            JsonpMessageEncodingBindingElement binding = (JsonpMessageEncodingBindingElement)bindingElement;  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">protected</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> BindingElement CreateBindingElement() {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            JsonpMessageEncodingBindingElement bindingElement = </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement();  </font></td></tr> <tr> <td>            </font><font style="color:blue">this</font><font style="font-size:11px">.ApplyConfiguration(bindingElement);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">return</font><font style="font-size:11px"> bindingElement;  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Xml;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Channels;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Description;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Text;  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">namespace</font><font style="font-size:11px"> JsonpEncoder {  </font></td></tr> <tr> <td>    </font><font style="color:blue">class</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement : MessageEncodingBindingElement {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">private</font><font style="font-size:11px"> WebMessageEncodingBindingElement _innerBindingElement;  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">internal</font><font style="font-size:11px"> WebMessageEncodingBindingElement InnerBindingElement {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerBindingElement; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">set</font><font style="font-size:11px"> { _innerBindingElement = value; }  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement()  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            : </font><font style="color:blue">this</font><font style="font-size:11px">(</font><font style="color:blue">new</font><font style="font-size:11px"> WebMessageEncodingBindingElement()) {  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        JsonpMessageEncodingBindingElement(WebMessageEncodingBindingElement bindingElement) {  </td></tr> <tr> <td style="background-color:#f7f7f7">            _innerBindingElement = bindingElement;  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> MessageVersion MessageVersion {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerBindingElement.MessageVersion; }  </font></td></tr> <tr> <td>            </font><font style="color:blue">set</font><font style="font-size:11px"> { _innerBindingElement.MessageVersion = value; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> IChannelFactory&lt;TChannel&gt; BuildChannelFactory&lt;TChannel&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (context == </font><font style="color:blue">null</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArgumentNullException(</font><font style="color:blue">&quot;context&quot;</font><font style="font-size:11px">);  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            context.BindingParameters.Add(</font><font style="color:blue">this</font><font style="font-size:11px">);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> context.BuildInnerChannelFactory&lt;TChannel&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> IChannelListener&lt;TChannel&gt; BuildChannelListener&lt;TChannel&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (context == </font><font style="color:blue">null</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArgumentNullException(</font><font style="color:blue">&quot;context&quot;</font><font style="font-size:11px">);  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            context.BindingParameters.Add(</font><font style="color:blue">this</font><font style="font-size:11px">);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> context.BuildInnerChannelListener&lt;TChannel&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> CanBuildChannelListener&lt;TChannel&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (context == </font><font style="color:blue">null</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArgumentNullException(</font><font style="color:blue">&quot;context&quot;</font><font style="font-size:11px">);  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            context.BindingParameters.Add(</font><font style="color:blue">this</font><font style="font-size:11px">);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> context.CanBuildInnerChannelListener&lt;TChannel&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> MessageEncoderFactory CreateMessageEncoderFactory() {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpMessageEncoderFactory(</font><font style="color:blue">this</font><font style="font-size:11px">, _innerBindingElement.CreateMessageEncoderFactory());  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> BindingElement Clone() {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement((WebMessageEncodingBindingElement)</font><font style="color:blue">this</font><font style="font-size:11px">.InnerBindingElement.Clone());  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> T GetProperty&lt;T&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> _innerBindingElement.GetProperty&lt;T&gt;(context) ?? context.GetInnerProperty&lt;T&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br>Next we need a custom MessageEncoderFactory to produce our jsonp encoder.  It's pretty straightfoward, it also wraps a MessageEncoderFactory created by the WebMessageEncodingBindingElement in the above code.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">using System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">using System.ServiceModel.Channels;  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">namespace JsonpEncoder {  </td></tr> <tr> <td>    class JsonpMessageEncoderFactory : MessageEncoderFactory {  </td></tr> <tr> <td style="background-color:#f7f7f7">        private JsonpEncoder _encoder;  </td></tr> <tr> <td>        private MessageEncoderFactory _innerFactory;  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        public JsonpMessageEncoderFactory(JsonpMessageEncodingBindingElement bindingElement, MessageEncoderFactory innerFactory) {  </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:red">_innerFactory</font><font style="font-size:11px"> = innerFactory;  </font></td></tr> <tr> <td>            </font><font style="color:red">_encoder</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpEncoder(bindingElement,_innerFactory.Encoder);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        public override MessageEncoder Encoder {  </td></tr> <tr> <td>            get { return _encoder; }  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        public override MessageVersion MessageVersion {  </td></tr> <tr> <td>            get { return _innerFactory.MessageVersion; }  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr></tbody></table></div><br>Now we need the actual encoder to do the work.  Here again we are wrapping an inner encoder provided by the wrapped factory.  For everything but the WriteMessage method we are just calling the methods on the wrapped encoder.  The WriteMessage method has pretty much all the interesting code in it as it actually sticks the call to the javascript function based on the querystring parameter named callback.  It defaults to something if that value isn't present.  Since we are referencing the HttpContext that means we'll need to set AspNetCompatibilityRequirements to Allowed.  This was one of the things in particular I was hoping someone had a better way to do.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.IO;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Channels;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.Text;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Web;  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">namespace</font><font style="font-size:11px"> JsonpEncoder {  </font></td></tr> <tr> <td>    </font><font style="color:blue">class</font><font style="font-size:11px"> JsonpEncoder : MessageEncoder {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">private</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement _bindingElement;  </font></td></tr> <tr> <td>        </font><font style="color:blue">private</font><font style="font-size:11px"> MessageEncoder _innerEncoder;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> JsonpEncoder(JsonpMessageEncodingBindingElement bindingElement, MessageEncoder innerEncoder) {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            _bindingElement = bindingElement;  </td></tr> <tr> <td>            _innerEncoder = innerEncoder;  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> ContentType {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.ContentType; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> MediaType {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.MediaType; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> MessageVersion MessageVersion {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.MessageVersion; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> IsContentTypeSupported(</font><font style="color:blue">string</font><font style="font-size:11px"> contentType) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (contentType.StartsWith(</font><font style="color:blue">&quot;application/json&quot;</font><font style="font-size:11px">)) {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">true</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            } </font><font style="color:blue">else</font><font style="font-size:11px"> {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">false</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            }  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> Message ReadMessage(ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; buffer, BufferManager bufferManager, </font><font style="color:blue">string</font><font style="font-size:11px"> contentType) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.ReadMessage(buffer, bufferManager, contentType);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> Message ReadMessage(Stream stream, </font><font style="color:blue">int</font><font style="font-size:11px"> maxSizeOfHeaders, </font><font style="color:blue">string</font><font style="font-size:11px"> contentType) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; WriteMessage(Message message, </font><font style="color:blue">int</font><font style="font-size:11px"> maxMessageSize, BufferManager bufferManager, </font><font style="color:blue">int</font><font style="font-size:11px"> messageOffset) {  </font></td></tr> <tr> <td>            ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; arraySegment = _innerEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">string</font><font style="font-size:11px"> messageString = Encoding.UTF8.GetString(arraySegment.Array, 0, arraySegment.Count);  </font></td></tr> <tr> <td>            bufferManager.ReturnBuffer(arraySegment.Array);  </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">byte</font><font style="font-size:11px">[] buffer = bufferManager.TakeBuffer(arraySegment.Count);  </font></td></tr> <tr> <td>            </font><font style="color:blue">string</font><font style="font-size:11px"> callbackFunction;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">if</font><font style="font-size:11px"> (HttpContext.Current.Request.QueryString[</font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">] != </font><font style="color:blue">&quot;&quot;</font><font style="font-size:11px">) {  </font></td></tr> <tr> <td>                callbackFunction = HttpContext.Current.Request.QueryString[</font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">];  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            } </font><font style="color:blue">else</font><font style="font-size:11px"> {  </font></td></tr> <tr> <td>                callbackFunction = </font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            messageString = callbackFunction + </font><font style="color:blue">&quot;('&quot;</font><font style="font-size:11px"> + messageString.Replace(</font><font style="color:blue">&quot;'&quot;</font><font style="font-size:11px">, </font><font style="color:blue">&quot;\\'&quot;</font><font style="font-size:11px">) + </font><font style="color:blue">&quot;');&quot;</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            </font><font style="color:blue">int</font><font style="font-size:11px"> count = Encoding.UTF8.GetBytes(messageString, 0, messageString.Length, buffer, 0);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt;(buffer, 0, count);  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> WriteMessage(Message message, Stream stream) {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            _innerEncoder.WriteMessage(message, stream);  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr></tbody></table></div><br>Now we need a service to use the new encoder.  I just made up something very basic for demonstration purposes, but note the setting of AspNetCompatibilityRequirements.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Activation;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Web;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.Runtime.Serialization;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>[ServiceContract()]  </td></tr> <tr> <td style="background-color:#f7f7f7">[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  </td></tr> <tr> <td></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> TestService  {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    [OperationContract]  </td></tr> <tr> <td>    [WebGet]  </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Operation1(</font><font style="color:blue">string</font><font style="font-size:11px"> val) {  </font></td></tr> <tr> <td>        </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">&quot;Hello: &quot;</font><font style="font-size:11px"> + val;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">    [OperationContract]  </td></tr> <tr> <td>    [WebGet]  </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">public</font><font style="font-size:11px"> TestContract Operation2(</font><font style="color:blue">string</font><font style="font-size:11px"> field1, </font><font style="color:blue">string</font><font style="font-size:11px"> field2) {  </font></td></tr> <tr> <td>        TestContract ret = </font><font style="color:blue">new</font><font style="font-size:11px"> TestContract();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        ret.Field1 = field1;  </td></tr> <tr> <td>        ret.Field2 = field2;  </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">return</font><font style="font-size:11px"> ret;  </font></td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">[DataContract]  </td></tr> <tr> <td></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> TestContract {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">string</font><font style="font-size:11px"> field1;  </font></td></tr> <tr> <td>    </font><font style="color:blue">string</font><font style="font-size:11px"> field2;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>    [DataMember]  </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Field1 {  </font></td></tr> <tr> <td>        </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> field1; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">set</font><font style="font-size:11px"> { field1 = value; }  </font></td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">    [DataMember]  </td></tr> <tr> <td>    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Field2 {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> field2; }  </font></td></tr> <tr> <td>        </font><font style="color:blue">set</font><font style="font-size:11px"> { field2 = value; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td>} </td></tr></tbody></table></div><br>Now we'll set up the config file to define different endpoints to the service.  Well setup one endpoint for jsonp using our custom binding and httptransport.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">    </font><font style="color:blue">&lt;</font><font style="font-size:11px">system.serviceModel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">&lt;</font><font style="font-size:11px">extensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;</font><font style="font-size:11px">bindingElementExtensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">add </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonpEncoding&quot;</font><font style="font-size:11px"> </font><font style="color:red">type</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JsonpEncoder.JsonpMessageEncodingElement, JsonpEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">bindingElementExtensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">extensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">&lt;</font><font style="font-size:11px">services</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;</font><font style="font-size:11px">service </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultServiceBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;webHttpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultEndpointBehavior&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestServiceJSON&quot;</font><font style="font-size:11px"> </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;json&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;webHttpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONEndpointBehavior&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestServiceJSONP&quot;</font><font style="font-size:11px"> </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonp&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;customBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">bindingConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONPEndpointBehavior&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;mex&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;IMetadataExchange&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;mexHttpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">service</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">services</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;</font><font style="font-size:11px">bindings</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;</font><font style="font-size:11px">customBinding</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">binding </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonpBinding&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">jsonpEncoding </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">httpTransport </font><font style="color:red">manualAddressing</font><font style="font-size:11px">=</font><font style="color:blue">&quot;true&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">binding</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">customBinding</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">bindings</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceHostingEnvironment </font><font style="color:red">aspNetCompatibilityEnabled</font><font style="font-size:11px">=</font><font style="color:blue">&quot;true&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">serviceHostingEnvironment</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;</font><font style="font-size:11px">behaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultServiceBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceDebug </font><font style="color:red">includeExceptionDetailInFaults</font><font style="font-size:11px">=</font><font style="color:blue">&quot;true&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceMetadata </font><font style="color:red">httpGetEnabled</font><font style="font-size:11px">=</font><font style="color:blue">&quot;True&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">serviceBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpointBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultEndpointBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">webHttp </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONEndpointBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">enableWebScript </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONPEndpointBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">enableWebScript </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">endpointBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>    </font><font style="color:blue">&lt;/</font><font style="font-size:11px">system.serviceModel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br>Finally we need an html page to call the service.  Again, something very very basic for demonstration.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">&lt;</font><font style="font-size:11px">html </font><font style="color:red">xmlns</font><font style="font-size:11px">=</font><font style="color:blue">&quot;http://www.w3.org/1999/xhtml&quot;</font><font style="font-size:11px"> </font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td></font><font style="color:blue">&lt;</font><font style="font-size:11px">head</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">&lt;</font><font style="font-size:11px">title</font><font style="color:blue">&gt;</font><font style="font-size:11px">Jsonp Encoder Test</font><font style="color:blue">&lt;/</font><font style="font-size:11px">title</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>      </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">&lt;</font><font style="font-size:11px">script </font><font style="color:red">type</font><font style="font-size:11px">=</font><font style="color:blue">&quot;text/javascript&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>        function callbackFunction(jsonstr) {  </td></tr> <tr> <td style="background-color:#f7f7f7">           var </font><font style="color:red">obj</font><font style="font-size:11px"> = </font><font style="color:blue">eval</font><font style="font-size:11px">('(' + jsonstr + ')');  </font></td></tr> <tr> <td>           </font><font style="color:red">obj</font><font style="font-size:11px"></font><font style="color:blue">obj</font><font style="font-size:11px"> = obj.d;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">           alert(&quot;</font><font style="color:red">Field1</font><font style="font-size:11px"> = &quot; + obj.Field1);  </font></td></tr> <tr> <td>           alert(&quot;</font><font style="color:red">Field2</font><font style="font-size:11px"> = &quot; + obj.Field2);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td>    </font><font style="color:blue">&lt;/</font><font style="font-size:11px">script</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">      </td></tr> <tr> <td>    </font><font style="color:blue">&lt;</font><font style="font-size:11px">script </font><font style="color:red">src</font><font style="font-size:11px">=</font><font style="color:blue">&quot;testservice.svc/jsonp/Operation2?field1=test1&amp;field2=test2&amp;callback=callbackFunction&quot;</font><font style="font-size:11px">&gt;</font><font style="color:blue">&lt;/</font><font style="font-size:11px">script</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">      </td></tr> <tr> <td></font><font style="color:blue">&lt;/</font><font style="font-size:11px">head</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">&lt;</font><font style="font-size:11px">body</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">&lt;/</font><font style="font-size:11px">body</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td></font><font style="color:blue">&lt;/</font><font style="font-size:11px">html</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br> <p>So, there it is.  I'd love to hear suggestions on better ways to do things.  Going through this process taught me quite a bit and I know I have a lot more to go.</p> <p>Eric</p><br><hr size="1" align="left" width="25%">Eric© 2009 Microsoft Corporation. All rights reserved.Thu, 15 Jan 2009 13:31:21 Zeb559c61-d512-4692-aaa1-2c3656c78e49http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#eb559c61-d512-4692-aaa1-2c3656c78e49http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#eb559c61-d512-4692-aaa1-2c3656c78e49ecairnshttp://social.msdn.microsoft.com/Profile/en-US/?user=ecairnsFeedback on json with padding implementation  <p>Hi all,</p> <p>I'm fairly new to WCF and this is my first post so please be gentle.  One of the attractive features of WCF to me is the ability to define different endpoints on the same service that return different encodings.  The ability to offer my customers a SOAP, POX, and JSON endpoint all with the same code is very cool.  In my particular case being able to offer JSONP would also be very useful as a lot of my customers want to be able to use javascript to call my services from different domains.  I tried to use the example described here:</p> <p><a href="http://msdn.microsoft.com/en-us/library/cc716898.aspx">http://msdn.microsoft.com/en-us/library/cc716898.aspx</a></p> <p>but that example is not available for whatever reason.  Since the description for the example does pretty much outline the approach used to return jsonp I decided to take a whack at my own implementation based on the description and other examples provided.  However, since I am very new to WCF I'm sure there is plenty of things that can be improved and I was hoping to get some feedback on what I've done and get suggestions for improvement.</p> <p>The basic approach was to write a custom MessageEncoder that would wrap the built-in JSON encoder with a javascript function name based on a passed in querystring parameter.</p> <p>The first thing we need is a MessageEncodingBindingElement and BindingElementExtensionElement so that we can define our own binding in the config file.  This code was based heavily on the GZip encoding example.  The MessageEncodingBindingElement wraps a WebMessageEncodingBindingElement which will be used to create the factory we need to get the built-in JSON encoder.<br><br></p> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Xml;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Channels;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Description;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td></font><font style="color:blue">namespace</font><font style="font-size:11px"> JsonpEncoder {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">class</font><font style="font-size:11px"> JsonpMessageEncodingElement : BindingElementExtensionElement {  </font></td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> JsonpMessageEncodingElement() {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> Type BindingElementType {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">typeof</font><font style="font-size:11px">(JsonpMessageEncodingElement); }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> ApplyConfiguration(BindingElement bindingElement) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">base</font><font style="font-size:11px">.ApplyConfiguration(bindingElement);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            JsonpMessageEncodingBindingElement binding = (JsonpMessageEncodingBindingElement)bindingElement;  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">protected</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> BindingElement CreateBindingElement() {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            JsonpMessageEncodingBindingElement bindingElement = </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement();  </font></td></tr> <tr> <td>            </font><font style="color:blue">this</font><font style="font-size:11px">.ApplyConfiguration(bindingElement);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">return</font><font style="font-size:11px"> bindingElement;  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Xml;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Channels;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Configuration;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Description;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Text;  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">namespace</font><font style="font-size:11px"> JsonpEncoder {  </font></td></tr> <tr> <td>    </font><font style="color:blue">class</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement : MessageEncodingBindingElement {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">private</font><font style="font-size:11px"> WebMessageEncodingBindingElement _innerBindingElement;  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">internal</font><font style="font-size:11px"> WebMessageEncodingBindingElement InnerBindingElement {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerBindingElement; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">set</font><font style="font-size:11px"> { _innerBindingElement = value; }  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement()  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            : </font><font style="color:blue">this</font><font style="font-size:11px">(</font><font style="color:blue">new</font><font style="font-size:11px"> WebMessageEncodingBindingElement()) {  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        JsonpMessageEncodingBindingElement(WebMessageEncodingBindingElement bindingElement) {  </td></tr> <tr> <td style="background-color:#f7f7f7">            _innerBindingElement = bindingElement;  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> MessageVersion MessageVersion {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerBindingElement.MessageVersion; }  </font></td></tr> <tr> <td>            </font><font style="color:blue">set</font><font style="font-size:11px"> { _innerBindingElement.MessageVersion = value; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> IChannelFactory&lt;TChannel&gt; BuildChannelFactory&lt;TChannel&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (context == </font><font style="color:blue">null</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArgumentNullException(</font><font style="color:blue">&quot;context&quot;</font><font style="font-size:11px">);  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            context.BindingParameters.Add(</font><font style="color:blue">this</font><font style="font-size:11px">);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> context.BuildInnerChannelFactory&lt;TChannel&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> IChannelListener&lt;TChannel&gt; BuildChannelListener&lt;TChannel&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (context == </font><font style="color:blue">null</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArgumentNullException(</font><font style="color:blue">&quot;context&quot;</font><font style="font-size:11px">);  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            context.BindingParameters.Add(</font><font style="color:blue">this</font><font style="font-size:11px">);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> context.BuildInnerChannelListener&lt;TChannel&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> CanBuildChannelListener&lt;TChannel&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (context == </font><font style="color:blue">null</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">throw</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArgumentNullException(</font><font style="color:blue">&quot;context&quot;</font><font style="font-size:11px">);  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            context.BindingParameters.Add(</font><font style="color:blue">this</font><font style="font-size:11px">);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> context.CanBuildInnerChannelListener&lt;TChannel&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> MessageEncoderFactory CreateMessageEncoderFactory() {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpMessageEncoderFactory(</font><font style="color:blue">this</font><font style="font-size:11px">, _innerBindingElement.CreateMessageEncoderFactory());  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> BindingElement Clone() {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement((WebMessageEncodingBindingElement)</font><font style="color:blue">this</font><font style="font-size:11px">.InnerBindingElement.Clone());  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> T GetProperty&lt;T&gt;(BindingContext context) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> _innerBindingElement.GetProperty&lt;T&gt;(context) ?? context.GetInnerProperty&lt;T&gt;();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br>Next we need a custom MessageEncoderFactory to produce our jsonp encoder.  It's pretty straightfoward, it also wraps a MessageEncoderFactory created by the WebMessageEncodingBindingElement in the above code.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">using System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">using System.ServiceModel.Channels;  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">namespace JsonpEncoder {  </td></tr> <tr> <td>    class JsonpMessageEncoderFactory : MessageEncoderFactory {  </td></tr> <tr> <td style="background-color:#f7f7f7">        private JsonpEncoder _encoder;  </td></tr> <tr> <td>        private MessageEncoderFactory _innerFactory;  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        public JsonpMessageEncoderFactory(JsonpMessageEncodingBindingElement bindingElement, MessageEncoderFactory innerFactory) {  </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:red">_innerFactory</font><font style="font-size:11px"> = innerFactory;  </font></td></tr> <tr> <td>            </font><font style="color:red">_encoder</font><font style="font-size:11px"> = </font><font style="color:blue">new</font><font style="font-size:11px"> JsonpEncoder(bindingElement,_innerFactory.Encoder);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        public override MessageEncoder Encoder {  </td></tr> <tr> <td>            get { return _encoder; }  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        public override MessageVersion MessageVersion {  </td></tr> <tr> <td>            get { return _innerFactory.MessageVersion; }  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr></tbody></table></div><br>Now we need the actual encoder to do the work.  Here again we are wrapping an inner encoder provided by the wrapped factory.  For everything but the WriteMessage method we are just calling the methods on the wrapped encoder.  The WriteMessage method has pretty much all the interesting code in it as it actually sticks the call to the javascript function based on the querystring parameter named callback.  It defaults to something if that value isn't present.  Since we are referencing the HttpContext that means we'll need to set AspNetCompatibilityRequirements to Allowed.  This was one of the things in particular I was hoping someone had a better way to do.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.IO;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Channels;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.Text;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.Web;  </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">namespace</font><font style="font-size:11px"> JsonpEncoder {  </font></td></tr> <tr> <td>    </font><font style="color:blue">class</font><font style="font-size:11px"> JsonpEncoder : MessageEncoder {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">private</font><font style="font-size:11px"> JsonpMessageEncodingBindingElement _bindingElement;  </font></td></tr> <tr> <td>        </font><font style="color:blue">private</font><font style="font-size:11px"> MessageEncoder _innerEncoder;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> JsonpEncoder(JsonpMessageEncodingBindingElement bindingElement, MessageEncoder innerEncoder) {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            _bindingElement = bindingElement;  </td></tr> <tr> <td>            _innerEncoder = innerEncoder;  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> ContentType {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.ContentType; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> MediaType {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.MediaType; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> MessageVersion MessageVersion {  </font></td></tr> <tr> <td>            </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.MessageVersion; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> IsContentTypeSupported(</font><font style="color:blue">string</font><font style="font-size:11px"> contentType) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (contentType.StartsWith(</font><font style="color:blue">&quot;application/json&quot;</font><font style="font-size:11px">)) {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">true</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            } </font><font style="color:blue">else</font><font style="font-size:11px"> {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">false</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            }  </td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> Message ReadMessage(ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; buffer, BufferManager bufferManager, </font><font style="color:blue">string</font><font style="font-size:11px"> contentType) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.ReadMessage(buffer, bufferManager, contentType);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> Message ReadMessage(Stream stream, </font><font style="color:blue">int</font><font style="font-size:11px"> maxSizeOfHeaders, </font><font style="color:blue">string</font><font style="font-size:11px"> contentType) {  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> _innerEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; WriteMessage(Message message, </font><font style="color:blue">int</font><font style="font-size:11px"> maxMessageSize, BufferManager bufferManager, </font><font style="color:blue">int</font><font style="font-size:11px"> messageOffset) {  </font></td></tr> <tr> <td>            ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; arraySegment = _innerEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">string</font><font style="font-size:11px"> messageString = Encoding.UTF8.GetString(arraySegment.Array, 0, arraySegment.Count);  </font></td></tr> <tr> <td>            bufferManager.ReturnBuffer(arraySegment.Array);  </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">byte</font><font style="font-size:11px">[] buffer = bufferManager.TakeBuffer(arraySegment.Count);  </font></td></tr> <tr> <td>            </font><font style="color:blue">string</font><font style="font-size:11px"> callbackFunction;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">if</font><font style="font-size:11px"> (HttpContext.Current.Request.QueryString[</font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">] != </font><font style="color:blue">&quot;&quot;</font><font style="font-size:11px">) {  </font></td></tr> <tr> <td>                callbackFunction = HttpContext.Current.Request.QueryString[</font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">];  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            } </font><font style="color:blue">else</font><font style="font-size:11px"> {  </font></td></tr> <tr> <td>                callbackFunction = </font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            messageString = callbackFunction + </font><font style="color:blue">&quot;('&quot;</font><font style="font-size:11px"> + messageString.Replace(</font><font style="color:blue">&quot;'&quot;</font><font style="font-size:11px">, </font><font style="color:blue">&quot;\\'&quot;</font><font style="font-size:11px">) + </font><font style="color:blue">&quot;');&quot;</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            </font><font style="color:blue">int</font><font style="font-size:11px"> count = Encoding.UTF8.GetBytes(messageString, 0, messageString.Length, buffer, 0);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt;(buffer, 0, count);  </font></td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">void</font><font style="font-size:11px"> WriteMessage(Message message, Stream stream) {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            _innerEncoder.WriteMessage(message, stream);  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr></tbody></table></div><br>Now we need a service to use the new encoder.  I just made up something very basic for demonstration purposes, but note the setting of AspNetCompatibilityRequirements.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px"></font><font style="color:blue">using</font><font style="font-size:11px"> System;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Activation;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">using</font><font style="font-size:11px"> System.ServiceModel.Web;  </font></td></tr> <tr> <td></font><font style="color:blue">using</font><font style="font-size:11px"> System.Runtime.Serialization;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>[ServiceContract()]  </td></tr> <tr> <td style="background-color:#f7f7f7">[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]  </td></tr> <tr> <td></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> TestService  {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    [OperationContract]  </td></tr> <tr> <td>    [WebGet]  </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Operation1(</font><font style="color:blue">string</font><font style="font-size:11px"> val) {  </font></td></tr> <tr> <td>        </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">&quot;Hello: &quot;</font><font style="font-size:11px"> + val;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">    [OperationContract]  </td></tr> <tr> <td>    [WebGet]  </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">public</font><font style="font-size:11px"> TestContract Operation2(</font><font style="color:blue">string</font><font style="font-size:11px"> field1, </font><font style="color:blue">string</font><font style="font-size:11px"> field2) {  </font></td></tr> <tr> <td>        TestContract ret = </font><font style="color:blue">new</font><font style="font-size:11px"> TestContract();  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        ret.Field1 = field1;  </td></tr> <tr> <td>        ret.Field2 = field2;  </td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">return</font><font style="font-size:11px"> ret;  </font></td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">}  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">[DataContract]  </td></tr> <tr> <td></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> TestContract {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">string</font><font style="font-size:11px"> field1;  </font></td></tr> <tr> <td>    </font><font style="color:blue">string</font><font style="font-size:11px"> field2;  </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>    [DataMember]  </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Field1 {  </font></td></tr> <tr> <td>        </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> field1; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">set</font><font style="font-size:11px"> { field1 = value; }  </font></td></tr> <tr> <td>    }  </td></tr> <tr> <td style="background-color:#f7f7f7">    [DataMember]  </td></tr> <tr> <td>    </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">string</font><font style="font-size:11px"> Field2 {  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">get</font><font style="font-size:11px"> { </font><font style="color:blue">return</font><font style="font-size:11px"> field2; }  </font></td></tr> <tr> <td>        </font><font style="color:blue">set</font><font style="font-size:11px"> { field2 = value; }  </font></td></tr> <tr> <td style="background-color:#f7f7f7">    }  </td></tr> <tr> <td>} </td></tr></tbody></table></div><br>Now we'll set up the config file to define different endpoints to the service.  Well setup one endpoint for jsonp using our custom binding and httptransport.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">    </font><font style="color:blue">&lt;</font><font style="font-size:11px">system.serviceModel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">&lt;</font><font style="font-size:11px">extensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;</font><font style="font-size:11px">bindingElementExtensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">add </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonpEncoding&quot;</font><font style="font-size:11px"> </font><font style="color:red">type</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JsonpEncoder.JsonpMessageEncodingElement, JsonpEncoder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">bindingElementExtensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">extensions</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>        </font><font style="color:blue">&lt;</font><font style="font-size:11px">services</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;</font><font style="font-size:11px">service </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultServiceBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;webHttpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultEndpointBehavior&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestServiceJSON&quot;</font><font style="font-size:11px"> </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;json&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;webHttpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONEndpointBehavior&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestServiceJSONP&quot;</font><font style="font-size:11px"> </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonp&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;TestService&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;customBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">bindingConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:red">behaviorConfiguration</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONPEndpointBehavior&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpoint </font><font style="color:red">address</font><font style="font-size:11px">=</font><font style="color:blue">&quot;mex&quot;</font><font style="font-size:11px"> </font><font style="color:red">contract</font><font style="font-size:11px">=</font><font style="color:blue">&quot;IMetadataExchange&quot;</font><font style="font-size:11px"> </font><font style="color:red">binding</font><font style="font-size:11px">=</font><font style="color:blue">&quot;mexHttpBinding&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">service</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">services</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;</font><font style="font-size:11px">bindings</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;</font><font style="font-size:11px">customBinding</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">binding </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;jsonpBinding&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">jsonpEncoding </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">httpTransport </font><font style="color:red">manualAddressing</font><font style="font-size:11px">=</font><font style="color:blue">&quot;true&quot;</font><font style="font-size:11px"> </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">binding</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">customBinding</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">bindings</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceHostingEnvironment </font><font style="color:red">aspNetCompatibilityEnabled</font><font style="font-size:11px">=</font><font style="color:blue">&quot;true&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">serviceHostingEnvironment</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;</font><font style="font-size:11px">behaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultServiceBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceDebug </font><font style="color:red">includeExceptionDetailInFaults</font><font style="font-size:11px">=</font><font style="color:blue">&quot;true&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">serviceMetadata </font><font style="color:red">httpGetEnabled</font><font style="font-size:11px">=</font><font style="color:blue">&quot;True&quot;</font><font style="font-size:11px">/&gt;  </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">serviceBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;</font><font style="font-size:11px">endpointBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;DefaultEndpointBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">webHttp </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONEndpointBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">enableWebScript </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;</font><font style="font-size:11px">behavior </font><font style="color:red">name</font><font style="font-size:11px">=</font><font style="color:blue">&quot;JSONPEndpointBehavior&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>                    </font><font style="color:blue">&lt;</font><font style="font-size:11px">enableWebScript </font><font style="color:blue">/&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">                </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behavior</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>            </font><font style="color:blue">&lt;/</font><font style="font-size:11px">endpointBehaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">        </font><font style="color:blue">&lt;/</font><font style="font-size:11px">behaviors</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>    </font><font style="color:blue">&lt;/</font><font style="font-size:11px">system.serviceModel</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br>Finally we need an html page to call the service.  Again, something very very basic for demonstration.<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">&lt;</font><font style="font-size:11px">html </font><font style="color:red">xmlns</font><font style="font-size:11px">=</font><font style="color:blue">&quot;http://www.w3.org/1999/xhtml&quot;</font><font style="font-size:11px"> </font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td></font><font style="color:blue">&lt;</font><font style="font-size:11px">head</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">&lt;</font><font style="font-size:11px">title</font><font style="color:blue">&gt;</font><font style="font-size:11px">Jsonp Encoder Test</font><font style="color:blue">&lt;/</font><font style="font-size:11px">title</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td>      </td></tr> <tr> <td style="background-color:#f7f7f7">    </font><font style="color:blue">&lt;</font><font style="font-size:11px">script </font><font style="color:red">type</font><font style="font-size:11px">=</font><font style="color:blue">&quot;text/javascript&quot;</font><font style="font-size:11px">&gt;  </font></td></tr> <tr> <td>        function callbackFunction(jsonstr) {  </td></tr> <tr> <td style="background-color:#f7f7f7">           var </font><font style="color:red">obj</font><font style="font-size:11px"> = </font><font style="color:blue">eval</font><font style="font-size:11px">('(' + jsonstr + ')');  </font></td></tr> <tr> <td>           </font><font style="color:red">obj</font><font style="font-size:11px"></font><font style="color:blue">obj</font><font style="font-size:11px"> = obj.d;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">           alert(&quot;</font><font style="color:red">Field1</font><font style="font-size:11px"> = &quot; + obj.Field1);  </font></td></tr> <tr> <td>           alert(&quot;</font><font style="color:red">Field2</font><font style="font-size:11px"> = &quot; + obj.Field2);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td>    </font><font style="color:blue">&lt;/</font><font style="font-size:11px">script</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">      </td></tr> <tr> <td>    </font><font style="color:blue">&lt;</font><font style="font-size:11px">script </font><font style="color:red">src</font><font style="font-size:11px">=</font><font style="color:blue">&quot;testservice.svc/jsonp/Operation2?field1=test1&amp;field2=test2&amp;callback=callbackFunction&quot;</font><font style="font-size:11px">&gt;</font><font style="color:blue">&lt;/</font><font style="font-size:11px">script</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">      </td></tr> <tr> <td></font><font style="color:blue">&lt;/</font><font style="font-size:11px">head</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">&lt;</font><font style="font-size:11px">body</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">&lt;/</font><font style="font-size:11px">body</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td></font><font style="color:blue">&lt;/</font><font style="font-size:11px">html</font><font style="color:blue">&gt;</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br> <p>So, there it is.  I'd love to hear suggestions on better ways to do things.  Going through this process taught me quite a bit and I know I have a lot more to go.</p> <p>Eric</p><br><hr size="1" align="left" width="25%">EricSat, 01 Nov 2008 00:35:07 Z2008-11-01T00:35:07Zhttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#106cbf28-cecf-43dd-89c5-c5e1c363bc1ahttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#106cbf28-cecf-43dd-89c5-c5e1c363bc1abkumars8http://social.msdn.microsoft.com/Profile/en-US/?user=bkumars8Feedback on json with padding implementation This's pretty neat<br>Though I'd lean towards using WebOperationContext in stead of HttpContext and turn off ASPCompaibilityMode as<br><br><font color="#0000ff" size=2> <p>if</font><font size=2> (</font><font color="#2b91af" size=2>WebOperationContext</font><font size=2>.Current.IncomingRequest.UriTemplateMatch.QueryParameters[</font><font color="#a31515" size=2>&quot;callback&quot;</font><font size=2>] != </font><font color="#a31515" size=2>&quot;&quot;</font><font size=2>)</p> <p>{</p> <p>callbackFunction = </font><font color="#2b91af" size=2>WebOperationContext</font><font size=2>.Current.IncomingRequest.UriTemplateMatch.QueryParameters[</font><font color="#a31515" size=2>&quot;callback&quot;</font><font size=2>];</p> <p>}</p></font>Thu, 11 Dec 2008 18:22:37 Z2008-12-11T18:22:37Zhttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#5ba298e7-62b3-402b-9e1b-8a0a3d71bde7http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#5ba298e7-62b3-402b-9e1b-8a0a3d71bde7Carlos Figueirahttp://social.msdn.microsoft.com/Profile/en-US/?user=Carlos%20FigueiraFeedback on json with padding implementation Great implementation! A few suggestions:<br><br>On JsonpEncoder.IsContentTypeSupported, some clients also send text/json (instead of application/json):<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> </font><font style="color:blue">bool</font><font style="font-size:11px"> IsContentTypeSupported(</font><font style="color:blue">string</font><font style="font-size:11px"> contentType)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        {  </td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (contentType.StartsWith(</font><font style="color:blue">&quot;application/json&quot;</font><font style="font-size:11px">) || contentType.StartsWith(</font><font style="color:blue">&quot;text/json&quot;</font><font style="font-size:11px">))  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            {  </td></tr> <tr> <td>                </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">true</font><font style="font-size:11px">;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            }  </td></tr> <tr> <td>            </font><font style="color:blue">else</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            {  </td></tr> <tr> <td>                </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">false</font><font style="font-size:11px">;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            }  </td></tr> <tr> <td>        }  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr></tbody></table></div><br>On JsonpEncoder.WriteMessage, you should take a buffer that is at least as large as your final message. Also, the messageOffset (which is almost always 0, but not guaranteed to be) should be taken into account as well:<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">        </font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">override</font><font style="font-size:11px"> ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; WriteMessage(Message message, </font><font style="color:blue">int</font><font style="font-size:11px"> maxMessageSize, BufferManager bufferManager, </font><font style="color:blue">int</font><font style="font-size:11px"> messageOffset)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        {  </td></tr> <tr> <td>            ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt; arraySegment = _innerEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">string</font><font style="font-size:11px"> messageString = Encoding.UTF8.GetString(arraySegment.Array, 0, arraySegment.Count);  </font></td></tr> <tr> <td>            bufferManager.ReturnBuffer(arraySegment.Array);  </td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">string</font><font style="font-size:11px"> callbackFunction;  </font></td></tr> <tr> <td>            </font><font style="color:blue">if</font><font style="font-size:11px"> (HttpContext.Current.Request.QueryString[</font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">] != </font><font style="color:blue">&quot;&quot;</font><font style="font-size:11px">)  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            {  </td></tr> <tr> <td>                callbackFunction = HttpContext.Current.Request.QueryString[</font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">];  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            }  </td></tr> <tr> <td>            </font><font style="color:blue">else</font><font style="font-size:11px"> </font></td></tr> <tr> <td style="background-color:#f7f7f7">            {  </td></tr> <tr> <td>                callbackFunction = </font><font style="color:blue">&quot;callback&quot;</font><font style="font-size:11px">;  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            }  </td></tr> <tr> <td> </td></tr> <tr> <td style="background-color:#f7f7f7">            messageString = callbackFunction + </font><font style="color:blue">&quot;('&quot;</font><font style="font-size:11px"> + messageString.Replace(</font><font style="color:blue">&quot;'&quot;</font><font style="font-size:11px">, </font><font style="color:blue">&quot;\\'&quot;</font><font style="font-size:11px">) + </font><font style="color:blue">&quot;');&quot;</font><font style="font-size:11px">;  </font></td></tr> <tr> <td>            </font><font style="color:blue">byte</font><font style="font-size:11px">[] buffer = bufferManager.TakeBuffer(messageString.Length + messageOffset);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">            </font><font style="color:blue">int</font><font style="font-size:11px"> count = Encoding.UTF8.GetBytes(messageString, 0, messageString.Length, buffer, messageOffset);  </font></td></tr> <tr> <td>            </font><font style="color:blue">return</font><font style="font-size:11px"> </font><font style="color:blue">new</font><font style="font-size:11px"> ArraySegment&lt;</font><font style="color:blue">byte</font><font style="font-size:11px">&gt;(buffer, messageOffset, count);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">        }  </td></tr> <tr> <td> </td></tr></tbody></table></div>Thu, 11 Dec 2008 18:45:39 Z2008-12-11T18:45:39Zhttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#eb1564d0-29eb-474f-8f4f-88589dfb790dhttp://social.msdn.microsoft.com/Forums/en-US/wcf/thread/eb559c61-d512-4692-aaa1-2c3656c78e49#eb1564d0-29eb-474f-8f4f-88589dfb790dAndy McCluggagehttp://social.msdn.microsoft.com/Profile/en-US/?user=Andy%20McCluggageFeedback on json with padding implementation Great post. It has been an enormous help to me.<br><br>Just something to watch out for if you are using non-standard character sets. We found that when using some character sets in our message (mainly characters with accents) the length of the un-encoded message was not enough to define the size of the buffer required for the encoded message. We had to use Encoder.GetByteCount(...).<br><br> <div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;line-height:100%! important;border-bottom:#7f9db9 1px solid;font-family:Courier New;background-color:white"> <table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="padding-left:10px;font-size:11px;border-bottom:#f7f7f7 1px solid;font-family:Courier New;white-space:nowrap"> <tbody> <tr> <td><font style="font-size:11px">int </font><font style="color:red">bufferSize</font><font style="font-size:11px"> = </font><font style="color:blue">Encoding</font><font style="font-size:11px">.UTF8.GetByteCount(messageString);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">byte[] </font><font style="color:red">buffer</font><font style="font-size:11px"> = </font><font style="color:blue">bufferManager</font><font style="font-size:11px">.TakeBuffer(bufferSize  + messageOffset);  </font></td></tr> <tr> <td>int </font><font style="color:red">count</font><font style="font-size:11px"> = </font><font style="color:blue">Encoding</font><font style="font-size:11px">.UTF8.GetBytes(messageString, 0, messageString.Length, buffer, messageOffset);  </font></td></tr> <tr> <td style="background-color:#f7f7f7">return new ArraySegment</font><font style="color:blue">&lt;</font><font style="font-size:11px">byte</font><font style="color:blue">&gt;</font><font style="font-size:11px">(buffer, messageOffset, count);  </font></td></tr> <tr> <td> </td></tr></tbody></table></div>Thu, 15 Jan 2009 13:20:34 Z2009-01-15T13:31:18Z