Hi,
I am developing an application using ASP.NET web services
I want to keep web Services as the business logic layer so basically my ASP.NET page will call the webservice and the web service will return appropriate data, in some cases I need to return my own class objects but when I try to return an object of some class i get an error
Server Error in '/WebService1' Application.

Returning Objects to UI
jaimlin
The following methods will add all public properties from all base classes of the type you wish to serialize using the XmlSerializer. Pass the returned XmlAttributeOverrides to the Serialize/Deserialize method.
public static XmlAttributeOverrides IgnoreBaseClass(Type type)
{
if(type == null)
throw new ArgumentNullException("type", "Type can't be null"); XmlAttributes attributes = new XmlAttributes();
attributes.XmlIgnore = true;
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
while(type.BaseType != null)
{
PropertyInfo[] properties = type.BaseType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
InsertMemberInfo(properties, overrides, type.BaseType, attributes);
/*
FieldInfo[] fields = type.BaseType.GetFields(BindingFlags.Public | BindingFlags.Instance);
InsertMemberInfo(properties, overrides, type.BaseType, attributes);
*/
type = type.BaseType;
}
return overrides;
}
private static void InsertMemberInfo(MemberInfo[] members, XmlAttributeOverrides overrides, Type type, XmlAttributes attributes) {
foreach(MemberInfo member in members)
{
overrides.Add(type, member.Name, attributes);
}
}
siaj
HI.guy.
the webservice also use XMLSerializer to Serialize the object return value or parameter passed to webmethod.
if the object that would be serialized has any property which is a Interface type. the above error will be throwed.
but , why you want to return component like "SQLConnection" etc.
for WebService pattern. only data object be return. and if there are Interface type property , XmlIgnoreAttribute will be assigned to them to
prevent them being serrialized.
Matt.
daaboots
Along these lines, I have some XML I want to deserialize to objects. The object I want to deserialize to is derived from a Control. I've used the following code:
XmlAttributes attribute = new XmlAttributes();
attribute.XmlIgnore = true;
// Ignore Site property of the control
XmlAttributeOverrides ov = new XmlAttributeOverrides();
ov.Add(typeof(Vehicle), "Site", attribute);
XmlSerializer serializer = new XmlSerializer(typeof(VehicleCollection), ov);
However, I still get the same exception saying "Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface."
littleflsh
Hi Matt,
Thanks a lot for you reply
basically I was trying to understand the essence of a Webservice!
I was thinking of a scenario where I have a Business object calling a web service to return an object that can be used (or perhaps cached) and then used again, so in case if I want to cache a SqlConnection object, can I do it within a webservice (I guess they are stateless)
SQLConnection was just an example, what I was basically looking for was that can I return *any* object from within a webservice.
also If I Implement an Interface within my Webservice class, will I get this error ,
I donot have any properties in my Webservice
Cheers
n3sachde
JustinCReid
HI.
to XmlSerializer , some special property would not be serialize , like interface , circle reference etc.
if it happens, you can use XmlIgnoreAttribute to prevent those properties from being serialized.
but ,if the properties were declared in base type and has not way to access the source code. you can use XmlAttributeOverrides class to override the XmlSerialization action.
Matt.
brohans
hi.
i mean , the property's type is inteface.
Jason D. Camp
NastyMatt
HI. guy.
if you have to get anyobject that have a interface type property. you can use XmlAttributeOverrides to Ignore the Interface type Property.
XmlAttributeOverrides _overrides = new XmlAttributeOverrides();
XmlAttributes _atrs = new XmlAttributes();
_atrs.XmlIgnore = true;
_overrides.Add(typeof(SampleClass),"<theInterfaceProperty>",_atrs);
XmlSerializer _xs = new XmlSerializer(typeof(SampleClass),_overrides);
System.IO.MemoryStream _ms = new System.IO.MemoryStream();
_xs.Serialize(_ms,this);
Matt.
d kretz
can you post a sample of how you did this I am trying to serialize a sqlcommand object.
Dim attribute As XmlAttributes = New XmlAttributesattribute.XmlIgnore = True
Dim XAO As XmlAttributeOverrides = New XmlAttributeOverrides() XAO.Add(GetType(System.ComponentModel.ISite), "System.ComponentModel.Component.Site", attribute) Dim Serializer As New Xml.Serialization.XmlSerializer(GetType(SqlClient.SqlCommand), XAO)I still get the error about not being able to serialize Isite
Saroj K. Nanda
Hi Matt
I am being a little silly here but when you say "Interface Type Property" does that mean a property that is basically an implementation of a property defined in a Interface
Cheers
n3sachde