views:

65

answers:

1

I am Using WCF service to implement my web-service I have problem when I try to call my function which takes URL as input parameter and returns an object class which was defined by me.

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));
    }
}

I have included every class defined by me in the DataContract as I have done in the above class. I am getting the error below mentioned when I am trying to return ClassWS but does not have any error with return Store[] or Address[]

I am getting the error. The error is not returned in the service code but occurs when the retuning the value to proxy.

The underlying connection was closed: The connection was closed unexpectedly. 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.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.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 IFindStore.My_Stores(String URL) at FindStoreClient.My_Stores(String URL) Inner Exception: The underlying connection was closed: The connection was closed unexpectedly. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

I would like to know how to get a datacontract for a class which has another classes as their fields.

Should I write the object to a stream using datacontractserialzer (even though data-contract uses datacontractserializer). Should I be using XmlSerializer?

Thanks in advance

+2  A: 

In general, as long as you are having DataContract + Datamembers for all relevant classes, which you already seem to have, that is all that is needed for the DataContractSerializer to serialize them.

The following

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

is a generic exception that you will commonly see if there is an unhandled exception in your service which is causing the remote connection to be closed.

It is not necessarily something to do with the serialization of your classes.

I would suggest that you step-into your service method while debugging as that will tell you exactly what exception is being thrown and why!

InSane
The service did not cause any error it returned the correct answer. the error occurs after the last "}" of the method. I am thinking it has some violation of contract when it is binding the return value to the proxy. The error occurs even If I return just new ClassWS, public ClassWS My_Stores(String URL) { try { return new ClassWS(); } catch (Exception ex) {} }
vasanth
@vasanth - Assuming that your service used to work is it possible that your service contract has changed recently but the client proxy is not updated? Maybe you can try refreshing your service reference by doing an "Update Service Reference" just to be sure? This kind of error is very much possible in that kind of scenario? This too should be possible to debug by stepping into the proxy class?
InSane
@InSane Error prevailed even when I update Service reference and I tried WCF Test Client in VS2010, It has the same error when I try to invoke the service in WCF Test Client.
vasanth
@vasanth - You tried stepping into the proxy class while debugging? Any clues there? Other than that...i am all out of ideas!! BTW, i have had issues sometimes with "Update Service Reference" - Just open up the client proxy class and verify anyways that it is in sync with your actual DataMember classes
InSane
@InSane: the field "granularity" is an enum, when I changed its attribute to EnumMember, everything worked fine. I thought the attribute EnumMember should be used only when defining and should be used as a DataMember when it is used as a field. But I was wrong. Thanks for your help InSane
vasanth
@vasanth - actually it seems weird to me as well!! My understanding of EnumMember is same as that of yours. I even checked the documentation http://msdn.microsoft.com/en-us/library/aa347875.aspx and even that seeems to agree with that understanding!! A bit weird...!!
InSane
@InSane: I am using Enums in the classes I used as fields, there I have used attribute "DataMember" for the contract. Those Enums seem to be working ok but not in the class that is being returned. May be their contract attribute varies on if the enum is directly referenced or used in another class which used as field
vasanth