I have a contract defined like this:
[OperationContract]
[WebGet(UriTemplate = "/GetX?myStr={myStr}&myX={myX}", BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetX(string myStr, int? myX);
I get an exception:
[InvalidOperationException: Operation 'GetX' in contract 'IMyGet' has a query variable named 'myX' of type 'System.Nullable1[System.Int32]', but type 'System.Nullable
1[System.Int32]' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.]
could not find anything about this error except the following link: http://blog.rolpdog.com/2007/07/webget-and-webinvoke-rock.html which is a little old and not a solution anyway.
any ideas what to do except get rid of the nullable parameter?
thanks.
Hum, the quick solution (not pretty) is to accept the nullable parameter as a string in the WCF respective Interface and Service codes.
There is a solution to this problem that does not require any hacks. It might look like a lot of work but it's not really and makes a lot of sense if you read through it. The core of the problem is that there is indeed an unresolved bug (as of .NET 4) that means the WebServiceHost does not use custom QueryStringConverters. So you need to a little extra work and understand how the WCF configuration for WebHttpEndpoints works. The below lays out the solution for you.
First, a custom QueryStringConverter that allows nulls to be provided in the query string by omitting them, or providing a blank string:
Now a custom WebHttpBehavior that will set the custom QueryStringConverter to be used in place of the standard one. Note that this behavior derivces from WebHttpBehavior which is important so that we inherit the behavior required for a REST endpoint:
Now a custom ServiceHost that adds the custom behavior to the WebHttpEndpoint so that it will use the custom QueryStringConverter. The important thing to note in this code, is that it derives from ServiceHost and NOT WebServiceHost. This is important because otherwise the bug mentioned above will prevent the custom QueryStringConverter from being used:
Because we are not deriving from WebServiceHost we need to do it's work and make sure our configuration is correct to ensure the REST service will work. Something like the following is all you need. In this configuration, i also have a WS HTTP endpoint setup because i needed to access this service from both C# (using WS HTTP as its nicer) and mobile devices (using REST). You can omit the configuration for this endpoint if you don't need it. One important thing to note is that you DO NOT need the custom endpoint behavior anymore. This is because we are now adding our own custom endpoint behavior that binds the custom QueryStringConverter. It derives from WebHttpBehavior which is what the configuration added, making it now redundant.
The last thing to do is create a custom ServiceHostFactory and tell the svc file to use it, which will cause all the custom code to be used. Of course you could also create a custom element that would allow you to add the behavior in configuration, but i think for this behavior a code-based approach is better, since it is unlikely you will want to remove the ability to process nullable types, as it will break your service:
Change the markup of your Service.svc file to the following:
Now you can use nullable types in your service interface without any issues, simply by omitting the parameter or setting it to an empty string. The following resources may be of more assistance to you:
Hope this helps!
Actually...you absolutely can have nullable parameters, or any other type of parameter that isn't supported by
QueryStringConverter
out of the box. All you need to do is extendQueryStringConverter
to support any type you would need. See the accepted answer in this post ==>In the WCF web programming model, how can one write an operation contract with an array of query string parameters (i.e. with the same name)?
Yes, you can have nullable parameters with WCF. I think your problem here is that QueryStringConverter does not work with nullable parameters.
What to do? Do you need to use the UriTemplate attribute? If you published this as a 'classic web service' then you wouldn't have this issue.
The other option is to follow the advise in the link you provided - i.e. receive the myX parameter as string and then cast it to an int?, where (say) "n" is null. Not pretty.