tags:

views:

1128

answers:

2

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.Nullable1[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.

+1  A: 

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.

Kirk Broadhurst
+4  A: 

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 extend QueryStringConverter to support any type you would need. See the accepted answer in this post ==>

http://stackoverflow.com/questions/354727/in-wcf-web-programming-model-how-can-one-write-a-operation-contract-with-an-arra

WayneC