这是我的操作合同:
[ServiceContract]
public interface IService
{
[OperationContract]
object Move();
}
这是操作合同的实现。我想将XElement作为对象返回,让客户端将对象转换回XElement
public object Move()
{
object _x;
var xmlTree1 = new XElement("Root",
new XElement("Child", 1),
new XElement("Child", 2),
new XElement("Child", 3),
new XElement("Child", 4),
new XElement("Child", 5),
new XElement("Child", 6)
);
var xmlTree2 = new XElement("Root",
from el in xmlTree1.Elements()
where ((int) el >= 3 && (int) el <= 5)
select el
);
_x = xmlTree2;
return _x;
}
以下是客户端代码:
XElement _xmlelem;
ServiceClient sc = new ServiceClient();
_xmlelem = (XElement)sc.Move();
以下是错误消息的堆栈:
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at ConsoleApplication1.ServiceReference1.IService.Move()
at ConsoleApplication1.ServiceReference1.ServiceClient.Move() in C:\Delete\ConsoleApplication1\Service References\ServiceReference1\Reference.cs:line 128
at ConsoleApplication1.Program.Main(String[] args) in C:\Delete\ConsoleApplication1\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:2)
您要求WCF服务将.NET类型(在您的情况下为XElement)反序列化为请求客户端,然后让客户端将.NET对象类型返回值强制转换为XElement类型。除非您通过using the NetDataContractSerializer而不是标准的DataContractSerializer为.NET序列化配置它,否则WCF不支持这种反序列化。
使用NetDataContractSerializer有很多限制,所以一般来说这不是一个好习惯。我相信你最好直接返回XML。答案in this question显示了如何在数据协定中处理XML。