المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Custom Classes in web services



C# Programming
05-05-2009, 06:42 PM
Hi all,

I have a problem with custom classes in web services. The scenario is as follows:

I have created custom classes for use with my environment. We will take as an example, class student and class teacher which are in the package MyPackage.Data. These are being serialised at ******** A, passed to the web service and then sent to ******** B. While at the web service, the object is deserialised in order to extract info about the object to keep a log of what is going through. When the object is deserialised in the web service and then reserialised to send to ******** B (this also happens when creating a new object at the web service and sending it out to a ********), the type of the object turns into MyWebservice.Student instead of MyPackage.Data.Student.

I get exceptions like: "Cannot cast object of type MyWebservice.Student to MyPackage.Data.Student".

I am guessing this is being taken from the reference file which is autogenerated on instantiation of a web service. An example of this auto generated code is shown below for the Student class


///
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="MyPackage.Date")]
public partial class Student{

private string Name;

///
public string Name{
get {
return this.name;
}
set {
this.name= value;
}
}

}


Now for this I used a work around, writing an alternate constructor for each class which takes an object of type MyWebservice.Student for example and creates a new instance of of MyPackage.Data.Student and sets all attributes to the attributes of the MyWebservice.Student object. An example is shown below:


public Starter(MyWebservice.Student webStudent)
{
this.name = webStudent.Name;
}


This works but is neither pleasant nor viable as a final solution. Can anybody please shed any light on how not to use the generated code of the web service or remove it completely or why this is happening.

Thanks a lot for your time and cooperation, any help is highly appreciated