472,122 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

passing a System.object parameter to a webservice.

Hi,
I'm trying to pass values of different data-types to a web-service. I
thought it would be easier to box these values and pass them as a
System.object parameter, like

[webmethod]
public void webmethod1(object a)

where a can be of type string, DateTime, float, etc..

Now, within this webmethod, I need to know the DataType of the object
received.

My question is, does the XML serialization preserve the datatype of the
System.object parameter. If not, I will have to pass the associated .NET
DataType as a second parameter.

Any suggestions would be greatly apprecaited.

Thank you,
Ravi
Jan 24 '07 #1
8 8764
Ravi,

From my understanding, you want to pass a variety of types and you are
trying to
box inside a custom object. These types are defined as individual proerties
of the custom object like the following
Public Class

Public Property CustomDate as DataTime
Public Propert Name as String
Public Property CostPrice as Float

End Class

XXL Serialization preserves basic types. I mean like string, integer, float
etc.
I'm not sure whether it will preserve DateTime.Just explore it.
It's a good idea to box (wrap) these parameters inside objects and pass
back & forth between services. This becomes really handy if you are trying
to interoperate with J2EE

To conclude the Passing Custom objects is not a restriction in WebServices
Thanks & Regards,
Mark Nelson
"Ravi" wrote:
Hi,
I'm trying to pass values of different data-types to a web-service. I
thought it would be easier to box these values and pass them as a
System.object parameter, like

[webmethod]
public void webmethod1(object a)

where a can be of type string, DateTime, float, etc..

Now, within this webmethod, I need to know the DataType of the object
received.

My question is, does the XML serialization preserve the datatype of the
System.object parameter. If not, I will have to pass the associated .NET
DataType as a second parameter.

Any suggestions would be greatly apprecaited.

Thank you,
Ravi
Jan 25 '07 #2

Thank you Mark.

"Mark Nelson" wrote:
Ravi,

From my understanding, you want to pass a variety of types and you are
trying to
box inside a custom object. These types are defined as individual proerties
of the custom object like the following
Public Class

Public Property CustomDate as DataTime
Public Propert Name as String
Public Property CostPrice as Float

End Class

XXL Serialization preserves basic types. I mean like string, integer, float
etc.
I'm not sure whether it will preserve DateTime.Just explore it.
It's a good idea to box (wrap) these parameters inside objects and pass
back & forth between services. This becomes really handy if you are trying
to interoperate with J2EE

To conclude the Passing Custom objects is not a restriction in WebServices
Thanks & Regards,
Mark Nelson
"Ravi" wrote:
Hi,
I'm trying to pass values of different data-types to a web-service. I
thought it would be easier to box these values and pass them as a
System.object parameter, like

[webmethod]
public void webmethod1(object a)

where a can be of type string, DateTime, float, etc..

Now, within this webmethod, I need to know the DataType of the object
received.

My question is, does the XML serialization preserve the datatype of the
System.object parameter. If not, I will have to pass the associated .NET
DataType as a second parameter.

Any suggestions would be greatly apprecaited.

Thank you,
Ravi
Jan 26 '07 #3
Ravi,

If my answer was useful, please press the 'was this post helpful to you' as
'Yes'.
--
Thanks & Regards,
Mark Nelson
"Ravi" wrote:
>
Thank you Mark.

"Mark Nelson" wrote:
Ravi,

From my understanding, you want to pass a variety of types and you are
trying to
box inside a custom object. These types are defined as individual proerties
of the custom object like the following
Public Class

Public Property CustomDate as DataTime
Public Propert Name as String
Public Property CostPrice as Float

End Class

XXL Serialization preserves basic types. I mean like string, integer, float
etc.
I'm not sure whether it will preserve DateTime.Just explore it.
It's a good idea to box (wrap) these parameters inside objects and pass
back & forth between services. This becomes really handy if you are trying
to interoperate with J2EE

To conclude the Passing Custom objects is not a restriction in WebServices
Thanks & Regards,
Mark Nelson
"Ravi" wrote:
Hi,
I'm trying to pass values of different data-types to a web-service. I
thought it would be easier to box these values and pass them as a
System.object parameter, like
>
[webmethod]
public void webmethod1(object a)
>
where a can be of type string, DateTime, float, etc..
>
Now, within this webmethod, I need to know the DataType of the object
received.
>
My question is, does the XML serialization preserve the datatype of the
System.object parameter. If not, I will have to pass the associated .NET
DataType as a second parameter.
>
Any suggestions would be greatly apprecaited.
>
Thank you,
Ravi
Jan 29 '07 #4
Hi Ravi,

As Mark was saying, basic data types are serialized automatically when
passing them as parameters in a web service method call. What I used to have
dificulties in the past is to pass more complex types as is the ArrayList
data type.

I had to write couple of methods to be able pass this data type in a web
method as follows:
public string SerializeObject(ArrayList obj, System.Type type)
{
XmlSerializer serializer = new XmlSerializer(type);
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
serializer.Serialize(writer,obj);
writer.Close();
return sb.ToString();
}
public ArrayList DeserialiseObject(string root, System.Type type, string
strReader)
{
XmlRootAttribute xmlRoot = new XmlRootAttribute();
xmlRoot.ElementName = root;
XmlSerializer serializer = new XmlSerializer(type,xmlRoot);
XmlReader reader = new XmlTextReader(new StringReader(strReader));
return (ArrayList)serializer.Deserialize(reader);
}
Hope that helps.
Best regards,
Eugen
"Mark Nelson" wrote:
Ravi,

If my answer was useful, please press the 'was this post helpful to you' as
'Yes'.
--
Thanks & Regards,
Mark Nelson
"Ravi" wrote:

Thank you Mark.

"Mark Nelson" wrote:
Ravi,
>
From my understanding, you want to pass a variety of types and you are
trying to
box inside a custom object. These types are defined as individual proerties
of the custom object like the following
>
>
Public Class
>
Public Property CustomDate as DataTime
Public Propert Name as String
Public Property CostPrice as Float
>
End Class
>
XXL Serialization preserves basic types. I mean like string, integer, float
etc.
I'm not sure whether it will preserve DateTime.Just explore it.
>
>
It's a good idea to box (wrap) these parameters inside objects and pass
back & forth between services. This becomes really handy if you are trying
to interoperate with J2EE
>
To conclude the Passing Custom objects is not a restriction in WebServices
Thanks & Regards,
Mark Nelson
>
>
"Ravi" wrote:
>
Hi,
I'm trying to pass values of different data-types to a web-service. I
thought it would be easier to box these values and pass them as a
System.object parameter, like

[webmethod]
public void webmethod1(object a)

where a can be of type string, DateTime, float, etc..

Now, within this webmethod, I need to know the DataType of the object
received.

My question is, does the XML serialization preserve the datatype of the
System.object parameter. If not, I will have to pass the associated .NET
DataType as a second parameter.

Any suggestions would be greatly apprecaited.

Thank you,
Ravi
Jan 31 '07 #5
Hello Eugen,

could you please explain the following line
xmlRoot.ElementName = root

i don't know which root is this.

i am assuming strReader is the serialized string you returned with the
SerializeObject function. am i right or wrong?

thanks beforehand
Jorge

Mar 7 '07 #6
Hi Jorge,

Sorry, haven't read thes posts for some time:

strList1 = SerializeObject(objList1,typeof(ArrayList));

ArrayList objList1=
(ArrayList)DeserialiseObject("ArrayOfAnyType",type of(ArrayList), strList1);

Eugen
"mo*********@hotmail.com" wrote:
Hello Eugen,

could you please explain the following line
xmlRoot.ElementName = root

i don't know which root is this.

i am assuming strReader is the serialized string you returned with the
SerializeObject function. am i right or wrong?

thanks beforehand
Jorge

Mar 15 '07 #7
Hello Eugen,

i got the serialization and deserialization to work Thanks.

Now i need to implement a transaction. i think i will not be able to
accomplish the following using web services.
Scenario:
the code behind web_page_1 Calls webservice_1 and starts transaction.
the results of webservice_1 are used by webservice_2.
if THERE_IS_AN_ERROR then
rollback transaction
else
commit transaction
the results of webservice2 are displayed in web_page_2.

thanks for any input.
Jorge

Mar 20 '07 #8
Hi Jorge,

There is no problem with creating a transaction from a web service,
commiting it or rolling it back. I am doing this using a database class I
have created. The constructor of this class has a parameter for stating a
transaction. I am using the "using" keyword when creating a new instance of
that class. Whenever this object goes out of scope, I have code in the
Dispose method to commit ot rollback the transaction based on a flag that
tells me if the statement executes successfully or not.

Something like that:
protected virtual void Dispose(bool disposing)
{
if(!is_disposed)
{
if(disposing)
{
}

if(m_bStartTransaction == true)
{
myTrans = m_SQLCommand.Transaction;
if (m_bError == true)
{
myTrans.Rollback();
}
else
{
myTrans.Commit();
}
}
m_SQLConnection.Close();
}

this.is_disposed = true;
}

--function call inside service1.asmx.cs

try
{
using(DatabaseClass dbObj = new DatabaseClass(m_strConnectionString,
true,"prApp_IsUserOwnerOfAnyPTG",false))
{
dbObj.BindStringParameter(strLANID,"@User_LAN_ID") ;
dbObj.BindStringParameter(strObjectName,"@Frmwk_co de");

nCount = dbObj.BindStoredProcedureOutputIntParameterAndExec ute("@nCount");
}
}
catch (SoapException se)
{
throw(se);
}
catch(SqlException mySqlEx)
{
strErrorMessage = mySqlEx.Source + ":" + mySqlEx.Message;
throwException(strErrorMessage,"",mySqlEx.Number,m _strModuleName,m_strClassName,
strFunctionName, 1243, 2, "Failed to update the database. Please contact the
system administrator");
}
catch(Exception myException)
{
strErrorMessage = myException.Source + ":" + myException.Message;
throwException(strErrorMessage, "", 0, m_strModuleName, m_strClassName,
strFunctionName, 1243, 2, "Failed to update the database. Please contact the
system administrator");
}
finally
{
}

Your intention is to use two separate web services to work in the context of
the same transaction in the database?

Eugen

"mo*********@hotmail.com" wrote:
Hello Eugen,

i got the serialization and deserialization to work Thanks.

Now i need to implement a transaction. i think i will not be able to
accomplish the following using web services.
Scenario:
the code behind web_page_1 Calls webservice_1 and starts transaction.
the results of webservice_1 are used by webservice_2.
if THERE_IS_AN_ERROR then
rollback transaction
else
commit transaction
the results of webservice2 are displayed in web_page_2.

thanks for any input.
Jorge

Mar 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Stan | last post: by
4 posts views Thread by Sahil Malik [MVP] | last post: by
reply views Thread by Steve | last post: by
22 posts views Thread by Arne | last post: by
1 post views Thread by Deecrypt | last post: by
9 posts views Thread by Greger | last post: by
8 posts views Thread by amazon | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.