A WCF Service Application with an XML DataStore Serialize/Deserialize & a “Tester”

About

This project presents two separate WCF Service Applications and a client “tester” console application in the same Visual Studio solution. The WCF Services were hosted using IIS Express. A simple console “test” application connects to both of the services through separate proxy references. There are two services: school service (the main demo) and a basic math service. The school service persists the database changes made in memory through serialization to an XML file and deserializion from this file upon the service start.

School Service

school house

Operational Contracts

The School Service allows for the following operational contracts (methods):

DataContract Modeling (Data Model)

It is important to understand the details of how the person, student, teacher, and PersonList (List<Person>) objects are modeled in this solution. The PersonList is just a CollectionDataContract that derives from List<Person>. This just models a list of Person objects which is just a single DataContract model. We use the Collection of PersonList as the in-memory datastore and object to be serialized/deserialized. The Person object is the base model for both the derived Student and Teacher classes. In the modeling contract for this base object we must remember to explicitly identify the polymorphic relationships to the base parent like this:

Then when we inherit from this base parent other objects like Student and Teacher…

We can continue with the standard DataContract and DataMember pattern modeling of our data model classes.

Note: Only the state data like field/properties are used for serialization/deserialization and transport across the wire via our service. We can put any additional methods in our models for the service to utilize, but the clients connecting to our service only care (and aware of) about the state data flowing back to them as explicitly specified in the contracts. WCF is opt-in, if you do not specify the data as a contract, your client will be unaware of its existence as it will be left out of the WSDL.

Check out the WCF DataContract modeling for each of the types of objects:

DataStore (In Memory) Serialization and Deserialization

The school service has a backend DataStore where it serializes and deserializes to/from an XML file each person, student, or teacher object (polymorphism is used here Person => Student & Person => Teacher) into a collection of PersonList (List<Person>) with Person objects. The XML attribute in the collection’s Person object tag holds the polymorphic type (example: i:type=”Student”). It’s best to just look at the XML example of the DataStore to see how the polymorphic objects are serialized into XML.

My serialization demo creates an XML file called “PersonData.xml” in a temporary directory (C:\Users\YourUserName\AppData\Local\Temp) on the user hard drive (this is a simple demo program, not production). If desired, a different data storage location can be specified in the code.
To see a sample of what the XML looks like when it serializes the DataContract models, please reference this file here:[demo XML file]

The DataStore utility class holds a reference to the data store object that is deserialized into memory upon loading/starting of the service. If the temporary file does not exist, the service creates one. The service calls upon the DataStore utility class to retrieve the now in-memory database and do basic crud operations by reference to this database object in memory. After each create, update, or delete operation by the service, the service calls upon the utility DataStore class to save the in-memory database object to file by serialization.

Basically, the DataStore utility class in the service holds the in-memory database and is responsible for serialization/deserialization. The service performs work by calling this DataStore database by reference into its own class and performs the create, read, update, and delete. Then when the service requests “Save” the DataStore object serializes the in-memory object to a file, in this case, it is XML. Note: In this particular demo, the temporary datastore file on the hard drive is wiped clean (if it already exists) when the service executes. This was done to facilitate testing of my class project.

HTTP Client Console (Service Tester)

This is a separate project in the Visual Studio solution that tests both the School (see above) and Math (see below) Services.

Remember what gets sent over the wire is just state data. So how does the client know about/access the PersonList, Person, Student, or Teacher DataContract models if they are stored on the service? It’s simple. When you build your client to connect through a proxy it will download/translate the DataContract models for you in your “Service Reference”. Make sure the service host is exposing its metadata exchange or WSDL data. In this Visual Studio demo, I used the Add Service Reference Utility (SVCUTIL.EXE) to make the process easy.

Note: If you UPDATE the DataContract models on your service host application, YOU MUST, your client application MUST, refresh/update the service reference. It’s as easy as right-clicking on the service reference and clicking update while the service host is running and sharing its WSDL.
All that my “tester” client console application did was connect to my service and do some simple CRUD applications and tested each OperationContract “method” on the service. See some of the screen captures in the DEMO.

Note: You must run your service host instance in “without debugging mode” when creating/updating your client tester program in the same solution because VS has its hooks. Furthermore, after you get your client tester application running you may wish to DISABLE the pesky WCF Tester via the project properties that is default in Visual Studio or just specify your solution to load first your service then client projects in the run order.

Basic Math Service

This is the second available service in my demo service application. It’s here because my student lab required it to be here, to show I can do two services running at the same time. If you would like to learn more about it, please reference the preceding project blog article here: https://portfolio.katiegirl.net/2019/03/14/basic-wcf-service-application-client-console-tester/

The basic Math service provides basic mathematical operations contracts including:

The client was expected to access the service using information available through the WSDL. I created my test client application by building a proxy using svcutil.exe http://localhost:portnumber/MathService.svcutil .

Note: You should update the client proxy or reference to the server before running this in your Visual Studio IDE by first going to TestClient (Console Application) –> Connected Services –> MathServiceRef and selecting “Update Service Reference”.

Demo

Code

You may download a copy of the project by visiting the code repo here.

Kathleen has 15 yrs. of experience analyzing business IT needs and engineering solutions in payments, electric utilities, pharmaceutical, financial, virtual reality, and internet industries with a variety of technologies. Kathleen's project experience has been diverse ranging from executing the business analysis “design phase” to hands-on development, implementation, and testing of the IT solutions. Kathleen and her husband reside in Indiana with no children, instead living along with their fur babies.