1

The OData v4 Client Code Generator (https://marketplace.visualstudio.com/items?itemName=bingl.ODatav4ClientCodeGenerator) will generate the proxy class I need using Visual Studio but I need to generate that class in my C# code instead.

Is there a way to generate the same proxy class in C# without using Visual Studio?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Darin
  • 119
  • 2
  • 12

1 Answers1

0

Investigate how to execute T4 templates from C#
Running a T4 template using C#
Run T4 text template programmatically
Can I use T4 programmatically from C#?

The short answer is that the Text Templating Transformation executable and associated runtimes are distributed with Visual Studio, so whilst it is achievable for your C# code to manually execute the transform process, without packaging your own parser and transformation engine it is pretty much only going to work on a PC that has visual studio installed.

Can you not simply generate the Client Proxy into a dll on a PC that does have VS installed and use that dll in your applications where you do not have visual studio, such as within VS Code or other editors? You can generate the client proxy with the community edition of VS without too many issues if that is a factor.

If you control the API, then the simplest solution is to create a new project within the API solution, put the T4 client in there and simply regenerate the client as part of your deployment process, then other apps can use the client dll directly.

If you do not control the project for the API, contact the developers to see if they already do this for you!

There might be other alternatives to explore instead of using the client library, or to avoid the need to regenerate at runtime. If your code is highly dynamic in terms of some elements of the schema, then consider writing your own generic proxy that takes string based parameters or anonymous objects and or expressions and manually convert those to the specific OData query syntax to execute via a WebClient. The ways to achieve this vary too much to go into here without a lot more information on your specific scenario.

Consider your requirement from a different point of view, the OData v4 client proxies try to replicate the entire API, if only some elements of the API are changing faster than you would like to re-distribute the regenerated proxies then you only need to find a non-client proxy solution for just the elements of the schema that you are experiencing problems with.

You do not have to use the proxy classes to communicate with an OData v4 API, the T4 script is provided as a solution accelerator to get you coding against the API quickly, you are not forced to use it, but it is helpful.

There also maybe 3rd party apps or dlls that you can package with your application to help you achieve this, however executing the T4 client proxy classes is only half the job, if you do this at runtime, then you would also be required to manually compile these generated classes so that you can execute them.


Realising this issue, in our applications, where we create the service API, we also generate a client proxy dll and make it available (via Nuget) to our developers or clients that might want to interact with the API. So each time the service is published, we use scripts within our CI (through Azure DevOps) to regenerate and publish the Client API DLL whenever the service is re-deployed.

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81