Walkthrough: Serialize POCO Proxies with WCF

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The POCO proxy type cannot be directly serialized or deserialized by the Windows Communication Foundation (WCF), because the DataContractSerializer serialization engine can only serialize and deserialize known types. The proxy type is not a known type. For more information, see the Serializing POCO Proxies section in the Working with POCO Entities topic. To serialize POCO proxies as POCO entities, use the ProxyDataContractResolver class to map proxy types to POCO types during serialization.

The example in this topic demonstrates how to instruct the DataContractSerializer to use the ProxyDataContractResolver class in service operations by defining an attribute class, which will be applied to the service operations, that internally uses the ProxyDataContractResolver to map proxy types to POCO types. It also shows how to associate the attribute class with methods that are part of a service contract in your WCF application.

The examples in this topic use the POCO classes that are defined in How to: Define POCO Entities and an AdventureWorks-based data model that is defined in How to: Customize Modeling and Mapping Files to Work with Custom Objects.

To create the class library project containing POCO classes.

  1. Create a new class library project named POCOAdventureWorksModel .

  2. Remove the default source code file that was added to the project.

  3. Add an empty model named AdventureWorksModel. To create an empty model, see the To create an empty .edmx file section in the How to: Create a New .edmx File topic. Modify the model by following the steps in Custom AdventureWorks .edmx File.

  4. Disable code generation for the .edmx file. Open the .edmx file in the Entity Data Model Designer (Entity Designer). Right-click on the designer surface and select Properties. In the Properties window, select the Code Generation Strategy property and select None. If the Properties window is not visible, press F4.

  5. Add the app.config file to your class library project. Right-click POCOAdventureWorksModel, click Add, and then click New Item.

  6. In the Add New Item dialog, select General templates and select Application Configuration File. Copy the following code between configuration tags in your application configuration file. Modify the Data Source value if necessary.

  7. Add a reference to the System.Runtime.Serialization library. This library is needed for the WCF's DataContract and DataMember attributes that are used on the serializable entity types.

  8. Add a new class to your project named POCOClasses. Add the code in Serializable POCO Classes Based on the AdventureWorks Model to the file. This contains the entity type and the object context definitions.

  9. Compile the project.

To create and configure the WCF project.

  1. Create a WCF Service Application project in the same solution as the class library project named POCOAdventureWorksService.

  2. Add a reference to the System.Data.Entity library.

  3. Add a reference to the POCOAdventureWorksModel project, which is where the model is defined.

  4. Add the connection string to the .config file so the Entity Framework runtime could find the metadata. Open the app.config file in your POCOAdventureWorksModel project, copy the connectionStrings element, and add it as a child element of the configuration element of the Web.config file.

  5. Create a new class and name it ApplyDataContractResolverAttribute.

  6. Add the following namespaces at the beginning of the file:

  7. Replace the code generated for the new class with the following code:

  8. Open the service interface file. By default, it is called IService1.

  9. Add the POCOAdventureWorksModel namespace at the beginning of the file. This is the namespace where the POCO types are defined.

  10. Replace the code that defines the service interface file with the following code:

  11. Open the service source code. By default, it is called Service1.srv.cs (or .vb)

  12. Add the POCOAdventureWorksModel namespace at the beginning of the file.

  13. Replace the code that defines the service class with the following code:

  14. Compile the project.

To test the service.

  1. Create a console application. Type POCOAdventureWorksTest for the project name.

  2. Add a reference to the POCOAdventureWorksModel project.

  3. Add a reference to the POCOAdventureWorksService service. In Solution Explorer, right-click the reference folder and select Add Service Reference.

  4. Open the app.config file and add the connection string to the file. Open the app.config file for POCOAdventureWorksModel, copy the connectionStrings element, and add it as a child element of the configuration element of the Web.config file.

  5. Open the file that contains the main function.

  6. Add the following namespaces, where the service and the POCO types are defined, at the beginning of the file:

  7. Replace the code with the following code. Note, that even though the service was able to serialize POCO proxies, the client received pure POCO objects.

See Also

Show: