Why am I getting a compile error on a referenced Web Service parameter

R

Ray Stevens

I have a simple method that is using a complex data class created by a
mainframe interface code generator. I compiled this into a Business Entities
project and referenced it in both the Web Service and the client, so it
should be identical in both.

A sample abrivation of the class is of the following form:

namespace SoftwareAG.EntireX.NETWrapper.Generated.tsrsubi1.Groups
{
public class Tsrsubi1
{
public class Xormsga2
{
[SendAs(IdlType.A, Length=3f, Trim=true)]public string
environment ;
[SendAs(IdlType.A, Length=3f, Trim=true)]public string sourceApp
;
[SendAs(IdlType.A, Length=8f, Trim=true)]public string
destinationApp ;
// etc
public class OrderHeader
{
[SendAs(IdlType.A, Length=12f, Trim=true)]public string
orderNo ;
[SendAs(IdlType.A, Length=5f, Trim=true)]public string
custId ;
[SendAs(IdlType.A, Length=7f, Trim=true)]public string
unitId ;
// etc
}
// etc
}
}
}

The test Web Service looks as follows:

using SoftwareAG.EntireX.NETWrapper.Generated.tsrsubi1.Groups;

[WebService(Namespace = "http://company.com/webservices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TSRTxns : System.Web.Services.WebService
{
[WebMethod]
public void ProcessTsrOrder(ref Tsrsubi1.Xormsga2 etx)
{
TSRBLL bll = new TSRBLL();
bll.ProcessTsrOrder(ref etx);
}
}

The test client is referencing the web service as:

using SoftwareAG.EntireX.NETWrapper.Generated.tsrsubi1.Groups;

namespace WebServiceTest
{
public partial class Form1 : Form
{
private void btnTest_Click(object sender, EventArgs e)
{
Tsrsubi1.Xormsga2 etx = new Tsrsubi1.Xormsga2();
etx.orderHeader.orderNo = "12321";
etx.clientTransactioId = "tran01";
etx.orderHeader.custId = "11111";
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
txns.ProcessTsrOrder(ref etx); // <<<< COMPILE ERROR HERE
txtMessage.Text = etx.messageText.ToString();
}
}
}

I am getting the following compile errors for some reason and don't know
why:

Error 1 The best overloaded method match for
'WebServiceTest.TSRWS.TSRTxns.ProcessTsrOrder(ref
WebServiceTest.TSRWS.Xormsga2)' has some invalid arguments C:\Documents and
Settings\bmattox\My Documents\Visual
Studio\Projects\TSRWS\WebServiceTest\Form1.cs 29 13 WebServiceTest
Error 2 Argument '1': cannot convert from 'ref
SoftwareAG.EntireX.NETWrapper.Generated.tsrsubi1.Groups.Tsrsubi1.Xormsga2'
to 'ref WebServiceTest.TSRWS.Xormsga2' C:\Documents and Settings\bmattox\My
Documents\Visual Studio\Projects\TSRWS\WebServiceTest\Form1.cs 29 38
WebServiceTest

Anyone have any suggestions?
 
J

Josh Twist

Because a web service leaves the appdomain (and process) boundary you
can't pass variables by reference (using the 'ref' keyword).

Therefore, when the proxy (client code) is generated it doesn't contain
the ref keyword and therefore there is no matching overload.

I have to say, I'm slightly surprised that the Web Service itself
doesn't throw an error when you try to access the help page or WSDL.

HTH

Josh
http://www.thejoyofcode.com/
 
R

Ray Stevens

I think I am missing something basic. I tried a simple test passing
StringBuilder with no ref and get the same error:

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

private void btnTest_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
sb = txns.ProcessTsrOrder(sb);
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
 
J

Josh Twist

Your example using StringBuilder is an interesting one - the
StringBuilder isn't designed to be serialized so the WSDL generator
creates it's own 'version' of sorts. You need to use that version
rather than the StringBuilder in System.Text in your web service
client. Weird I know, but try this it will work.

private void btnTest_Click(object sender, EventArgs e)
{
TSRWS.StringBuilder sb = new TSRWS.StringBuilder(); // it will
compile now, but this isn't a real stringbuilder so it won't work like
one.
TSRWS.TSRTxns txns = new TSRWS.TSRTxns();
sb = txns.ProcessTsrOrder(sb);
txtMessage.Text = sb.ToString();
}

There are a number of things going on here so bear with me whilst I try
to explain.

1. You can't send StringBuilders over web services. They must be used
within the same process they are created.

2. Getting to understand XmlSerialization is key to this whole process.
In order to send a class from one application to another (potentially
written in an entirely different language on a different platform -
thats the idea behind web services) the framework must change that
class/type into a format that can be sent in a (SOAP) message. To
achieve this in .NET web services, the framework uses the XmlSerializer
which serializes (converts) your class/types into XML sends it across
the wire. The other application then deserializes this XML back into
classes/types.

So - you can't pass things by reference. If you pass something by
reference your passing it by a pointer to memory - but how could the
other application (probably on a different computer) access the memory
of this application? It just doesn't make sense. And you can't pass an
*actual* System.Text.StringBuilder either, that contains heaps of
functionality, streams etc.

Try this example service instead as a working example

public class Employee
{
public string Name;
public int Age;
public string Email;
}

[WebMethod]
public Employee ReturnTheSameEmployee(Employee emp)
{
return emp;
}

Try that and let me know how you get on - in this case you'll be passed
back an exact copy of the employee object you pass in to the service.
Have a search on google for XmlSerializer and web services - there are
lot tutorials and introductions out there. If you need any more help -
just shout. Good luck!
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top