[英]VS2010 confuses System.Xml.XmlElement with System.Xml.Linq.XElement?
I have created a WCF service with one method which returns a System.Xml.XmlElement
: 我用一个返回System.Xml.XmlElement
方法创建了一个WCF服务:
Interface: 接口:
[ServiceContract]
public interface IWCFService
{
[OperationContract]
XmlElement Execute(...);
}
Service: 服务:
public XmlElement Execute(...)
{
XmlNode node = ...;
return (XmlElement)node;
}
When I try to access the service deployed on my server 当我尝试访问我的服务器上部署的服务时
WCFServiceClient service = new WCFServiceClient("WSHttpBinding_IWCFService");
XmlElement node = service.Execute(...);
I get the error: 我收到错误:
Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement' 无法将类型'System.Xml.Linq.XElement'隐式转换为'System.Xml.XmlElement'
Searching my service solution, I cannot see any reference to System.Xml.Linq.XElement
. 搜索我的服务解决方案,我看不到对System.Xml.Linq.XElement
任何引用。 Is it wrong of me to expect a System.Xml.XmlElement
or is VS 2010 fooling around with me? 期待System.Xml.XmlElement
或VS 2010和我一起System.Xml.XmlElement
我是不对的?
What I have found works the best and easiest for me is to right-click on the service reference and then click on "Configure Service Reference". 我发现的最好和最简单的方法是右键单击服务引用,然后单击“配置服务引用”。 Once in the dialog box for this at the bottom you will see 2 radio buttons that say "Reuse types in all referenced assemblies" or "Reuse types in specified referenced assemblies". 进入底部的对话框后,您将看到2个单选按钮,表示“在所有引用的程序集中重用类型”或“在指定的引用程序集中重用类型”。 Click on the 2nd option for 'specified assemblies'. 单击“指定程序集”的第二个选项。 The listbox will then be enabled and you can check all assemblies and leave System.Xml.Linq unchecked . 然后将启用列表框,您可以检查所有程序集并取消选中 System.Xml.Linq 。 This way every time you update the service reference you will get the expected results and this will be stored in the configuration of the client. 这样,每次更新服务引用时,您都将获得预期的结果,并将其存储在客户端的配置中。
1)从客户端删除system.xml.linq引用2)更新服务引用3)重新生成的Reference.cs文件现在将引用XmlElement而不是XElement
I hate to answer my own question, but here it goes: 我讨厌回答我自己的问题,但在这里:
As far as I can see, the System.Xml.XmlElement
CAN be used as a return type for a WCF service. 据我所知, System.Xml.XmlElement
可以用作WCF服务的返回类型。 The DataContractSerializer
used by default in WCF does support this type and thus it shouldn't be necessary to use the XmlSerializer
. 默认情况下在WCF中使用的DataContractSerializer
确实支持此类型,因此不必使用XmlSerializer
。
However my problem has nothing to do with serializing, but thanks to Raj Kaimal for suggesting this possibility. 然而,我的问题与序列化无关,但感谢Raj Kaimal建议这种可能性。
I followed the WCF tutorials given by Microsoft, which tells you to add the WCF service as a Service Reference when you want to use the service. 我按照Microsoft提供的WCF教程 ,告诉您在要使用该服务时将WCF服务添加为服务引用 。 This seems to be okay in VS2008, but when switching to VS2010 this approach changes the return type from System.Xml.XmlElement
to the (newer?) System.Xml.Linq.XElement
. 这似乎在VS2008中没问题,但是当切换到VS2010时,这种方法将返回类型从System.Xml.XmlElement
更改为(较新的?) System.Xml.Linq.XElement
。 To solve this, you need to add the service as a Web Reference instead. 要解决此问题,您需要将服务添加为Web引用 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.