WCF服务合同可以有一个可为空的输入参数? [英] Can a WCF service contract have a nullable input parameter?
问题描述
我有这样定义的合同:
[OperationContract]
[WebGet(UriTemplate = "/GetX?myStr={myStr}&myX={myX}", BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetX(string myStr, int? myX);
我得到一个异常:
[出现InvalidOperationException:操作'的getX'合同'IMyGet有一个查询变量命名的类型为MYX'System.Nullable 1 [System.Int32]',但类型System.Nullable
1 [System.Int32]不是通过QueryStringConverter敞篷车。为UriTemplate查询的值的变量必须具有可以通过'QueryStringConverter'转换类型]
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'.]
无法找到有关此错误除下列链接任何东西:
< A HREF =http://blog.rolpdog.com/2007/07/webget-and-webinvoke-rock.html> http://blog.rolpdog.com/2007/07/webget-and-webinvoke-rock html的这是一个小老头,反正不是办法。
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?
感谢。
推荐答案
是的,你可以有WCF空的参数的。我觉得这里的问题是,QueryStringConverter不空的参数的工作。
Yes, you can have nullable parameters with WCF. I think your problem here is that QueryStringConverter does not work with nullable parameters.
该怎么办?你需要使用UriTemplate属性?如果您发布此为典型的Web服务',那么你就不会有这个问题。
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.
另外一种选择是跟随劝你提供的链接 - 即收到MYX参数作为字符串,然后将其转换为int ?,其中,(说)n是零。不漂亮。
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.
这篇关于WCF服务合同可以有一个可为空的输入参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!