Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've implemented a simple custom QueryStringConverter so that you can make qs1 an string[] then have the query string variable be comma delimited (e.g. <a href="http://server/service/SomeRequest?qs1=val1,val2,val3,val4" rel="noreferrer">http://server/service/SomeRequest?qs1=val1,val2,val3,val4</a>)</p> <pre><code>[OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "SomeRequest?qs1={qs1}")] XElement SomeRequest2(string[] qs1); </code></pre> <p>First you need a class that inherits from WebHttpBehavior so that we can inject our custom QueryStringConverter:</p> <pre><code>public class CustomHttpBehavior : System.ServiceModel.Description.WebHttpBehavior { protected override System.ServiceModel.Dispatcher.QueryStringConverter GetQueryStringConverter(System.ServiceModel.Description.OperationDescription operationDescription) { return new CustomQueryStringConverter(); } } </code></pre> <p>Then our CustomQueryStringConverter that handles string[] parameters:</p> <pre><code>public class CustomQueryStringConverter : System.ServiceModel.Dispatcher.QueryStringConverter { public override bool CanConvert(Type type) { if (type == typeof(string[])) { return true; } return base.CanConvert(type); } public override object ConvertStringToValue(string parameter, Type parameterType) { if (parameterType == typeof(string[])) { string[] parms = parameter.Split(','); return parms; } return base.ConvertStringToValue(parameter, parameterType); } public override string ConvertValueToString(object parameter, Type parameterType) { if (parameterType == typeof(string[])) { string valstring = string.Join(",", parameter as string[]); return valstring; } return base.ConvertValueToString(parameter, parameterType); } } </code></pre> <p>The last thing you need to do is create a behavior configuration extension so that the runtime can get an instance of the CustomWebHttpBehavior:</p> <pre><code>public class CustomHttpBehaviorExtensionElement : System.ServiceModel.Configuration.BehaviorExtensionElement { protected override object CreateBehavior() { return new CustomHttpBehavior(); } public override Type BehaviorType { get { return typeof(CustomHttpBehavior); } } } </code></pre> <p>Now we add the element to our configuration extensions so that our CustomWebHttpBehavior is used, we use the Name of that extension instead of <code>&lt;webHttp /&gt;</code> in our behavior:</p> <pre><code> &lt;system.serviceModel&gt; &lt;services&gt; &lt;service name="NameSpace.ServiceClass"&gt; &lt;endpoint address="" behaviorConfiguration="MyServiceBehavior" binding="webHttpBinding" contract="NameSpace.ServiceClass" /&gt; &lt;/service&gt; &lt;/services&gt; &lt;behaviors&gt; &lt;endpointBehaviors&gt; &lt;behavior name="MyServiceBehavior"&gt; &lt;customWebHttp/&gt; &lt;/behavior&gt; &lt;/endpointBehaviors&gt; &lt;/behaviors&gt; &lt;extensions&gt; &lt;behaviorExtensions&gt; &lt;add name="customWebHttp" type="NameSpace.CustomHttpBehaviorExtensionElement, MyAssemblyName" /&gt; &lt;/behaviorExtensions&gt; &lt;/extensions&gt; &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt; &lt;/system.serviceModel&gt; </code></pre> <p>You can now also extend your CustomQueryStringConverter to handle other types that the default one doesn't, such as nullable value types.</p> <p>NOTE: There is a bug logged at microsoft connect that directly relates to this code. The code does not actually work in almost all circumstances where you attempt to Query Convert different types.</p> <p><a href="http://connect.microsoft.com/VisualStudio/feedback/details/616486/bug-with-getquerystringconverter-not-being-called-by-webservicehost#tabs" rel="noreferrer">http://connect.microsoft.com/VisualStudio/feedback/details/616486/bug-with-getquerystringconverter-not-being-called-by-webservicehost#tabs</a></p> <p>Please make sure you read this carefully before wasting hours of your time creating custom REST query string converters that cannot possibly work. (Applies to Framework 4.0 and below).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

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

Reload