错误WCF服务
问题描述:
返回一个类型的XElement作为对象的时候,这里是我的经营合同:错误WCF服务
[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();
下面是错误消息的堆栈strace的:
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()
答
您正在要求WCF服务将.NET类型(您的案例中的XElement)反序列化为请求客户端,然后让客户端将.NET对象类型返回值转换为XElement类型。除非您通过using the NetDataContractSerializer而不是标准DataContractSerializer将它配置为.NET序列化,否则WCF不支持这种反序列化。
使用NetDataContractSerializer有很多限制,所以通常不是一个好的做法。我相信你最好直接返回XML。答案in this question显示了如何在数据合同中处理XML。
你能否详细说明如何在配置文件中配置NetDataContractSeriliazer? –
向OperationContract添加[ServiceKnownType(typeof(XElement))]之后,我得到下面的错误 - 是否响铃? 将与'XElement'对应的类型添加到已知类型的列表中 - 例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型的列表中 –
我已经运行的唯一示例位于[在WF_WCF_Samples \ WCF \ Basic \ Contract \ Data \ NetDCSasDCSwithDCR \ CS文件夹中的解决方案中的WCF 4 MSDN示例](http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=21459)。您还需要实现标准序列化程序的[OperationBehavior for swapping out](http://forums.silverlight.net/forums/p/214152/507252.aspx)。所有这些工作都需要你暂停一下,因为你只需要通过电线移动一些XML :) –