2011-07-13 155 views
1

返回一個類型的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() 

回答

2

您正在要求WCF服務將.NET類型(您的案例中的XElement)反序列化爲請求客戶端,然後讓客戶端將.NET對象類型返回值轉換爲XElement類型。除非您通過using the NetDataContractSerializer而不是標準DataContractSerializer將它配置爲.NET序列化,否則WCF不支持這種反序列化。

使用NetDataContractSerializer有很多限制,所以通常不是一個好的做法。我相信你最好直接返回XML。答案in this question顯示瞭如何在數據合同中處理XML。

+0

你能否詳細說明如何在配置文件中配置NetDataContractSeriliazer? –

+0

向OperationContract添加[ServiceKnownType(typeof(XElement))]之後,我得到下面的錯誤 - 是否響鈴? 將與'XElement'對應的類型添加到已知類型的列表中 - 例如,通過使用KnownTypeAttribute屬性或將其添加到傳遞給DataContractSerializer的已知類型的列表中 –

+0

我已經運行的唯一示例位於[在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 :) –