类的数据合同,其中字段是用户定义的类本身

时间:2010-10-04 04:11:46

标签: wcf datacontractserializer datacontract

我正在使用WCF服务来实现我的web服务当我尝试调用我的函数时,我遇到了问题,该函数将URL作为输入参数并返回由我定义的对象类。

public class Service: IService<br>
{
    public ClassWS My_Stores(String URL)
    {
        try
        {
            //ClassWS is a class which has other classes like address() defined by me 
            ClassWS My_WS = new ClassWS ();
            return ClsStore.My_Stores(URL);
        }
        catch (Exception ex)
        {}
    }
}

[DataContract]
public class ClassWS 
{
    [DataMember]
    public granularity Granularity;
    [DataMember]
    public address[] Address = new address[5];
    [DataMember]
    public Store[] Stores;
    [DataMember]
    public int Status;

    public ClassWS My_Stores(String URL)
    {
        ClassQuery q = new ClassQuery();
        return (sq.PopulateStores(URL));
    }
}

我在DataContract中包含了我定义的每个类,就像我在上面的类中所做的那样。我在尝试返回ClassWS时收到以下错误,但返回Store []或地址[]

时没有任何错误

我收到错误。该错误不会在服务代码中返回,但会在将值重新调整为代理时发生。

  

基础连接已关闭:   连接已关闭   不料。服务器堆栈跟踪:
  在   System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(引发WebException   webException,HttpWebRequest请求,   HttpAbortReason abortReason)at   System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度   超时)at   System.ServiceModel.Channels.RequestChannel.Request(消息   消息,TimeSpan超时)at   System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息   消息,TimeSpan超时)at   System.ServiceModel.Channels.ServiceChannel.Call(字符串   动作,布尔单向,   ProxyOperationRuntime操作,   Object [] ins,Object [] outs,TimeSpan   超时)at   System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage   methodCall,ProxyOperationRuntime   操作)   System.ServiceModel.Channels.ServiceChannelProxy.Invoke(即时聊天   消息)在[0]处重新抛出异常:
  在   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天   reqMsg,IMessage retMsg)at   System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培;   msgData,Int32类型)at   IFindStore.My_Stores(String URL)at   FindStoreClient.My_Stores(String URL)   内在例外:潜在的   连接已关闭:连接   意外关闭了。在   System.Net.HttpWebRequest.GetResponse()   在   System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度   超时)

我想知道如何为另一个类作为其字段的类获取datacontract。

我是否应该使用datacontractserialzer将对象写入流中(即使data-contract使用datacontractserializer)。我应该使用XmlSerializer吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

一般情况下,只要您拥有所有相关类的DataContract + Datamembers,就像您已经拥有的那样,这就是DataContractSerializer序列化所需的全部内容。

以下

The underlying connection was closed: The connection was closed unexpectedly. 
Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.
ProcessGetResponseWebException (WebException .....

是一个常规异常,您通常会看到服务中是否存在未处理的异常导致远程连接被关闭。

它不一定与您的类的序列化有关。

我建议你在调试时逐步进入你的服务方法,因为它会告诉你究竟抛出了什么异常以及为什么!

相关问题