In this post we will be discussing a minor issue with the serialization of DataTable in WCF. We, then would present its solution. The issue is that we cannot send a DataTable object across the wire for which TableName property is not set. If we do try to send it, it throws exception. The post would just have an example to show the issue and its solution.
There are a lot of material you can find, not recommending DataSet and DataTable on WCF boundaries. But you might run into situations where some architectural design decisions are horribly taken and you cannot change them. This case would be one of them. Or this might be a client requirement which needs data in this format.
Let's create a WCF Library project DataTableWCFApp.Service. We can update the service contract as follows:
We can use the following as the implementation of the ServiceContract interface presented above. This is just creating a untyped DataTable, filling it with a single row and returning it.
We wouldn't be able to test it using WCF Test Client because of non-primitive DataTable type which it cannot deserialize.
So let's create a simple MVVM Light WPF project to test it. Here we are creating a project with name DataTableWCFApp as follows:
After adding the service reference for the DataTable Service, we need to update the view and view model for showing the DataTable returned from the service. Let's update MainWindow as follows:
So the view expects a collection MyData in its DataContext. The collection is expected to be available at the time of loading the view, hence OneTime mode for Binding. Let's update the view model as follows:
Before running it, we need to find a way to host the WCF Service. Luckily Visual Studio makes it easier for us. Just check the following option is checked for your WCF project. This would host the WCF Service when another project in the solution is being debugged. If we set the WCF Service project as the start up project then this would host it in WCF Service Host, additionally, this would launch WCF Test Client for the service.
Now set the WPF Project as the Startup Project and run it. We get the following CommunicationException with no details about what actually happened or how to fix it.
In order to find the actual cause, let's take some help from WCF Service Trace Viewer. We need to add the following configuration in our WCF Service project's app.config file.
Now we can run it again. We would again get the same exception but the traces should be recorded in the location for the trace file as mentioned in the above configuration. Let's try opening the file using Microsoft Service Trace Viewer. We need to install Windows SDK for this tool. It shows the actual cause of the problem.
The error message is descriptive enough. We are just missing to set the TableName of the DataTable. The DataTable cannot be serialized without setting it. Let's update the Service to do the same as follows:
Now we run the project again. Now the service returns the response without any issue. The DataTable is used by the view to display it in a DataGrid. For an untyped DataTable, this would be the most suitable control as this can analyze data and create columns based on the data in the DataTable. Other control, e.g. ListBox would show DataRowView for each entry in the DataTable.
Download
Showing posts with label SOA. Show all posts
Showing posts with label SOA. Show all posts
Thursday, July 26, 2012
Thursday, March 15, 2012
Visual Studio hosting WCF Services using WCF Service Host
In this post we are going to be creating a simple WCF Service library project. We are going to be hosting this using Visual Studio Service host. We are going to see what various options are available to launch a WCF Service using Visual Studio including how we can test it using WCF Service Client. We are also going to be using Process Explorer to see how these supporting tools are used by Visual Studio.
When we create a WCF class library project, then Visual Studio creates a default WCF service for us with default template. It creates the following in the project.
Basically the additional start-up argument is the default start-up argument of WCF Service library project. It can be seen in the Debug tab of project properties. If we don't want the WCFTestClient to be launched when WCF Service runs as a start-up project then just remove this extra switch.
Download Code:
When we create a WCF class library project, then Visual Studio creates a default WCF service for us with default template. It creates the following in the project.
- Adds the relevant assembly reference to the project. This includes System.Runtime.Serialization.dll and System.ServiceModel.dll.
- Adds a WCF Service containing a simple method. The class implements a contract added separately. The method (defined as operation contract)
- Adds a interface decorated with [ServiceContract] attribute. This also has a method decorated with [operationContract] attribute. This is the same interface implemented by the above class.
- Adds a class decorated with [DataContract] attribute. This type is used as a parameter in the operation contract method of above service contract.
- Adds app.config to the project. The configuration has the details of the exposed WCF service.
- If the WCF class library project is the start-up project then the service host is launched with relevant start-up arguments to use WCF Test Client as the client tool for the hosted service. The service host takes care of launching the client tool. We can use Process explorer to see the start-up argument of service host process.
It is basically launched with the following start-up arguments.
/service:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll /config:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll.config /event:dc8e4802-046c-4134-97b3-0d940ab3ee82 /client:"WcfTestClient.exe
Specifying the switch for client launches the WCF Test Client after the service is hosted in WCF Service Host tool. The WCF Test Client is launched with the start-up arguments specifying the service details.
-
When the service project is not selected as a start-up project then Visual Studio just launches the WCF Service host with the details of the service being hosted. The start-up arguments to launch WCF Service host are as follows:
/service:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll /config:C:\Users\shujaat\documents\visual studio 2010\Projects\InstituteApp\Institute.Data\bin\Debug\Institute.Data.dll.config /event:56023077-b3b8-486d-8e62-ffc2ccf7cad7
We can see that all the parameters are same except the client is not expected to be launched so the client switch hasn't been specified.
See the properties of a WCF Service library project. There is an additional WCF options tab added.
There is only one configurable option for the WCF service. When checked, Visual Studio automatically launches WCF Service Host utility hosting the service even when there is another start up project in the solution. This is a Visual Studio supporting tool and gets installed with Visual Studio installation in the following path in your Windows 7 computer.
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\
This is available as follows:
Let's download Process Explorer to see the details about WCF Service Host process from the following technet link:
http://technet.microsoft.com/en-us/sysinternals/bb896653
When launched, WCF Service host is available in system tray as follows:
The tray icon can be used to launch the tool's interface. The interface has the details about hosted service.
Basically the additional start-up argument is the default start-up argument of WCF Service library project. It can be seen in the Debug tab of project properties. If we don't want the WCFTestClient to be launched when WCF Service runs as a start-up project then just remove this extra switch.
Download Code:
Subscribe to:
Posts (Atom)