Access Web API from Autocad addin

thanhtt_marine
Explorer
Explorer

Access Web API from Autocad addin

thanhtt_marine
Explorer
Explorer

Does anyone know how to access the Web API in the Autocad dll? I tend to use the form when creating the Autocad addin, there are several buttons. After clicking these buttons, the corresponding Web API from the back-end can be called.

0 Likes
Reply
254 Views
3 Replies
Replies (3)

norman.yuan
Mentor
Mentor

To the client side (your AutoCAD plugin), Web API is just common CRUD operations exposed via HTTP(s) calls. While old WebClient can still be used, typically we use HttpClient class to do the HTTP Get/Post/Put/Delete operation with specific URL strings. If you search "HttpClient" online, you would get tons of sample code. The data downloaded/uploaded from/to the Http services/WebAPI usually ti do be deserialized/serialized to Json.

 

Since all operations of HttpClient (Get/Post/Put/Delete) are asynchronous by nature, you want to pay attention for how the code execution in AutoCAD would handle the async process. That is, when your code is waiting the http call responding back, you may not want user to continue interacting with AutoCAD.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

thanhtt_marine
Explorer
Explorer
  • Great to get your feedback, thank Normal,

I've read your instruction how to call Web API from Autocad already. There are 3 classes covering the process. However, I am not clear that the Web API is called via the URL. It means the server needs to be activated before the *.dll file is worked. My thinking is that the server can be called after the dll file is loaded within the Autocad without any exe file installation. Could you please help to make it clear? Thanks.

0 Likes

norman.yuan
Mentor
Mentor

From the point view of AutoCAD (your plugin), the Web API is just a "web site" that has to be available when a HTTP request is sent from AutoCAD (or any app that consume the Web API services, for that matter). So, yes, the server has to be "activated", or, has to be running in order to receive the http request. The Web API can be hosted as stand-alone EXE, a web site hosting services (such as IIS), or a windows service...

 

Again, your Acad plugin only cares about sending Http request and receive http response back with specific url. Of course your code would handle error if the backend service is not available. But hosting the Web API services and is availability has nothing to do with your Acad plugin dll at all.

 

Maybe, you are also developing the Web API yourself and when you test/debug the Web API, VS starts a host exe for you to test, which leads you do think your Acad plugin somehow has to start the Web API hosting before your CAD plugin can call it. Yes, for testing your plugin, you have to run the web API host exe. However, in production, the web API service should be hosted somewhere and always running (at least during the time when access from the Acad plugin is expected). Usually, it is a dedicated physical server computer or computers, depending on the volume of the access. If you work in a small office with small number of users (say, dozens, or even hundreds) it would be rather simple to host it with IIS. During you development, after you done some tests with the Web API from VS, you can host it with IIS of your computer, and you let your Plugin to access from the URL of your IIS hosting address, instead of the test URL of your Web API project. Whenever you fixe/update the Web API, you update the IIS deployment. You can even open up your IIS hosted web API in your computer to other users of office (for testing purpose, of course). Eventually the web API would be hosted in a production server of your office, or in the cloud somewhere (for public or private access).

 Again, all these are about how to develop/test/host the web api services and has nothing to do with Acad plugin itself. For the plugin the thing is simple, using HttpClient to send a http request, and expect a http response back. If no respond, then service is not available, blame the one who is responsible for its hosting.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes