Skip to content

1.1 Create an OData V4 client app in Visual Studio 2012

Yi Ding edited this page Sep 1, 2014 · 16 revisions

This section is a tutorial about how to create a client application for the OData V4 service with ODataCpp using Visual Studio 2012. The ODataCpp client libraries currently only supports Visual Studio 2012. We'll add more support soon.

Generate the client side proxy of the target service

Suppose you already have your ODataCpp client libraries and code generation tools built. If you haven't, please refer to the README file for instructions.

1. Navigate to the path of where the built libraries and tool are located

Open Command Prompt (cmd.exe) or PowerShell and type in the following command to navigate to the location:

cd <Home>\odatacpp\output\<Configuration>

<Home> is the path where your odatacpp folder is located and <Configuration> is the build configuration you've chosen to build your library (either "Debug" or "Release"). In this tutorial, we chose "Release" as the build configuration. So the command to type is:

cd <Home>\odatacpp\output\Release

2. Run the code generation tool to generate the proxy

Type in the following command in Command Prompt or PowerShell

.\odata_codegen_tool.vs11.exe <ServiceURL> <ProxyFileName> <Username> <Password>

<ProxyFileName> is the name you want for the .h and .cpp files of the proxy code. <ServiceURL> is the URL of the service document of the target OData service. <Username> and <Password> are the credential for accessing the service in case it requires authentication.

In this tutorial, we want to build a client application for the TripPin service published on www.odata.org and it doesn't require authentication, so we type in the following command:

.\odata_codegen_tool.vs11.exe http://services.odata.org/v4/TripPinServiceRW TripPinService

and we get the following two proxy files: The generated proxy files for the TripPin service

Create an client app in Visual Studio 2012 and include the generated proxy

1. Open Visual Studio 2012 and create a Win32 C++ project

Create an empty C++ project in Visual Studio 2012

2. Configure the properties of the project

In Visual Studio, from the Build menu, select Configuration Manager. In the Configuration Manager window that shows up, change the value of Active solution configuration to "Release". Change the build configuration in Configuration Manager

In Visual Studio, right click the client project in the Solution Explorer and select Properties from the menu. The Property Pages windows of the project will show up. Follow the steps to set the directories and dependencies:

Change the project properties

  1. Select Configuration Properties > C/C++ > General from the left navigation area, and add <Home>\odatacpp\include and <Home>\odatacpp\output\Release (because that's where TripPinService.h is) to the value of Additional Include Directories.
  2. Select Configuration Properties > Linker > General from the left navigation area and add <Home>\odatacpp\output\Release to the value of Additional Library Directories.
  3. Select Configuration Properties > Linker > Input from the left navigation area and add odata_client.vs11.lib, odata_library.vs11.lib, and utilities.vs11.lib to the value of Additional Dependencies.

3. Install Casablanca to the project via Nuget package manager

In Visual Studio, from the Tools menu, select NuGet Package Manager > Package Manager Console. In the Package Manager Console window, type:

Install-Package cpprestsdk -Version <Version>

and return, where <Version> is the same version number of the Casablanca library that you used to build your ODataCpp libraries. The library will then be installed.

4. Add the generated proxy to the project

In the Solution Explorer of Visual Studio, right-click the project and select Add > Existing item and choose the "TripPinService.cpp" formerly generated to add it to the project.

5. Make the dynamic ODataCpp libraries accessible to the project at run time

Copy "odata_client.vs11.dll", "odata_library.vs11.dll", and "utilities.vs11.dll" from <Home>\odatacpp\output\Release to <Workspace>\TripPinClient\Release, where <Workspace> is the path where your client project resides.

6. Write a simple client application

In the Solution Explorer of Visual Studio, right-click the project and select Add > New item. In the Add New Item window that shows up, select C++ File under the Visual C++ directory, name it "Main.cpp" and click Add.

In the Main.cpp windows, fill in the following code:

#include <iostream>
#include <TripPinService.h>

using namespace std;
using namespace Microsoft_OData_SampleService_Models_TripPin;

const ::utility::string_t trippin_service_root(U("http://services.odata.org/V4/(S(khax213ynjvk4pqdhdgcadvw))/TripPinServiceRW/"));

int main()
{
    auto service_context = make_shared<DefaultContainer>(trippin_service_root);
    auto airlines = service_context->create_airlines_query()->execute_query().get();
    for(auto iter = airlines.cbegin(); iter != airlines.cend(); iter++)
    {
        wcout << (*iter)->get_name() << endl;
    }

    return 0;
}

Build and run the project. The follow results will be shown in the console window: The result of the project shown in console window