How to generate C# or Visual Basic client code for OData protocol versions 1.0–4.0

Nikolay Chebotov
4 min readAug 11, 2019

--

This How-To describes how to generate C# or Visual Basic client code for OData protocol versions 1.0–4.0 with Unchase OData Connected Service to Visual Studio 2017/2019 projects.

The Unchase OData Connected Service is a Visual Studio 2017/2019 extension to generate C# (Visual Basic) client code for OData web services. Simply put, it is like kind old Add Service Reference for WCF or Add Web Reference for WSDL, but for OData protocol versions 1.0–4.0.

Follow 4 steps below to achieve this:

All steps of generation process in Visual Studio Community 2019 v16.1.3 with some customization of code generation

Step 1. Install the Connected Service

Launch the installed Visual Studio 2017/2019. Open the menu to manage Visual Studio extensions:

  • For Visual Studio 2017: Tools -> Extensions and Updates...
“Extensions and Updates…” menu item in Visual Studio Community 2017 v15.9.12
  • For Visual Studio 2019: Extensions -> Manage Extensions
“Manage Extensions” menu item in Visual Studio Community 2019 v16.1.3

Choose Online -> Visual Studio Marketplace tab. In search field type unchase, then install Unchase OData Connected Service (just click Download button):

Install the “Unchase OData Connected Service” in Visual Studio

After all, restart the Visual Studio to install the Connected Service.

Step 2. Open the Connected Service wizard for Visual Studio project

Open the Visual Studio solution with project to generate client code in. In Solution Explorer make right-click on Connected Services or Dependencies item in the project for which you need to generate client code, and click on Add Connected Service:

“Connected Services” item in the project in Solution Explorer
“Dependencies” item in the project in Solution Explorer

Then choose Unchase OData Connected Service to show the Connected Service wizard:

Choose the “Unchase OData Connected Service”
“Unchase OData Connected Service” wizard

Step 3. Configure OData metadata endpoint

In the first page of the wizard you need to configure some necessary options:

  • Generating Language — C# or Visual Basic client code generation option;
  • Service name — the name of the folder (after generation) in Connected Service directory in the project (by default, if field is empty — OData Service);
  • OData metadata URI (path) — the metadata endpoint URI (URL or local file).
Filled the necessary options to start generating the code

After setting the necessary options, you can immediately start generating the code files by clicking the Finish button. In this case, there will choose default code generation options.

If you find a bug in the Connected Service, you can use the “Report a bug” button.

There are a few optional options you can specify before start generation process:

  • Load from 'Connected Service.json' button — allows load client code generation parameters from json-file generated by Unchase OData Connected Service or OData Connected Service by Microsoft;
  • Open generated files on complete in IDE checkbox — allows automatically open the generated files in IDE in generation process;
  • Accept all untrusted certificates checkbox — allows to accept all untrusted certificates when connecting to the services;
  • If you need to set network credentials for connecting to the endpoint (metadata file), you should specify options in the Network Credentials section. If your network used Proxy, you should specify options in the Web Proxy section:
Specify “Network credentials” and “Web Proxy with credentials” to access the metadata URI

Step 4 (Optional). Configure code generation options

If you want to specify some options for generating code, just need to go to generation settings tab(s):

  • Common OData protocol versions 1.0–4.0 Advanced Settings wizard page settings:
Common Advanced Settings for OData protocol versions 1.0–4.0

There you can specify generated client code file name.

  • OData protocol versions 1.0–3.0 Advanced Settings wizard page setting in AdvancedSettings link:
OData protocol versions 1.0–3.0 Advanced Settings

If you check Select OData methods from FunctionImports (V3) or OperationImports (V4), the wizard page (tab) named Operation Imports Settings will be added where you can select the necessary methods that will be added after generation:

Select methods that will be added after generation
  • OData protocol version 4.0 Advanced Settings wizard page setting in AdvancedSettings link:
OData protocol version 4.0 Advanced Settings

Enjoy programming with Unchase!

--

--

Nikolay Chebotov

Security researcher, developer. Mostly working with “Centurion” and Visual Studio Extensions. https://github.com/unchase