【发布时间】:2012-11-17 02:09:09
【问题描述】:
我正在尝试将 WebService 调用的结果读取/转换/解析为字符串。
WebMethod method = list.Services[0].Methods[0] as WebMethod;
if(method != null)
{
Object ResultsObj;
ResultsObj = new Object();
ResultsObj = method.Invoke();
//Returns "WS.GeocodeResponse" needs to be inner values
results = ResultsObj.ToString();
//Like to do the following but get error:
//'object' does not contain a definition for 'Results' and no extension
// method 'Results' accepting a first argument of type 'object' could be
// found (are you missing a using directive or an assembly reference?)
results = ResultsObj.Results[0].ToString();
//Or, do something like this but get same error
results = String.Format("Latitude: {0}, Longitude: {1}",
ResultsObj.Results[0].Locations[0].Latitude,
ResultsObj.Results[0].Locations[0].Longitude);
//Tried casting as an XmlDocument but get runtime error:
//{"Unable to cast object of type 'WS.GeocodeResponse' to type 'System.Xml.XmlDocument'."}
XmlDocument doc = new XmlDocument();
doc = (XmlDocument) method.Invoke();
results = "do something with doc";
//Tried looping through array of results but get error:
// The type or namespace name 'WS' could not be found (are you missing a using directive or an assembly reference?)
foreach(WS.GeocodeResult result in ResultsObj.GetType().Name)
{
continue;
}
//Tried reverse engineering what Visual Studio does when create Web Reference
//thanks to clue at http://www.west-wind.com/presentations/dotnetwebservices/DotNetWebServices.asp
// “That's because the proxy object is actually compiled into the project
//(take a look at \CodeClient\Web References\CodeWebService\CodeWebService.cs
// to see the source for this 'phantom' object that VS generates for you and compiles into your application.”
//Mine was "Project Name"/Web References/"Webservice Name"/Reference.cs
//I.E. ( LatLonVerification/Web References/net.virtualearth.dev/Reference.cs)
// Renamed it and added "using LatLonVerification.VirtualEarthGeo;" reference to it in this class;
//then finally tried to cast it to the new object type but got runtime error:
//{"Unable to cast object of type 'WS.GeocodeResponse' to type 'LatLonVerification.VirtualEarthGeo.GeocodeResponse'."} System.Exception {System.InvalidCastException}
GeocodeResponse geocodeResponse = (GeocodeResponse) ResultsObj;
results = "parse or do something with geocodeResponse";
背景:
我需要更新我们创建的一个 LatLongVerification dll,以便通过 com 从 MS Access 调用 Web 服务。直到几个月前它还在工作。
但是,Bing Web 服务发生了变化,我们的 IT 部门也发生了变化。防火墙变了。因此,在使用 Web References 方法更新 Web 服务后,一切正常,当不在公司网络上但不在公司网络上时。 SoapUI 测试工具也躲在防火墙后面。但是,Open Source Wizdl C# .net 工具确实可以在防火墙后面工作。
因此,这开始了对 Wizdl 工具 (http://wizdl.codeplex.com/ ) 进行逆向工程以作为 dll 而不是 Windows 窗体应用程序工作的目标。我认为各种各样的 Access 和 Excel 人也会喜欢。
当然,为了让它正常工作,我对几个方面进行了硬编码(稍后会修复)。到目前为止,即使在网络上,我也已成功地将数据从 Bing Web 服务返回到对象!然而,我似乎无法进行最终转换或解析或转换,以便将 dll 中的数据作为字符串输出。我是如此接近,因为我可以在本地调试器窗口中看到结果,但是到目前为止它们是不可返回的。
【问题讨论】:
-
是否有理由不只使用“添加服务参考”?
-
我认为该选项与 Web Service Reference 相同。但是,经过一些研究,Service Reference 较新,也许它可能没有防火墙问题。我会试一试。谢谢你的提示!
-
不行,我用新的服务参考方法让它在公司网络外再次工作(在测试模块中需要服务参考才能工作)。但是,在公司网络上,该服务被阻止。使用wireshark,它显示了两种更简单的Web服务/服务方法,尝试通过TCP直接连接,而CustomProxy使用HTTP/XML作为通过公司防火墙的协议,我只是无法读取响应.
-
CustomProxyGenerator 类使用 CodeDom 即动态源代码生成器来创建用于 web 服务调用和回复的类结构。我仍然拥有的问题是读取生成的回复类,该回复类是通过 CodeDom 在程序集中动态确定和创建的。 propertyGrid 控件能够读取结果并且调试器可以看到对象。可以找到显示名为 resultsGrid 的 propertyGrid 类的调试器屏幕截图here。
标签: c# xml web-services xml-serialization codedom