How to generate C# or Visual Basic client code for OData protocol versions 1.0–4.0
Tags: odata, dotnet, connectedservice, csharp
Published: 2019-10-14 21:51:22

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/urg0bqdbfgcmmm050rgn.png)

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](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice) to Visual Studio 2017/2019 projects.

> The [Unchase OData Connected Service](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice) 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](https://docs.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide) for WCF or [Add Web Reference](https://docs.microsoft.com/en-us/sharepoint/dev/general-development/step-2-adding-a-web-reference) for WSDL, but for OData protocol versions 1.0–4.0.

Follow **4 steps** below to achieve this:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ee2n5jvjb0d7xt4ikq0k.gif)

### **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...`

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/nz7bplu5g92yoowj1vvj.png)

* For Visual Studio 2019: `Extensions -> Manage Extensions`

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/lc86kgndcd50guin6gfo.png)

Choose `Online -> Visual Studio Marketplace` tab. In search field type *unchase*, then install [Unchase OData Connected Service](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice) (just click Download button):

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/shwtmmnjjjk9whd0xw3f.png)

After all, restart the Visual Studio to install the [Connected Service](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice).

### **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`:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9xi7x27373nwo9i6047k.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/1wlu2okll5qx8f7yk1l0.png)

Then choose [Unchase OData Connected Service](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice) to show the Connected Service wizard:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ovnlooh288ryxdgfne64.png)

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/yfezu129zs94tr0zy5qi.png)

### **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).

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/wxht6inusc04b3cqe8gz.png)

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](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice), you can use the "[Report a bug](https://github.com/unchase/Unchase.OData.Connectedservice/issues/new?title=%3CBUG_title%3E&labels=bug&body=%3CBUG_body%3E)" 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](https://marketplace.visualstudio.com/items?itemName=Unchase.unchaseodataconnectedservice) 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:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/gqsq1jimsrdn4tyc3bws.png)

### **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:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/tsl2vb77sk6iw5pwdt47.png)

There you can specify generated client code file name.

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/m4o8v82q3bahxh2vfqz4.png)

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:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/vr5hrifvmzzakimvi9p2.png)

* OData protocol version 4.0 `Advanced Settings` wizard page setting in `AdvancedSettings` link:

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/atqwveqbv47rvhanr317.png)

---

Enjoy programming with [Unchase](https://dev.to/unchase)!