How to return class with simple type props from Web Service?

D

Derrick

I have the below simple class -

[Serializable()]
public class MyClass
{
protected string m_stName;
protected string m_stCode;
protected int m_iId;

public MyClass() {}

public string Name { /* get and set */ }
public string Code { /* get and set */ }
public int Id { /* get and set */ }
}

And a simple web service project with one web method -

[WebMethod]
public MyClass MyClass()
{
MyClass retVal = new MyClass();
retVal.Name = "My Class";
retVal.Id = 0;
retVal.Code = "MC 0";
return retVal;
}

When I try debugging (simple debug using ie as client in VS, maybe that is
the problem?), I get...
A first chance exception of type 'System.InvalidOperationException' occurred
in system.xml.dll

Additional information: The XML element named 'MyClass' from namespace
'http://tempuri.org/' references a method and a type. Change the method's
message name using WebMethodAttribute or change the type's root element
using the XmlRootAttribute.
<<

If I change the web method name to "getClass" so it isn't the same name as
the return type, I get...
A first chance exception of type 'System.ArgumentException' occurred in
mscorlib.dll

Additional information: Error binding to target method.
<<

Anyone have a link to a good example of returning a custom class with web
services? Been googling, haven't found one yet.

Thanks in advance!

Derrick
 
D

Derrick

Well, this code actually works fine if I create a stand alone solution. If
I create a new c# web service project and add it to a solution containing a
C# windows forms app and C# class library projects, then I get the error,
even with the simple HelloWorld method that is stock in new projects?

Anyone ever see that before?

Thanks!

Derrick


Derrick said:
I have the below simple class -

[Serializable()]
public class MyClass
{
protected string m_stName;
protected string m_stCode;
protected int m_iId;

public MyClass() {}

public string Name { /* get and set */ }
public string Code { /* get and set */ }
public int Id { /* get and set */ }
}

And a simple web service project with one web method -

[WebMethod]
public MyClass MyClass()
{
MyClass retVal = new MyClass();
retVal.Name = "My Class";
retVal.Id = 0;
retVal.Code = "MC 0";
return retVal;
}

When I try debugging (simple debug using ie as client in VS, maybe that is
the problem?), I get...
A first chance exception of type 'System.InvalidOperationException' occurred
in system.xml.dll

Additional information: The XML element named 'MyClass' from namespace
'http://tempuri.org/' references a method and a type. Change the method's
message name using WebMethodAttribute or change the type's root element
using the XmlRootAttribute.
<<

If I change the web method name to "getClass" so it isn't the same name as
the return type, I get...
A first chance exception of type 'System.ArgumentException' occurred in
mscorlib.dll

Additional information: Error binding to target method.
<<

Anyone have a link to a good example of returning a custom class with web
services? Been googling, haven't found one yet.

Thanks in advance!

Derrick
 

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