Transfer a generic list object

Source: Internet
Author: User

Http://www.cnblogs.com/WizardWu/archive/2009/08/09/1542102.html

 

In WCF, objects of the following types, such as list <t> and list <custom class>, cannot be transmitted by default, are transmitted between server-side and client-side, and transmits generic collection objects such as dictionary. This post is not highly advanced, but I think it is highly practical to use the skills of this post, and it is a problem that everyone who learns WCF will encounter, therefore, I am brave enough to post this article on the blog homepage.

--------------------------------------------------------
Example download point of this post:
Http://files.cnblogs.com/WizardWu/090809.zip

The execution example requires Visual Studio 2008 + SP1 and does not require a database.
If the project fails to run normally when you press F5 in vs 2008 after the download, leave a message to inform you.
--------------------------------------------------------

During programming, dataset, able, list, dictionary, and other collection types are usually used. In. NET web service and WCF, the return type of the server-side function (operation). If it is dataset or datatable, the client can call it directly (if the clientProgramYes. but in WCF, the default configuration of vs 2008 does not allow you to transmit objects of the following types: List <string>, list <custom class>, however, generic dictionary objects are acceptable.

[Note: Methods and operation are called methods, but the former is a class in OO and does not exist on the network. The latter is in service, the public network can be called by other programs. The functions and methods published on the network in WCF, data services, and RIA services are called Operation.]

In this regard, I checked the official books [10] And o'reilly books [11] for Microsoft MCTs-certified WCF 3.5, and did not mention how to solve them. the metadata of net collections is displayed in array format when the WSDL is uploaded or transmitted over the network.

Because. net collections are. Net-specific, WCF cannot expose them in the Service metadata, yet because they are so useful,WCF offers dedicated using aling rules for collections.

Whenever you define a service operation that uses the collection interfaces ienumerable <t>, ilist <t>, or icollection <t>, the specific collection-type information gets lost in the metadata (WSDL) export, so in terms of how Collection types are sent guest ss the wire, they all are representedArrays, The resulting metadata always uses an array.

 

When developing WCF, if vs 2008 uses the default configuration, when the return type of the WCF server-side function (operation) is list <string>, the actual return type is string [] array, therefore, if the client still uses the list <string> variable to receive and assign values, a transformation error of 1 will occur during the compilation period:


Figure 1 after List Data Structure deserialization, the array is automatically changed on the client

Later, I found two blog posts on the network [1], [2], and mentioned that the WCF client program "add service reference" only needs to be changed in vs 2008) to handle this requirement. The procedure is as follows:

See the download example of this post. When our client program references the existing WCF Service Contract on the network, we will add a Service proxy reference like 2.


Figure 2 reference the WCF Service in the ASP. NET client program 

In the "add service reference" form in 3, the "go" button in the upper-right corner is the WCF Service to view an IP address and port on the network. The "Discover" button on the right is displayed, is to view the WCF Service in the same vs 2008 solution as this client project. Click the "advanced" button in the lower-left corner of the form.


Figure 3 enter the correct metadata exchange address in this pane and the operation name of the WCF Service is automatically obtained.

For example, 4. In the "set type" drop-down menu, set the default system. array is changed to the generic we want to use. the list type. The drop-down menu of another dictionary set type remains unchanged, indicating that the WCF Service can transmit generic dictionary type objects over the network.


Figure 4 default set type: system. Array

Microsoft vs is set like this by default. As mentioned in blog [2], the WCF client may be an old version. the environment of net 1.x may also be Java or other non-Microsoft technical platforms. Therefore, vs 2008 uses the array supported by all vendors and platforms by default, as the type of network transmission, rather than the latest version.. NET platform-specific collection data structure.

Finally, if the user-side program needs to change the configuration, select "Configure service reference" on the reference in the vs project, as long as it is like 5.


Figure 5 modifying referenced WCF Service in ASP. NET Client

The following is an example of downloading this post:Code. In the server-side WCF Service, we provide the functions of list <string>, list <custom class>, and dictionary <string, string> for the WCF client ASP.. Net program call. The execution result is 6.

 

Server-side/iservice. CS

 

 

Server-side/service. CS

 

Client-side/default. aspx. CS

Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Servicereference1.serviceclient prox =   New Servicereference1.serviceclient ();

/* * ********* List <string> ********** */
// String [] list1 = new string [2]; // Before modification, the list <string> returned by the server, the client can only retrieve the String Array
List < String > List1 =   New List < String > ();
List1 = Prox. getliststring ();

Response. Write (list1 [0]+ "<Br>");
Response. Write (list1 [1]+ "<P>");

/* * ********* List <custom class> ********** */
List < Servicereference1.employee > List2 =   New List < Servicereference1.employee > ();
List2 = Prox. getlistemployee ();

Response. Write (list2 [ 0 ]. Name +   " <Br> " );
Response. Write (list2 [ 0 ]. Age +   " <Br> " );
Response. Write (list2 [ 0 ]. Oooo +   " <P> " );// Object type

/* * ******** Dictionary <string, string> ********** */
Dictionary < String , String > Dict1 =   New Dictionary < String , String > ();
Dict1 = Prox. getdictionarystring ();

Foreach (Keyvaluepair < String , String > KVp In Dict1)
{
Response. Write (kVp. Key +   " , "   + KVp. Value +   " <Br> " );
}
}
}

Copy code

 

 


Figure 6 shows the execution result of this post. The following variables are returned from the WCF Service: List <string>, list <custom class>, and generic dictionary.

 

If you are interested in implementation, but you are not familiar with WCF, refer to [8] and [9] below for quick start teaching.

--------------------------------------------------------

RelatedArticle:

[1] using generic list <t> from WCF services
Http://dotnet.org.za/hiltong/archive/2008/05/21/using-generic-list-lt-t-gt-from-wcf-services.aspx

[2] WCF data contract support for collection and Dictionary
Http://www.cnblogs.com/artech/archive/2007/11/27/974665.html

[3] WCF distributed development: Win (8): Use a dataset, a data table, and a collection to transmit data
Http://www.cnblogs.com/frank_xl/archive/2009/04/23/1437486.html

[4] session 13: known type in serialization)
Http://www.cnblogs.com/artech/archive/2009/07/12/1521994.html

[5] trouble getting a WCF return a collection to A. NET 2.0 Client
Http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/3749248c-c3b0-40eb-88db-95574205dd9a

[6] Collection types in data contracts
Http://msdn.microsoft.com/zh-cn/library/aa347850.aspx
Http://msdn.microsoft.com/en-us/library/aa347850.aspx

[7] sharing WCF Collection types between service and client
Http://www.codeproject.com/KB/WCF/WCFCollectionTypeSharing.aspx

[8] Step-by-Step tutorial (10): deploy the case-type WCF server to IIS (Traditional Chinese, not directly related to this post)
Http://vmiv.blogspot.com/2008/05/step-by-step10wcfiis.html

[9] WCF learning ---- my first WCF Program (not directly related to this post)
Http://www.cnblogs.com/ASPNET2008/archive/2008/06/01/1211613.html

--------------------------------------------------------

Related books:

[10] Microsoft. NET Framework 3.5-Windows Communication Foundation, MCTs Exam 70-503 training kit, Chapter 1
Http://www.microsoft.com/learning/en/us/Books/12486.aspx
Http://www.amazon.com/MCTS-Self-Paced-Training-70-503-PRO-Certification/dp/0735625654

[11] programming WCF services, 2nd edition, Juval löwy, Chapter 3
For WCF Service programming, Juval löwy. The Chinese version is translated by "Zhang Yi and Xu Ning.
Http://oreilly.com/catalog/9780596521301/

--------------------------------------------------------

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.