开发者

error when returning an XElement type as an object in a WCF service

开发者 https://www.devze.com 2023-03-19 19:51 出处:网络
Here is my operation contract: [ServiceContract] public interface IService { [OperationContract] object Move();

Here is my operation contract:

[ServiceContract]
public interface IService
{

    [OperationContract]
    object Move();

}

Here is the implementation for the operation contract. I want to return XElement as an object, leaving client to convert the object back to 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;

    }

Here is the client code:

XElement _xmlelem;

ServiceClient sc = new ServiceClient();

_xmlelem = (XElement)sc.Move();

Below is the stack strace of the error message:

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 m开发者_开发问答essage, 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()


You're asking a WCF service to deserialize a .NET type (XElement in your case) to a requesting client and then have the client cast the .NET object type return value to a XElement type. WCF doesn't support that kind of deserialization unless you configure it for .NET serialization by using the NetDataContractSerializer instead of the standard DataContractSerializer.

There are a lot of constraints to using NetDataContractSerializer so generally it is not a good practice. I believe you'd be better off returning straight XML. The answer in this question shows how to deal with XML in a data contract.

0

精彩评论

暂无评论...
验证码 换一张
取 消