3

I am bit curious about one thing which has happen while trying to understand the concept of Service References and Web Service References.

What I did is?

In my project I have added a web service as a Service Reference and trying to get my script run through the use of client. But while getting result it is throwing an exception as in the following image:

Exception while running the script

I have tried to trace out the cause but not able to get the proper answer for that. I have following code for the resultant object.

[
        ComVisible(false), 
        Serializable,
        SoapTypeAttribute("RecordList", "http://www.someadd.com/dev/ns/SOF/2.0"),       
        XmlType(TypeName="RecordList", Namespace="http://www.someadd.com/dev/ns/SOF/2.0")       
    ]
    public class MyRecordListWrapper
    {
        private IxRecordList recordList = null;
        private const string XMLW3CSchema = "http://www.w3.org/2001/XMLSchema";

        [SoapElement("Headers")]
        public Header[] Headers = null;
        [SoapElement("Records")]
        public Record[] Records = null;
        // some methods to work on intialization
        public SmRecordListWrapper(ref IxRecordList p_RecordList)
        {
            recordList = p_RecordList;// record list initialization             
            Headers = CreateWrapperHeaders(); // will return header class object
            Records = CreateWrapperRecords(); // will return record object
        }   
    }

Can anyone tell me why this error is showing for me?

While adding reference as a Web Service Reference when I add the same reference as a web reference that time the program is not showing any error and runs successfully?

So can anyone tell me what is the difference in working with the same code using service reference and web service reference? and Which is a correct way to ass references?

Hope I will get some more described answers to make the things easy to understand.

Thanks in advance.

A B
  • 1,461
  • 2
  • 19
  • 54
  • 1
    See this similar question: http://stackoverflow.com/questions/2158106/web-reference-vs-service-reference – Oscar Jul 13 '15 at 07:06
  • @Oscar Thanks for the suggestion but it still not clear to me why and how I can use my code as `service reference`? and why the error is showing while working with `service references`? – A B Jul 13 '15 at 08:10
  • Is IxRecordList an interface? Is yes, where it's bee assigned? Have you used the KnownType attribute somewhere else in your code? https://msdn.microsoft.com/en-us/library/ms751512(v=vs.110).aspx – Oscar Jul 13 '15 at 08:11
  • @Oscar I have updated the code please check. :) and for the KnowntypeAttributes I am using the SoapElements and SoapTypeAttributes for XMLSerialization – A B Jul 13 '15 at 08:45
  • Not sure, but I think that as WCF is transport independent, you should use the KnownTypes attribute instead of SoapAttribute. WCF can operate in other scenarios than Soap, so you should not be tight to xml serialization. – Oscar Jul 13 '15 at 09:11

1 Answers1

1

Adding a web reference, visual studio uses xsd.exe to generate the classes from the service metadata. This uses XmlSerializer under the hood.

Adding a service reference, visual studio uses svcutil.exe to generate the classes from the metadata. This uses DataContractSerializer under the hood.

Two separate tools, two outcomes. For general information, DataContractSerializer is a lot less forgiving when it comes to generating classes from metadata.

tom redfern
  • 30,562
  • 14
  • 91
  • 126