Can an object be passed to a WebMethod?

R

Ray Stevens

I have a C# dataclass object that I would like to pass to a Web Service
WebMethod, modify some strings, and return it to the caller but I am getting
compiler errors in the client. I'm not an expert on Web Services and am
wondering if what I am attempting is even doable?

Even this simple test generates the error:

From the Web Service:

[WebMethod]
public StringBuilder ProcessTsrOrder(StringBuilder sb)
{
return sb.Append("Hello World!");
}

From the test client:

private void btnTest_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
sb = txns.ProcessTsrOrder(sb); //<<< compiler error here
txtMessage.Text = sb.ToString();
}

Error 1 The best overloaded method match for
'WebServiceTest.TSRWS.TSRTxns.ProcessTsrOrder(WebServiceTest.TSRWS.StringBuilder)'
has some invalid arguments C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 18
WebServiceTest
Error 2 Argument '1': cannot convert from 'System.Text.StringBuilder' to
'WebServiceTest.TSRWS.StringBuilder' C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 39
WebServiceTest
 
N

Nicholas Paldino [.NET/C# MVP]

Ray,

Yes, you can pass objects between web methods. However, I wouldn't
recommend passing something like a StringBuilder. I would just pass a
string. In reality, you are gaining nothing from passing the StringBuilder.
Your method is taking a StringBuilder and returning one. Instead, just pass
strings.

The reason you are getting this error is that the WSDL is exposing a
type definition for StringBuilder, and when you create a proxy to that
method, you are getting that definition of StringBuilder, not the one that
is exposed in the framework. It is for this reason that you can not perform
the cast.

You could use the StringBuilder type that is exposed by the web service,
but that doesn't gain you anything either, since that will only expose
properties, and not the methods on the StringBuilder.

You should not expect to pass functional objects around through web
services. Rather, if you want to send objects around, you should think of
sending pieces of information around, which have no inherent methods on them
to perform operations on them.

Hope this helps.
 
R

Ray Stevens

The link helped with my immediate problem, but then I encountered another.
We are using a layered architecture. The web service calls down to a
Business Logic Layer which, in turn, calls down to a Data Access Layer. With
the information provided in the link, I am now able to reference the data
class in the client and successfully call the Web Service. However, I am
unable to pass it down to the BLL/DAL layers for the same reason. (I.e., I
get the identical compile error as before)

Dale said:
Nicholas is right, of course, about the StringBuilder class. This might
help
you understand what you can pass to a web service and how.

http://www.dalepreston.com/Blog/2005/02/returning-custom-classes-from-web.html
--
Dale Preston
MCAD C#
MCSE, MCDBA


Ray Stevens said:
I have a C# dataclass object that I would like to pass to a Web Service
WebMethod, modify some strings, and return it to the caller but I am
getting
compiler errors in the client. I'm not an expert on Web Services and am
wondering if what I am attempting is even doable?

Even this simple test generates the error:

From the Web Service:

[WebMethod]
public StringBuilder ProcessTsrOrder(StringBuilder sb)
{
return sb.Append("Hello World!");
}

From the test client:

private void btnTest_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
sb = txns.ProcessTsrOrder(sb); //<<< compiler error here
txtMessage.Text = sb.ToString();
}

Error 1 The best overloaded method match for
'WebServiceTest.TSRWS.TSRTxns.ProcessTsrOrder(WebServiceTest.TSRWS.StringBuilder)'
has some invalid arguments C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 18
WebServiceTest
Error 2 Argument '1': cannot convert from 'System.Text.StringBuilder' to
'WebServiceTest.TSRWS.StringBuilder' C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 39
WebServiceTest
 
G

Guest

Are the business layer and data layer on the same machine and in the same
application as the web service?

If so, it is still likely to be a namespace issue. The same techniques
would still work.

This link may help, too:

http://msdn.microsoft.com/library/d...ide/html/cpconhowruntimelocatesassemblies.asp

--
Dale Preston
MCAD C#
MCSE, MCDBA


Ray Stevens said:
The link helped with my immediate problem, but then I encountered another.
We are using a layered architecture. The web service calls down to a
Business Logic Layer which, in turn, calls down to a Data Access Layer. With
the information provided in the link, I am now able to reference the data
class in the client and successfully call the Web Service. However, I am
unable to pass it down to the BLL/DAL layers for the same reason. (I.e., I
get the identical compile error as before)

Dale said:
Nicholas is right, of course, about the StringBuilder class. This might
help
you understand what you can pass to a web service and how.

http://www.dalepreston.com/Blog/2005/02/returning-custom-classes-from-web.html
--
Dale Preston
MCAD C#
MCSE, MCDBA


Ray Stevens said:
I have a C# dataclass object that I would like to pass to a Web Service
WebMethod, modify some strings, and return it to the caller but I am
getting
compiler errors in the client. I'm not an expert on Web Services and am
wondering if what I am attempting is even doable?

Even this simple test generates the error:

From the Web Service:

[WebMethod]
public StringBuilder ProcessTsrOrder(StringBuilder sb)
{
return sb.Append("Hello World!");
}

From the test client:

private void btnTest_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
sb = txns.ProcessTsrOrder(sb); //<<< compiler error here
txtMessage.Text = sb.ToString();
}

Error 1 The best overloaded method match for
'WebServiceTest.TSRWS.TSRTxns.ProcessTsrOrder(WebServiceTest.TSRWS.StringBuilder)'
has some invalid arguments C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 18
WebServiceTest
Error 2 Argument '1': cannot convert from 'System.Text.StringBuilder' to
'WebServiceTest.TSRWS.StringBuilder' C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 39
WebServiceTest
 
R

Ray Stevens

They are on the same machine but the same application. Each layer is in a
separate project.

BTW, this does not appear to be a problem with .NET 1.1 for some reason. In
fact, I am even able to use ref in the Web Service call, make changes in the
web service, and reflect them in the client. Something seems to have been
drastically changed with .NET 2.0.

Dale said:
Are the business layer and data layer on the same machine and in the same
application as the web service?

If so, it is still likely to be a namespace issue. The same techniques
would still work.

This link may help, too:

http://msdn.microsoft.com/library/d...ide/html/cpconhowruntimelocatesassemblies.asp

--
Dale Preston
MCAD C#
MCSE, MCDBA


Ray Stevens said:
The link helped with my immediate problem, but then I encountered
another.
We are using a layered architecture. The web service calls down to a
Business Logic Layer which, in turn, calls down to a Data Access Layer.
With
the information provided in the link, I am now able to reference the data
class in the client and successfully call the Web Service. However, I am
unable to pass it down to the BLL/DAL layers for the same reason. (I.e.,
I
get the identical compile error as before)

Dale said:
Nicholas is right, of course, about the StringBuilder class. This
might
help
you understand what you can pass to a web service and how.

http://www.dalepreston.com/Blog/2005/02/returning-custom-classes-from-web.html
--
Dale Preston
MCAD C#
MCSE, MCDBA


:

I have a C# dataclass object that I would like to pass to a Web
Service
WebMethod, modify some strings, and return it to the caller but I am
getting
compiler errors in the client. I'm not an expert on Web Services and
am
wondering if what I am attempting is even doable?

Even this simple test generates the error:

From the Web Service:

[WebMethod]
public StringBuilder ProcessTsrOrder(StringBuilder sb)
{
return sb.Append("Hello World!");
}

From the test client:

private void btnTest_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
sb = txns.ProcessTsrOrder(sb); //<<< compiler error here
txtMessage.Text = sb.ToString();
}

Error 1 The best overloaded method match for
'WebServiceTest.TSRWS.TSRTxns.ProcessTsrOrder(WebServiceTest.TSRWS.StringBuilder)'
has some invalid arguments C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 18
WebServiceTest
Error 2 Argument '1': cannot convert from 'System.Text.StringBuilder'
to
'WebServiceTest.TSRWS.StringBuilder' C:\Documents and
Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 25 39
WebServiceTest
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top