|
| 1 | +# Use .Net / C# to send HTTP(s) requests from inside WASM in Node.js, Bun, and Deno |
| 2 | + |
| 3 | +## Instructions for this devcontainer |
| 4 | + |
| 5 | +Tested with .Net version 8.0.100-rc.2.23502.2 . |
| 6 | + |
| 7 | +### Preparation |
| 8 | + |
| 9 | +1. Open this repo in devcontainer, e.g. using Github Codespaces. |
| 10 | + Type or copy/paste following commands to devcontainer's terminal. |
| 11 | + |
| 12 | + |
| 13 | +2. Install .Net workloads to handle WASM: |
| 14 | + |
| 15 | +```sh |
| 16 | +dotnet workload install wasm-tools |
| 17 | +dotnet workload install wasm-experimental |
| 18 | +``` |
| 19 | + |
| 20 | +### Building |
| 21 | + |
| 22 | +1. `cd` into the folder of this example: |
| 23 | + |
| 24 | +```sh |
| 25 | +cd browser-and-node/node |
| 26 | +``` |
| 27 | + |
| 28 | +2. Create new .Net project using `wasmconsole` template: |
| 29 | + |
| 30 | +```sh |
| 31 | +dotnet new wasmconsole |
| 32 | +``` |
| 33 | + |
| 34 | +3. Replace generated HelloWorld-like `Program.cs` and `main.mjs` with HTTP-enabled ones: |
| 35 | + |
| 36 | +```sh |
| 37 | +cp ../Program.cs ./ |
| 38 | +cp ../main.js ./main.mjs |
| 39 | +``` |
| 40 | + |
| 41 | +4. Compile the example: |
| 42 | + |
| 43 | +```sh |
| 44 | +dotnet build |
| 45 | +``` |
| 46 | + |
| 47 | +### Test with Node.js |
| 48 | + |
| 49 | +1. Run the configuration: |
| 50 | + |
| 51 | +```sh |
| 52 | +dotnet run |
| 53 | +``` |
| 54 | + |
| 55 | +Or, alternatively, the same by directly using `node` command: |
| 56 | + |
| 57 | +```sh |
| 58 | +node bin/Debug/net8.0/browser-wasm/AppBundle/main.mjs |
| 59 | +``` |
| 60 | + |
| 61 | +### Test with Bun |
| 62 | + |
| 63 | +1. Install Bun: |
| 64 | + |
| 65 | +```sh |
| 66 | +curl -fsSL https://bun.sh/install | bash |
| 67 | +``` |
| 68 | + |
| 69 | +2. Run with Bun: |
| 70 | + |
| 71 | +```sh |
| 72 | +~/.bun/bin/bun bin/Debug/net8.0/browser-wasm/AppBundle/main.mjs |
| 73 | +``` |
| 74 | + |
| 75 | +### Test with Deno |
| 76 | + |
| 77 | +1. Install Deno: |
| 78 | + |
| 79 | +```sh |
| 80 | +curl -fsSL https://deno.land/x/install/install.sh | sh |
| 81 | +``` |
| 82 | + |
| 83 | +2. Run with Deno: |
| 84 | + |
| 85 | +```sh |
| 86 | +~/.deno/bin/deno run --allow-read --allow-net bin/Debug/net8.0/browser-wasm/AppBundle/main.mjs |
| 87 | +``` |
| 88 | + |
| 89 | +You may want adding `Deno.exit();` to the end of `main.mjs`, in the case Deno does not exit after printing to console. |
| 90 | + |
| 91 | +### Finish |
| 92 | + |
| 93 | +Perform your own experiments if desired. |
0 commit comments