如何为 RESTful WCF 实现自定义 QueryStringConverter?

发布于 2024-09-19 11:23:40 字数 221 浏览 4 评论 0原文

我已经实现了一个自定义的 QueryStringConverter 类,并使用自定义的 WebHttpBehavior 子类将其连接起来。当我进行服务调用时,它会命中 CanConvert 覆盖中的断点(并且我为此参数返回 true),但它从未调用我的 ConvertStringToValue 覆盖,并且最终只是将 null 传递给服务调用...为什么 ConvertStringToValue 永远不会打电话,我该如何解决?

I've implemented a customized QueryStringConverter class and hooked it up using a customized WebHttpBehavior subclass. When I make a service call, it hits my breakpoint in the CanConvert override (and I return true for this parameter), but it never calls my ConvertStringToValue override, and ends up just passing null to the service call... why is ConvertStringToValue never called and how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疑心病 2024-09-26 11:23:40

这是不可能的。
Microsoft 对该功能的实现非常草率,以至于他们只是更新了标准 QueryStringConverter,而不是使用配置文件中配置的 QueryStringConverter。

没有任何实际可行的解决方法。错误报告中的第二个实际上根本不起作用。

简短的回答是你不能。
请参阅此处的错误: http://connect.microsoft.com/VisualStudio/feedback/details/616486/bug-with-getquerystringconverter-not-being- Called-by-webservicehost#tabs

它在框架 4.0 中仍然损坏。
我的猜测是这并不重要 - 因此也许需要花时间增加错误的计数。

问候

克雷格。

This is not possible.
Microsoft were so sloppy with the implementation of this functionality that they merely newed up the standard QueryStringConverter instread of using the one configured in the configuration file.

There are no work arounds that actually work. The second one in the bug report doesn't actually work at all.

The short answer is that you cannot.
See the bug here: http://connect.microsoft.com/VisualStudio/feedback/details/616486/bug-with-getquerystringconverter-not-being-called-by-webservicehost#tabs

It is still broken in framework 4.0.
My guess is that it's not important - so perhaps take the time to increase the counts on the bug.

Regards

Craig.

陪你搞怪i 2024-09-26 11:23:40

我知道这是一个很老的问题。对于任何寻找答案的人,您应该能够将 TypeConverter 添加到您的类中,该类可以将类型与字符串表示形式相互转换

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.querystringconverter.aspx

具有 TypeConverterAttribute 的类型可以将类型转换为
并来自字符串表示。

I know it is quite old question. For any one who looking for some answer, you should be able to add the TypeConverter to your class which can convert type to and from string representation

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.querystringconverter.aspx

Types that have a TypeConverterAttribute that can convert the type to
and from a string representation.

智商已欠费 2024-09-26 11:23:40

做这样的事情:

在合同文件中

    [OperationContract]
   [WebGet(UriTemplate = "/TabelasAuxiliares?requestex={requestex}", ResponseFormat = WebMessageFormat.Xml)]
        CadastrodeEscolasResponse TabelasAuxiliares(string requestex);

在服务文件中

public CadastrodeEscolasResponse TabelasAuxiliares(string requestex)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(CadastrodeEscolasRequest));
            StringReader rdr = new StringReader(requestex);
            CadastrodeEscolasRequest request = (CadastrodeEscolasRequest)serializer.Deserialize(rdr);
}

结论:
通过将 Xml 格式数据发送到字符串参数来调用服务。然后将xml转换为所需的类对象。这样你就可以避免创建相当麻烦的QueryStringConvertor。希望这会有所帮助!此帮助适用于所有人,而不仅仅是这篇文章。

Do some thing like this:

In contract file

    [OperationContract]
   [WebGet(UriTemplate = "/TabelasAuxiliares?requestex={requestex}", ResponseFormat = WebMessageFormat.Xml)]
        CadastrodeEscolasResponse TabelasAuxiliares(string requestex);

In the Service file

public CadastrodeEscolasResponse TabelasAuxiliares(string requestex)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(CadastrodeEscolasRequest));
            StringReader rdr = new StringReader(requestex);
            CadastrodeEscolasRequest request = (CadastrodeEscolasRequest)serializer.Deserialize(rdr);
}

Conslusion:
Call the service by sending Xml format data to a string parameter. Then convert the xml to the required class object. This way you can avoid creating QueryStringConvertor which is quite cumbersome. Hope this will help! This help is for all and not just for this post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文