Solution for using custom classes in WebService [from www.bitscn.com]

Source: Internet
Author: User

The so-called custom class does not know whether it is clearly expressed. Here it refers to the model-layer entity class in petshop.
For example, the following code:
Using system;
Using system. collections;
Using system. Collections. Generic;
Using system. text;

Namespace Model
{
[Serializable]
Public class student
{

Private string stuname;

Public student ()
{}
Public String stuname
{
Get {return this. stuname ;}

Set {This. stuname = value ;}

}
}
}

Content transmitted by WebService must be serializable, whether it is a parameter or a return value. The entity class student defined above indicates [serializable] before the class definition to indicate serializable. However, when an entity class set is involved, if ilist <student> is used for representation, an error occurs because ilist cannot be serialized. In this case, we can use system. collections. objectmodel. collection <student> to indicate a collection of object classes. There are two possible entity classes and entity classes. The following describes the solutions:

1. The object class set is passed as object.
In this case, we must use the entity class in WebService to pass the object [] corresponding to the object class set. The parameter type of the method in WebService is arraylist.
For example, the method in WebService is: [xmlinclude (typeof (student)]
[Webmethod]
Public String hellostus (arraylist stulist)

{

Bll. class1 CLS = new BLL. class1 ();
Return Cls. getname (stulist );
} Do not miss the [xmlinclude (typeof (student)] line. Otherwise, the entity class in WebService will not be referenced in the presentation layer.
At this time, add a web reference in the presentation layer. The calling code in the presentation layer is as follows: (refer to the button#click () method in the demo)
Network Management ujia u.bitscn.com

/// <Summary>
/// You must use the entity class in WebService to pass the object class set as object []. The parameter type in WebService is arraylist, and provides a public class for converting a set into object [].
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button#click (Object sender, eventargs E)
{
String STR = "";
Localhost. Student stuinfo1 = new localhost. Student ();
Stuinfo1.stuname = "lxinxuan ";
Localhost. Student stuinfo2 = new localhost. Student ();
Stuinfo2.stuname = "www.cnblogs.com/lxinxuan ";
Ilist <localhost. Student> stulist = new list <localhost. Student> ();
Stulist. Add (stuinfo1 );
Stulist. Add (stuinfo2 );
Object [] array = This. converttoarray <localhost. Student> (stulist); // This is a generic method that converts a set to objec [].
STR = Ser. hellostus (array); // pass object []. The returned value is the value of stuname.

 

MessageBox. Show (STR );
}
// This is a generic method that converts a set to objec [].
Private object [] converttoarray <t> (ilist <t> tlist)
{
Object [] array = new object [tlist. Count];
Int I = 0;
Foreach (t in tlist)
{
Array [I] = T;
I ++;
}
Return array ;}

China Network Management Forum bbs.bitscn.com 2. transfer a single entity class, use the entity class in WebService
In this case, it can be seen as a special case of Case 1-an array with only one element.
Of course, in this case, we can use the entity class in WebService.
First look at the code in WebService:

 

[Xmlinclude (typeof (student)]
[Webmethod]
Public String hellostu (student stuinfo)
{

Return stuinfo. stuname;
} You must also add this line of code [xmlinclude (typeof (student)].
Then the call code is:
/** // <Summary>
/// Pass a single object class using the entity class in WebService
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button2_click (Object sender, eventargs E)
{Network Administrator www_bitscn_com
String STR = "";
Localhost. Student stuinfo1 = new localhost. Student (); // note that the entity class in WebService is called here, instead of the entity class in model. Otherwise, an error occurs.
Stuinfo1.stuname = "lxinxuan"; STR = Ser. hellostu (stuinfo1); // pass the entity class in WebService
MessageBox. Show (STR );
}
3. Transfer collection composed of object classes. This is similar to Case 1, but the transfer type is different. For more information, see.
In this case, you must modify the code of reference. CS, but each update must be modified again, and each class must be modified, which is troublesome! It is not recommended. I don't know which of the following methods has been developed. I also summarized them after reading the practices of others, but I can modify the reference. the CS Code has already demonstrated the spirit of research and encouraged me.
Similarly, the code of the method in WebService is given first:
[Webmethod]
Public String hellostusbylist (collection <student> stulist) // The parameter type here is collection
{

Bll. class1 CLS = new BLL. class1 ();
Return Cls. getname (stulist );
} The method parameter is collection. After WebService is added, the parameter of the corresponding method in reference. CS is changed to student [], array !! WebService and array are really close to each other... Change the parameter type of the method hellostusbylist in reference. CS to collection <localhost. Student>, as shown below.

Presentation Layer call code:
/** // <Summary>
/// Transfer the collection composed of object classes by modifying the code of reference. cs. However, each time WebService is updated, it must be modified again and each class must be modified, which is troublesome.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void button3_click (Object sender, eventargs E)
{
String STR = "";

Localhost. Student stuinfo1 = new localhost. Student ();
Stuinfo1.stuname = "lxinxuan"; Network Management ujia u.bitscn.com
Localhost. Student stuinfo2 = new localhost. Student ();
Stuinfo2.stuname = "www.cnblogs.com/lxinxuan ";

Collection <localhost. Student> stulist = new collection <localhost. Student> (); Network Management Alliance bitscn_com
Stulist. Add (stuinfo1 );
Stulist. Add (stuinfo2 );

STR = Ser. hellostusbylist (stulist); // by default, the parameter of the hellostusbylist method is student []. You can manually change it to collection.

MessageBox. Show (STR );
Network Management ujia u.bitscn.com

}
4. serialize the object class set to a string in XML format, and then deserialize it into a collection in WebService <> (Note: it cannot be ilist <> ), and then pass it to the business layer object.

 

 

[Webmethod]
Public String hellostusbycollection (string sxml)
{

Bll. class1 CLS = new BLL. class1 ();
Collection <student> stulist = Cls. deserializercollection <student> (sxml, typeof (collection <student>); // deserialize the collection network to www_bitscn_com
Return Cls. getname (stulist );
} The code of the deserializercollection method is as follows:

///
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "sxml"> </param> /// <Param name = "type"> </param>
/// <Returns> </returns>
Public Collection <t> deserializercollection <t> (string sxml, type)

{Network Management Alliance bitscn_com
Xmlreader reader = xmlreader. Create (New stringreader (sxml ));
System. xml. serialization. xmlserializer serializer = new system. xml. serialization. xmlserializer (type );

Object OBJ = serializer. deserialize (Reader );

Return (collection <t>) OBJ;
}
The calling code for the presentation layer is as follows:
/** // <Summary> // serialize the object class set to string, deserialize it to collection in WebService, and then pass it to the business layer object.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>

Private void button4_click (Object sender, eventargs E)
{

String STR = "";

Student stuinfo1 = new student ();
Stuinfo1.stuname = "lxinxuan ";

Student stuinfo2 = new student ();
Stuinfo2.stuname = "www.cnblogs.com/lxinxuan ";

Collection <student> stulist = new collection <student> (); stulist. Add (stuinfo1 );
Stulist. Add (stuinfo2 );

String stustring = This. serializer <collection <student> (stulist); // serialize the data to a string in XML file format.

STR = Ser. hellostusbycollection (stustring );
MessageBox. Show (STR );
} The serialize method code is as follows:
/** // <Summary>

/// The object class set is serialized as a string
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "objtoxml"> </param>

/// <Returns> </returns>
Public String serializer <t> (T objtoxml)
{System. Io. stringwriter writer = new system. Io. stringwriter ();
System. xml. serialization. xmlserializer serializer = new system. xml. serialization. xmlserializer (objtoxml. GetType ());
Serializer. serialize (writer, objtoxml );
Return writer. getstringbuilder (). tostring ();

}
5. This is a special case in case 4. serialize and pass an object class. If the method is similar, it will not be written. See the Demo code.

This is probably the case. Passing dataset is the most traditional and best way ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.