How can I implement a custom QueryStringConverter for RESTful WCF?

此生再无相见时 提交于 2019-12-10 23:15:56

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3738201/how-can-i-implement-a-custom-querystringconverter-for-restful-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!