How to return custom objects using WebServices

[Problem]
Having created a shared library that will be deployed on the server machine and on the client computer, how can I establish a connection between the client server and the classes provided by the library?

Passing information through web services does not work because the serialized object returned by the web service is a webservice class that cannot be converted to a shared library.

Am I using web services incorrectly? Is there a better way?

[Example]

MyLibrary.cs and SubLibrary.cs are in a common assembly that should be used by the client application.

Mylibrary.cs

public class MyLibrary
{
    private SubLibrary sublib = new SubLibrary();

    public class MyLibrary()
    {
    }

    public string GetValue()
    {
        return sublib.GetValue();
    }
}

SubLibrary.cs

public class SubLibrary
{
    private string str = "Hello World";

    public SubLibrary()
    {
    }

    public string GetValue()
    {
        return str;
    }
}

WebService.asmx.cs

[WebMethod]
public MyLibrary GetInfo()
{
    return new MyLibrary();
}

Client application

private void GetInfo_Click(object sender, System.EventArgs e)
{
  WS.WebService services = new WS.WebService();

  MyLibrary info = services.GetInfo();  // This of course doesn't convert.

  MessageBox.Show(info.GetValue());
}
+3
3

, 1.1 ( 2.0). ( ) WCF (.NET 3.0). , 1.1, "" ; ( "sharetypes"?), , IIRC.

# 1.2, , , , .

( ):

  • ,
  • ditto, XmlSerializer ( , )
  • WCF ( )
+5

- , , . SubLibrary IXmlSerializable.

public class SubLibrary : IXmlSerializable 
{
    private string str = "Hello World";

    public SubLibrary()
    {
    }

    public string GetValue()
    {
        return str;
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        //...
    }

    public void WriteXml(XmlWriter writer)
    {
        //...
    }
}

: MyLibrary, SubLibrary WS, , . , MyLibrary SubLibrary, MyLibrary SubLibrary , . , SubLibrary MyLibrary, , - SubLibrary. , :

[WebMethod]
public SubLibrary GetInfo()
{

    return new Sublibrary();
}

:

private void GetInfo_Click(object sender, System.EventArgs e)
{
  WS.WebService services = new WS.WebService();

  SubLibrary info = services.GetInfo();  // This of course doesn't convert.

  MessageBox.Show(info.GetValue());
}

- : XML, , ( -) .

+1

, - . - , , - .

, , :

WS.WebService services = new WS.WebService();

// Use client side proxy in same namespace as generated web reference
WS.MyLibrary info = services.GetInfo();


, - ( ), , , . .

This may be your example, but your MyLibrary class has no data to return. The reason is that the XmlSerializer (used by the runtime to convert between XML and CLR objects) only serializes the public properties and fields of the object. MyLibrary and SubLibrary do not display any properties or public fields, so no data is serialized. See XML and SOAP Serialization for more information.

0
source

Source: https://habr.com/ru/post/1718432/


All Articles