Transfer generic List objects in WCF-WCF)

Source: Internet
Author: User
During programming, DataSet, able, List, Dictionary, and other Collection types are usually used. In. 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 client program is also. 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 represented as arrays, 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

You can use the Add Service Reference setting in the WCF client program. The procedure is as follows:

See 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's VS is set like this by default. As mentioned in the blog post, 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 the sample code for downloading this post. 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 System. Collections. Generic;
Using System. ServiceModel;
Using System. Runtime. Serialization;

[ServiceContract]
[ServiceKnownType (typeof (Employee)]
Public interface IService
{
[OperationContract]
String GetData (int value );

[OperationContract]
List <string> getListString ();

[OperationContract]
[ServiceKnownType (typeof (Employee)]
List <Employee> getListEmployee ();

[OperationContract]
Dictionary <string, string> getDictionaryString ();
}

[DataContract]
[KnownType (typeof (Employee)]
Public class Employee
{
Public Employee (string name, int age, object oooo)
{
This. name = name;
This. age = age;
This. oooo = oooo;
}

[DataMember]
Public string name;
[DataMember]
Public int age;
[DataMember]
Public object oooo;
}
Server-side/Service. cs System. Collections. Generic;
Using System. Runtime. Serialization;

Public class Service: IService
{
Public string GetData (int value)
{
Return string. Format ("You entered: {0}", value );
}

Public List <string> getListString ()
{
List <string> list1 = new List <string> ();
List1.Add ("List string element 1 ");
List1.Add ("List string element 2 ");
Return list1;
}

Public List <Employee> getListEmployee ()
{
List <Employee> list2 = new List <Employee> ();
List2.Add (new Employee ("Wu yuze", 18, new object ()));
List2.Add (new Employee ("Wang Datong", 20, new object ()));
Return list2;
}

Public Dictionary <string, string> getDictionaryString ()
{
Dictionary <string, string> dict1 = new Dictionary <string, string> ();
Dict1.Add ("Wu yuze", "programmer ");
Dict1.Add ("Wang Datong", "salesman ");
Return dict1;
}
}

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]; // The List <string> returned by the Server can only retrieve string Arrays
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> ");
}
}
}


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.

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.