| name | generate-and-scan-barcode-dotnet |
|---|---|
| description | Write or update C#/.NET code that uses the Aspose.BarCode Cloud SDK (`Aspose.BarCode.Cloud.Sdk.*` namespaces; NuGet package `Aspose.BarCode-Cloud`) to generate, recognize, or scan barcodes through Aspose's cloud REST API. Use this skill whenever the user wants barcode work in C#/.NET, touches files under `submodules/dotnet`, or mentions `GenerateApi`, `RecognizeApi`, `ScanApi`, `Configuration`, `GenerateParams`, `RecognizeBase64Request`, or `ScanBase64Request`. The SDK has several easy-to-miss idioms - `Stream` return values, `JwtToken` switching auth mode, GET methods requiring a public `fileUrl`, `RecognizeBase64Async`/`ScanBase64Async` naming, and the `(clientSecret, clientId)` convenience-constructor order - so consult this skill instead of guessing. |
The .NET SDK is a thin generated client over the Aspose BarCode Cloud REST API. Most tasks boil down to choosing the right API class (GenerateApi, RecognizeApi, ScanApi), choosing the right transport variant (GET, body/base64, or multipart), and setting up Configuration correctly.
The NuGet package name and code namespaces differ. Install Aspose.BarCode-Cloud, then import namespaces under Aspose.BarCode.Cloud.Sdk.*.
Use these namespaces in most C# examples:
using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;Prefer constructing APIs from a Configuration instance:
var config = new Configuration
{
ClientId = clientId,
ClientSecret = clientSecret
};
var generateApi = new GenerateApi(config);
var recognizeApi = new RecognizeApi(config);
var scanApi = new ScanApi(config);If the task is repo maintenance inside submodules/dotnet, read references/repo-workflow.md. If the task needs style-matching examples or snippet locations, read references/snippet-map.md.
Use one of these two patterns:
- Let the SDK fetch JWT tokens for you.
var config = new Configuration
{
ClientId = clientId,
ClientSecret = clientSecret
};- Inject a pre-fetched bearer token.
var config = new Configuration
{
JwtToken = token
};Setting Configuration.JwtToken changes AuthType from JWT to external bearer-token mode. Do not also expect ClientId and ClientSecret to be used after that.
Prefer new GenerateApi(config) and the equivalent RecognizeApi/ScanApi constructors. Convenience constructors exist, but their parameter order is new GenerateApi(clientSecret, clientId) and the same pattern applies to the other API classes. That order is easy to reverse, so avoid it unless there is a strong reason.
Inside this repo, TestsBase populates Configuration from Tests/Configuration.json or TEST_CONFIGURATION_* environment variables. Examples use TEST_CONFIGURATION_JWT_TOKEN; many snippets still use TEST_CONFIGURATION_ACCESS_TOKEN. Treat Configuration.JwtToken as the stable API surface and mirror the surrounding file when editing existing examples.
Pick the operation first:
GenerateApi: create a barcode image.RecognizeApi: decode a known or limited set of barcode types and optionally tune recognition.ScanApi: auto-detect any barcode types with the smallest API surface.
Then pick the transport variant based on what the user has:
- Public internet URL to an image: use
RecognizeAsyncorScanAsync. ThefileUrlmust be a public URL, not a local path. - Local file or stream: use
RecognizeMultipartAsyncorScanMultipartAsync. - Raw bytes already in memory: base64-encode them yourself and use
RecognizeBase64AsyncorScanBase64Async. - Small text plus simple query parameters for barcode generation: use
GenerateAsync. - Structured generate payload or larger data: use
GenerateBodyAsync. - Multipart form generation: use
GenerateMultipartAsyncwhen the caller explicitly needs multipart.
Key method names:
GenerateAsyncGenerateBodyAsyncGenerateMultipartAsyncRecognizeAsyncRecognizeBase64AsyncRecognizeMultipartAsyncScanAsyncScanBase64AsyncScanMultipartAsync
GenerateAsync,GenerateBodyAsync, andGenerateMultipartAsyncreturnStream, notbyte[]or a file path. Save the stream or pass it onward, and dispose it withusingorawait using.RecognizeBase64AsyncandScanBase64Asyncexpect a base64 string in the request model. The SDK does not callConvert.ToBase64Stringfor you.RecognizeBase64Request.BarcodeTypesaccepts multipleDecodeBarcodeTypevalues.RecognizeAsyncandRecognizeMultipartAsynctake a singleDecodeBarcodeType.ScanApidoes not take a barcode type or recognition-quality knobs. Use it when the caller wants auto-detection.- GET-based recognize and scan methods only work with remote files reachable by URL. For local files on disk, do not pass a local path to
fileUrl; use multipart or base64. BarcodeResponseListmay contain multiple results. Iterateresponse.Barcodesand readBarcodeValue,Type,Region, andChecksum.- API failures throw
Aspose.BarCode.Cloud.Sdk.Api.ApiExceptionwith anErrorCodeHTTP status. Turn onConfiguration.DebugMode = truewhen request or response logging would help. - The repo has a tested end-to-end pattern where a generated
Streamis passed directly intoScanMultipartAsyncwithout saving to disk first. Reuse that pattern when it fits.
Generate and save a QR code:
var config = new Configuration { ClientId = clientId, ClientSecret = clientSecret };
var api = new GenerateApi(config);
await using Stream generated = await api.GenerateAsync(
EncodeBarcodeType.QR,
"hello from .NET",
imageFormat: BarcodeImageFormat.Png,
textLocation: CodeLocation.None);
await using FileStream file = File.Create("qr.png");
await generated.CopyToAsync(file);Recognize specific barcode types from bytes already in memory:
var bytes = await File.ReadAllBytesAsync("many-types.png");
var request = new RecognizeBase64Request
{
BarcodeTypes = new List<DecodeBarcodeType>
{
DecodeBarcodeType.QR,
DecodeBarcodeType.Code128
},
FileBase64 = Convert.ToBase64String(bytes)
};
BarcodeResponseList result = await new RecognizeApi(config).RecognizeBase64Async(request);Auto-scan a local stream without specifying the barcode type:
await using Stream image = File.OpenRead("unknown.png");
BarcodeResponseList result = await new ScanApi(config).ScanMultipartAsync(image);Read references/repo-workflow.md when the task changes SDK source, tests, snippets, package metadata, or generated code in submodules/dotnet.
Read references/snippet-map.md when the task needs example code, README-aligned snippets, or the closest existing pattern for a generate, recognize, or scan scenario.
- Use the right package and namespace pair: NuGet
Aspose.BarCode-Cloud, namespacesAspose.BarCode.Cloud.Sdk.*. - Prefer
Configuration-based construction and avoid swappingclientSecretandclientId. - Choose GET only for public URLs, multipart for local streams, and base64 or body variants for bytes or structured payloads.
- Base64-encode body payloads yourself before calling
RecognizeBase64AsyncorScanBase64Async. - Dispose generated or opened streams.
- Iterate
response.Barcodesinstead of assuming a single result. - When changing the repo, validate with the submodule workflow in
references/repo-workflow.md.