in Education by
Using the WCF web programming model one can specify an operation contract like so: [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "SomeRequest?qs1={qs1}&qs2={qs2}")] XElement SomeRequest1(string qs1, string qs2); Now if we had to make a contract that accepts an array of parameters with the same name (in this case qs1) contract like so... [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "SomeRequest?qs1={qs1}&qs1={qs2}")] XElement SomeRequest2(string qs1, string qs2); We get the error message at run time when we make the invocation to the method: the query string must have 'name=value' pairs with unique names. Note that the names are case-insensitive. See the documentation for UriTemplate for more details. How does one define an HTTP service that exposes a resource with an array of parameters without resorting to a loosey-goosey interface? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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. http://server/service/SomeRequest?qs1=val1,val2,val3,val4) [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "SomeRequest?qs1={qs1}")] XElement SomeRequest2(string[] qs1); First you need a class that inherits from WebHttpBehavior so that we can inject our custom QueryStringConverter: public class CustomHttpBehavior : System.ServiceModel.Description.WebHttpBehavior { protected override System.ServiceModel.Dispatcher.QueryStringConverter GetQueryStringConverter(System.ServiceModel.Description.OperationDescription operationDescription) { return new CustomQueryStringConverter(); } } Then our CustomQueryStringConverter that handles string[] parameters: 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); } } The last thing you need to do is create a behavior configuration extension so that the runtime can get an instance of the CustomWebHttpBehavior: public class CustomHttpBehaviorExtensionElement : System.ServiceModel.Configuration.BehaviorExtensionElement { protected override object CreateBehavior() { return new CustomHttpBehavior(); } public override Type BehaviorType { get { return typeof(CustomHttpBehavior); } } } Now we add the element to our configuration extensions so that our CustomWebHttpBehavior is used, we use the Name of that extension instead of in our behavior: You can now also extend your CustomQueryStringConverter to handle other types that the default one doesn't, such as nullable value types. 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. http://connect.microsoft.com/VisualStudio/feedback/details/616486/bug-with-getquerystringconverter-not-being-called-by-webservicehost#tabs 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).

Related questions

+1 vote
    Can variables belonging to different scope have same name in C Programming? If so show an example ?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Write a program to store 10 names in an array. Arrange these in alphabetical orderby sorting. Print the sorted list. ... , LUCY, etc. Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    What is one-way operation in WCF?...
asked Jul 26, 2021 in Technology by JackTerrance
0 votes
    A game consists of tossing a one-rupee coin three times and noting its outcome each time. Hanif wins if all the ... will lose the game. Select the correct answer from above options...
asked Nov 17, 2021 in Education by JackTerrance
0 votes
    I am new to jmeter. I have been experimenting on creating a jmeter script manually by inspecting the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    What is Fault Contract in WCF?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
    What is Message Contract in WCF?...
asked Apr 2, 2021 in Education by JackTerrance
0 votes
0 votes
    What is Service Contract in WCF?...
asked Apr 1, 2021 in Education by JackTerrance
0 votes
    write a query to display the numeric of employees with same job? Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    What is the process of defining two or more methods within same class that have same name but different ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    If Linear regression model perfectly first i.e., train error is zero, then _____________________ (a) Test error ... of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
...