Skip to content

Commit 0a2e108

Browse files
Improve EmbeddedResource documentation with step-by-step guide (#1631)
- Expand JsonProvider.fsx EmbeddedResource section with step-by-step setup instructions, fsproj snippet, naming convention explanation, and common pitfall warning - Expand CsvProvider.fsx to include a brief EmbeddedResource section pointing readers to the full guide in JsonProvider.fsx Addresses #1315. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a93b511 commit 0a2e108

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

docs/library/CsvProvider.fsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,21 @@ transformed the dataset to be smaller.
475475
*)
476476

477477
(**
478+
## Using CSV provider in a library
479+
480+
When using `CsvProvider` in a library that others will reference, the sample file must be
481+
embedded in the compiled assembly. See the step-by-step guide in
482+
[Using JSON provider in a library](JsonProvider.html#jsonlib) — the same `EmbeddedResource`
483+
parameter and naming convention applies to `CsvProvider`.
484+
485+
In brief, for a file `data/sample.csv` in a library `MyLib`:
486+
1. Mark the file as `<EmbeddedResource>` in the `.fsproj`
487+
2. Use `EmbeddedResource="MyLib, MyLib.data.sample.csv"` in the type provider declaration
488+
478489
## Related articles
479490
480-
* [Using JSON provider in a library](JsonProvider.html#jsonlib) also applies to CSV type provider
491+
* [Using JSON provider in a library](JsonProvider.html#jsonlib) — includes step-by-step
492+
`EmbeddedResource` guide that applies to all type providers
481493
* [CSV Parser](CsvFile.html) - provides more information about
482494
working with CSV documents dynamically.
483495
* [API Reference: CsvProvider type provider](../reference/fsharp-data-csvprovider.html)

docs/library/JsonProvider.fsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,26 @@ access the sample JSON. This works fine when the sample is specified inline, but
462462
the sample is specified as a local file (unless you distribute the samples with your library).
463463
464464
For this reason, the JSON provider lets you specify samples as embedded resources using the
465-
static parameter `EmbeddedResource` (don't forget then to [include the file](https://docs.microsoft.com/en-us/visualstudio/ide/build-actions) as EmbeddedResource in the
466-
project file). If you are building a library `MyLib.dll`, you can write:
465+
static parameter `EmbeddedResource`. When this parameter is set, the type provider at design time
466+
reads the sample from the local path, but at runtime (when the library is loaded by a consumer)
467+
it reads the sample from the embedded resource in the compiled assembly.
468+
469+
### Step-by-step guide
470+
471+
**Step 1**: Mark your sample file as an embedded resource in the `.fsproj` file:
472+
473+
```xml
474+
<ItemGroup>
475+
<EmbeddedResource Include="data/worldbank.json" />
476+
</ItemGroup>
477+
```
478+
479+
**Step 2**: Use the `EmbeddedResource` static parameter. The value must be
480+
`"AssemblyName, AssemblyName.dotted.path.to.file.json"` where:
481+
- `AssemblyName` is the name of your library assembly (without `.dll`)
482+
- The file path uses **dots** (not slashes) as separators, prefixed with `AssemblyName`
483+
484+
So for a file `data/worldbank.json` in a library `MyLib`, the value is:
467485
468486
*)
469487
type WB =
@@ -480,6 +498,14 @@ to load `MyLib.dll` and locate the sample `worldbank.json` as a resource of the
480498
this succeeds, it does not attempt to find the local file and so your library can be used
481499
without providing a local copy of the sample JSON files.
482500
501+
> **Common pitfall**: If you get a cryptic error where the type provider interprets the file path
502+
> as the CSV/JSON content itself (resulting in a single-column type named after the path), you
503+
> have likely forgotten to add the `EmbeddedResource` parameter, or the assembly name or resource
504+
> path in the parameter value is incorrect.
505+
>
506+
> To verify the embedded resource name, you can inspect the compiled `.dll` using a tool such as
507+
> `ildasm`, `dotnet-ildasm`, or ILSpy and look at the `.mresource` entries.
508+
483509
## Related articles
484510
485511
* [JSON Parser](JsonValue.html) - provides more information about

0 commit comments

Comments
 (0)