DICOMweb Client

DICOMweb client

DicomWebClient is included in the Aspose.Medical NuGet package. Install the separate Aspose.Medical.Dicom.Web.AspNetCore package in an ASP.NET Core server that hosts DICOMweb endpoints; client applications use the core package.

DicomWebClient is an HTTP user agent independent of the server application in this library. Give it an application-configured HttpClient and DicomWebClientOptions with an absolute BaseUri pointing at the remote service root. The client does not own or dispose the HttpClient. Configure authentication, TLS, proxy, redirects, and connection pooling on that transport. ServiceBaseUris can override roots by DicomWebService when a deployment splits services across paths or origins. BulkDataLoader supplies external metadata Bulk Data resolution; MaxNotificationMessageBytes defaults to 16 MiB.

Option Default and valid values Effect
BaseUri Required absolute HTTP or HTTPS URI Default root for every operation group.
ServiceBaseUris Empty immutable dictionary; each override must be an absolute HTTP or HTTPS URI Overrides one DicomWebService root.
MaxNotificationMessageBytes 16 MiB; positive integer Rejects larger incoming WebSocket event reports.
BulkDataLoader null IAsyncBulkDataLoader required when metadata contains external Bulk Data references. Select DiscardingBulkDataLoader.Instance explicitly to replace referenced values with empty data.

Supply service roots without query strings or fragments. The client appends each standard resource path to the selected root and escapes route and query values; pass those values decoded. A root of https://example.org/dicomweb/ produces a Studies search at https://example.org/dicomweb/studies, while WADO-URI sends its identifying query parameters to the root itself.

using System.Net.Http.HttpClient transport = new();
Aspose.Medical.Dicom.Web.DicomWebClient client = new(transport,
    new Aspose.Medical.Dicom.Web.DicomWebClientOptions
    {
        BaseUri = new System.Uri("https://example.org/dicomweb/")
    });

await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.Dataset> studies =
    await client.Studies.SearchAsync(new Aspose.Medical.Dicom.Web.DicomWebSearchOptions { Limit = 20 });
await foreach (Aspose.Medical.Dicom.Dataset study in studies)
    System.Console.WriteLine(study.Count);

DicomWebSearchOptions supplies ordered matching parameters, included fields, Limit, Offset, fuzzy, empty-value, and multiple-value matching flags, accepted media types, and extra headers. Null option values omit the corresponding query control. Searches return DicomWebClientStream<Dataset>, which owns a live response, can be enumerated once, and should be disposed even if enumeration stops early. DicomWebRetrieveOptions carries accepted media types, extra query parameters, and headers. DicomWebRenderOptions adds quality (1–100), viewport, window, ICC profile, annotation tokens, and additional parameters; see rendering control formats and thumbnail differences. DicomWebRequestOptions carries query parameters and headers for body operations. An empty AcceptedMediaTypes uses the transaction default; do not set both it and an explicit Accept header.

Set MatchingParameters to repeated, decoded DicomWebQueryParameter values. IncludeFields requests additional projected attributes; EmptyValueMatching, FuzzyMatching, and MultipleValueMatching are nullable so an omitted value leaves the server default in place. The provider receives a compiled search plan and the standard engine performs final DICOM matching:

using System.Net.Http.HttpClient transport = new();
Aspose.Medical.Dicom.Web.DicomWebClient client = new(transport,
    new Aspose.Medical.Dicom.Web.DicomWebClientOptions
    {
        BaseUri = new System.Uri("https://example.org/dicomweb/")
    });
Aspose.Medical.Dicom.Web.DicomWebSearchOptions options = new()
{
    MatchingParameters = [new Aspose.Medical.Dicom.Web.DicomWebQueryParameter("PatientName", "SMITH*")],
    IncludeFields = ["StudyDate", "PatientID"],
    FuzzyMatching = false,
    EmptyValueMatching = false,
    MultipleValueMatching = false,
    Limit = 20,
    Offset = 0
};
await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.Dataset> matches =
    await client.Studies.SearchAsync(options);
await foreach (Aspose.Medical.Dicom.Dataset study in matches)
    System.Console.WriteLine(study.Count);

When explicitly including a private data attribute in a standard study, series, instance, or workitem search, also supply a matching parameter for its Private Creator attribute. The creator must identify the same private block and, for a nested attribute, have the same sequence path. The standard service rejects an included private data attribute without its corresponding creator match. This requirement does not apply to including the Private Creator attribute itself.

Read metadata before loading bulk data

Studies.RetrieveStudyMetadataAsync, RetrieveSeriesMetadataAsync, and RetrieveInstanceMetadataAsync defer external value initialization for single-part DICOM JSON and Native DICOM Model XML responses, where bulk data is referenced outside the response payload. Enumerating metadata does not initialize those values. You can inspect tags and read inline attributes first, then explicitly initialize the values you need.

Multipart payloads are expected to supply referenced bulk data in separate parts of the same response. Data supplied in those parts is initialized after the corresponding parts are read, without an additional request. Multipart datasets are returned with their referenced values initialized; read those values directly. The explicit bootstrap steps in the following examples apply to deferred single-part responses.

Configure DicomWebClientOptions.BulkDataLoader even when retrieval is deferred. For single-part JSON/XML, encountering an external reference without a loader throws Aspose.Medical.Errors.MedicalApiException. Supply an application loader that implements the storage and authentication required by the referenced sources. Reading a deferred element’s Data, Count, or values before initialization also throws; it does not start loading. See deferred value initialization and its exception text.

This example uses an application-supplied Aspose.Medical.Dicom.Serialization.IAsyncBulkDataLoader bulkDataLoader and System.Threading.CancellationToken cancellationToken. It requests single-part JSON, reads an inline Rows attribute, and then initializes every asynchronous deferred value in each dataset, including nested sequence items:

using System.Net.Http.HttpClient transport = new();
Aspose.Medical.Dicom.Web.DicomWebClient client = new(transport,
    new Aspose.Medical.Dicom.Web.DicomWebClientOptions
    {
        BaseUri = new System.Uri("https://example.org/dicomweb/"),
        BulkDataLoader = bulkDataLoader
    });
Aspose.Medical.Dicom.Web.DicomWebRetrieveOptions options = new()
{
    AcceptedMediaTypes = [Aspose.Medical.Dicom.Web.DicomWebMediaTypes.ApplicationDicomJson]
};
await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.Dataset> metadata =
    await client.Studies.RetrieveSeriesMetadataAsync("1.2.3", "1.2.3.4", options, cancellationToken);
await foreach (Aspose.Medical.Dicom.Dataset instance in metadata.WithCancellation(cancellationToken))
{
    System.Console.WriteLine(instance.Contains(Aspose.Medical.Dicom.Tags.Tag.PixelData));
    System.Console.WriteLine(instance.GetSingleValueOrDefault<ushort>(Aspose.Medical.Dicom.Tags.Tag.Rows, 0));

    await instance.BootstrapAsync(cancellationToken);
    Aspose.Medical.Dicom.Elements.IElement? pixelData =
        instance.GetOrDefault(Aspose.Medical.Dicom.Tags.Tag.PixelData);
    if (pixelData is not null)
        System.Console.WriteLine(pixelData.Count);
}

To initialize only a selected element, open a fresh metadata response. The following alternative reuses client, options, and cancellationToken from above and initializes an OB Encapsulated Document if present; other external values remain deferred:

await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.Dataset> selectedMetadata =
    await client.Studies.RetrieveInstanceMetadataAsync("1.2.3", "1.2.3.4", "1.2.3.4.5", options, cancellationToken);
await foreach (Aspose.Medical.Dicom.Dataset instance in selectedMetadata.WithCancellation(cancellationToken))
{
    if (instance.GetOrDefault(Aspose.Medical.Dicom.Tags.Tag.EncapsulatedDocument)
        is Aspose.Medical.Dicom.Elements.OtherByte document)
    {
        await document.BootstrapAsync(cancellationToken);
        System.Console.WriteLine(document.Data.Length);
    }
}

For deferred single-part metadata, choose either dataset-level or element-level initialization. Each source permits one initialization attempt; initialize it before reading its values and keep the loader and its data source available until initialization completes. After a failed or canceled attempt, retrieve fresh metadata before retrying.

To discard externally referenced content deliberately, use DiscardingBulkDataLoader.Instance. It supplies empty data without accessing the sources; it does not retain a reference for later retrieval. Inline values are unaffected. Deferred single-part metadata still needs explicit initialization before those empty values can be read:

using System.Net.Http.HttpClient transport = new();
Aspose.Medical.Dicom.Web.DicomWebClient client = new(transport,
    new Aspose.Medical.Dicom.Web.DicomWebClientOptions
    {
        BaseUri = new System.Uri("https://example.org/dicomweb/"),
        BulkDataLoader = Aspose.Medical.Dicom.Serialization.DiscardingBulkDataLoader.Instance
    });
Aspose.Medical.Dicom.Web.DicomWebRetrieveOptions options = new()
{
    AcceptedMediaTypes = [Aspose.Medical.Dicom.Web.DicomWebMediaTypes.ApplicationDicomJson]
};
await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.Dataset> metadata =
    await client.Studies.RetrieveInstanceMetadataAsync("1.2.3", "1.2.3.4", "1.2.3.4.5", options, cancellationToken);
await foreach (Aspose.Medical.Dicom.Dataset instance in metadata.WithCancellation(cancellationToken))
{
    System.Console.WriteLine(instance.GetSingleValueOrDefault<ushort>(Aspose.Medical.Dicom.Tags.Tag.Rows, 0));
    await instance.BootstrapAsync(cancellationToken);
    Aspose.Medical.Dicom.Elements.IElement? pixelData =
        instance.GetOrDefault(Aspose.Medical.Dicom.Tags.Tag.PixelData);
    if (pixelData is not null)
        System.Console.WriteLine(pixelData.Count); // Zero when Pixel Data was an external reference.
}

Operation groups and results

Client property Operations Result shape
Capabilities RetrieveAsync Raw DicomWebClientResponse for JSON or WADL capabilities
Uri RetrieveDicomInstanceAsync, RetrieveRenderedInstanceAsync Materialized RetrieveUriDicomInstanceResult.File, or raw rendered response
Studies Study/series/instance search; StoreAsync and StoreStudyAsync; study/series/instance DICOM and metadata retrieval; bulk data, pixel data, frame retrieval; rendered, MPR, 3D, and thumbnail retrieval Search/metadata DicomWebClientStream<Dataset>; study/series DicomWebClientStream<DicomFile>; single instance RetrieveInstanceResult.File; store StoreInstancesResult.Response; binary/rendered methods return raw responses
Worklist Create, search, retrieve, update, change state, request cancellation, workitem/worklist/filtered-worklist subscription changes, ConnectNotificationsAsync Workitem datasets, optional status reports, materialized outcomes, search stream, or notification connection
StorageCommitment RequestAsync, CheckResultAsync IsPending and optional Report
ModalityWorklist SearchAsync Dataset stream
ModalityPerformedProcedureSteps CreateAsync, UpdateAsync, RetrieveAsync Optional status report or retrieved procedure-step dataset
NonPatientInstances SearchAsync, StoreAsync, RetrieveAsync Dataset stream, STOW response dataset, or RetrieveNonPatientInstanceResult.File

The method overloads accept a typed Dataset or DicomFile for common requests and DicomWebContent for caller-controlled payloads. Studies and non-patient stores also accept synchronous or asynchronous file sequences where provided. StoreStudyAsync constrains the study in the URI. Worklist update and typed state-change overloads accept the transaction UID that identifies workflow ownership. Cancellation overloads with a requester argument instead identify the requesting AE Title. The results table describes the payload type; always inspect Response.StatusCode, headers, and the operation-specific value for partial or pending outcomes.

Aspose.Medical.Dicom.Web.DicomWebClientResult<Aspose.Medical.Dicom.Web.Transactions.RetrieveInstanceResult> instance =
    await client.Studies.RetrieveInstanceAsync("1.2.3", "1.2.3.4", "1.2.3.4.5");
Aspose.Medical.Dicom.DicomFile file = instance.Value.File;

Aspose.Medical.Dicom.Web.DicomWebClientResult<Aspose.Medical.Dicom.Web.Transactions.StoreInstancesResult> stored =
    await client.Studies.StoreAsync(file);
Aspose.Medical.Dicom.Dataset storeReport = stored.Value.Response;

For large study retrieval, use the one-pass stream instead of accumulating all files:

await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.DicomFile> files =
    await client.Studies.RetrieveStudyAsync("1.2.3");
await foreach (Aspose.Medical.Dicom.DicomFile item in files)
    System.Console.WriteLine(item.Dataset.Count);

The other service groups use the same client and result conventions. These calls use UIDs from an existing deployment:

await using Aspose.Medical.Dicom.Web.DicomWebClientStream<Aspose.Medical.Dicom.Dataset> scheduled =
    await client.ModalityWorklist.SearchAsync();
await foreach (Aspose.Medical.Dicom.Dataset step in scheduled)
    System.Console.WriteLine(step.Count);

Aspose.Medical.Dicom.Web.DicomWebClientResult<Aspose.Medical.Dicom.Web.Transactions.CheckStorageCommitmentResult> commitment =
    await client.StorageCommitment.CheckResultAsync("1.2.3.6");
if (commitment.Value.IsPending)
    System.Console.WriteLine("Check again later");

Aspose.Medical.Dicom.Web.DicomWebClientResult<Aspose.Medical.Dicom.Web.Transactions.RetrieveModalityPerformedProcedureStepResult> mpps =
    await client.ModalityPerformedProcedureSteps.RetrieveAsync("1.2.3.7");
Aspose.Medical.Dicom.Dataset procedureStep = mpps.Value.ProcedureStep;

Aspose.Medical.Dicom.Web.DicomWebClientResult<Aspose.Medical.Dicom.Web.Transactions.RetrieveNonPatientInstanceResult> palette =
    await client.NonPatientInstances.RetrieveAsync("color-palettes", "1.2.3.8");
Aspose.Medical.Dicom.DicomFile paletteFile = palette.Value.File;

await using Aspose.Medical.Dicom.Web.DicomWebClientResponse rendered =
    await client.Studies.RetrieveRenderedInstanceAsync("1.2.3", "1.2.3.4", "1.2.3.4.5");
rendered.EnsureSuccessStatusCode();

Read the raw rendered ContentReader or copy it to your output destination before disposing the response. The server must advertise and implement the requested representation. The client exposes MPR and 3D methods, but the bundled standard Studies handler currently throws NotSupportedException for those transactions; use a server with a custom implementation before calling them.

WADO-URI and separate service roots

Use ServiceBaseUris[DicomWebService.Uri] when WADO-URI has a different endpoint from the Studies service. The client adds requestType=WADO, the Study, Series, and SOP Instance UIDs, and contentType to the query. For rendered retrieval, provide a concrete non-DICOM contentType; do not use a wildcard. WADO-URI media types must not include transfer-syntax or charset parameters. This example retrieves both Part 10 and JPEG representations from an existing server:

using System.Net.Http.HttpClient transport = new();
Aspose.Medical.Dicom.Web.DicomWebClient client = new(transport,
    new Aspose.Medical.Dicom.Web.DicomWebClientOptions
    {
        BaseUri = new System.Uri("https://example.org/dicomweb/"),
        ServiceBaseUris = System.Collections.Immutable.ImmutableDictionary<
            Aspose.Medical.Dicom.Web.DicomWebService, System.Uri>.Empty.Add(
                Aspose.Medical.Dicom.Web.DicomWebService.Uri,
                new System.Uri("https://example.org/wado/"))
    });
Aspose.Medical.Dicom.Web.DicomWebClientResult<
    Aspose.Medical.Dicom.Web.Transactions.RetrieveUriDicomInstanceResult> instance =
    await client.Uri.RetrieveDicomInstanceAsync("1.2.3", "1.2.3.4", "1.2.3.4.5");
System.Console.WriteLine(instance.Value.File.Dataset.Count);

await using Aspose.Medical.Dicom.Web.DicomWebClientResponse image =
    await client.Uri.RetrieveRenderedInstanceAsync("1.2.3", "1.2.3.4", "1.2.3.4.5",
        Aspose.Medical.Dicom.Web.DicomWebMediaTypes.ImageJpeg,
        new Aspose.Medical.Dicom.Web.DicomWebRenderOptions { Quality = 80 });
image.EnsureSuccessStatusCode();
await using System.IO.FileStream output = System.IO.File.Create("wado-instance.jpg");
await image.ContentReader.CopyToAsync(output);

Raw requests and response ownership

SendAsync(DicomWebClientRequest, token) sends a standard DicomWebTransaction with route-template values, ordered query parameters, headers, and optional DicomWebContent. It returns DicomWebClientResponse; dispose it after reading. StatusCode, IsSuccessStatusCode, Headers, ContentType, and ContentReader expose the live HTTP outcome. EnsureSuccessStatusCode throws HttpRequestException on unsuccessful HTTP status. ReadDatasetAsync, ReadDatasetsAsync, ReadDatasetsLazyAsync, ReadDicomFileAsync, and ReadDicomFilesAsync parse one semantic form from the same forward-only body. ReadDatasetsLazyAsync leaves bulk data referenced outside single-part JSON/XML payloads for explicit initialization. Multipart datasets are returned with referenced values initialized, including data supplied in other parts of the same response. ContentReader itself is a System.IO.Pipelines.PipeReader, not the owner of these semantic methods. Do not mix these readers with direct ContentReader reads on one response. Typed, materialized DicomWebClientResult<T> owns a consumed value and Response metadata; its response body has already been closed. Raw rendered and binary retrievals leave response consumption to you.

Aspose.Medical.Dicom.Web.DicomWebClientRequest request =
    new(Aspose.Medical.Dicom.Web.Transactions.RetrieveCapabilities.Instance);
await using Aspose.Medical.Dicom.Web.DicomWebClientResponse response =
    await client.SendAsync(request);
response.EnsureSuccessStatusCode();
System.Console.WriteLine(response.ContentType);

DicomWebClientRequest.RouteValues fills the named segments in a standard route template. This direct request uses the application-supplied bulkDataLoader and cancellationToken described above, requests single-part JSON study metadata, and selects deferred reading on the response:

using System.Net.Http.HttpClient transport = new();
Aspose.Medical.Dicom.Web.DicomWebClient client = new(transport,
    new Aspose.Medical.Dicom.Web.DicomWebClientOptions
    {
        BaseUri = new System.Uri("https://example.org/dicomweb/"),
        BulkDataLoader = bulkDataLoader
    });
Aspose.Medical.Dicom.Web.DicomWebClientRequest request =
    new(Aspose.Medical.Dicom.Web.Transactions.RetrieveStudyMetadata.Instance)
    {
        RouteValues = new System.Collections.Generic.Dictionary<string, string> { ["study"] = "1.2.3" },
        Headers = new Aspose.Medical.Dicom.Web.DicomWebHeaders(
            ("Accept", [Aspose.Medical.Dicom.Web.DicomWebMediaTypes.ApplicationDicomJson]))
    };
await using Aspose.Medical.Dicom.Web.DicomWebClientResponse response =
    await client.SendAsync(request, cancellationToken);
await foreach (Aspose.Medical.Dicom.Dataset metadata in
    response.ReadDatasetsLazyAsync(cancellationToken).WithCancellation(cancellationToken))
{
    System.Console.WriteLine(metadata.GetSingleValueOrDefault<ushort>(Aspose.Medical.Dicom.Tags.Tag.Rows, 0));
    await metadata.BootstrapAsync(cancellationToken);
}

See worklist and procedure step workflows, storage commitment and non-patient instances, and representations and streaming for service-specific requests and response consumption.

Failures and cancellation

Operations that return a materialized result or open a DicomWebClientStream<T> check the HTTP status before returning. An unsuccessful HTTP status throws DicomWebClientException, which derives from HttpRequestException. Response contains the status and headers. ResponseBody contains the captured bytes, and IsResponseBodyComplete indicates whether the entire body was captured. A body that exceeds the capture limit or cannot be read in full leaves a prefix in ResponseBody.

For a Studies or Non-Patient Instance Store 409 Conflict, ResponseDataset contains the failure report if the client captured the complete body and parsed it as DICOM JSON or XML. An empty body, unsupported format, incomplete capture, or parse failure leaves ResponseDataset null. If reading the body or parsing the report fails, InnerException provides the cause. Transport failures and local input or payload validation can throw other exceptions.

This example reads the rejected instances from a parsed Store failure report.

var fileToStore = await Aspose.Medical.Dicom.DicomFile.OpenAsync("image.dcm");
try
{
    var stored = await client.Studies.StoreAsync(fileToStore);
    System.Console.WriteLine(stored.Response.StatusCode);
}
catch (Aspose.Medical.Dicom.Web.DicomWebClientException exception) when (
    exception.StatusCode == System.Net.HttpStatusCode.Conflict &&
    exception.ResponseDataset is { } report &&
    report.Contains(Aspose.Medical.Dicom.Tags.Tag.FailedSOPSequence))
{
    var failed = report.Get<Aspose.Medical.Dicom.Elements.Sequence>(Aspose.Medical.Dicom.Tags.Tag.FailedSOPSequence);
    foreach (var instance in failed.Items)
    {
        var uid = instance.GetSingleValue<string>(Aspose.Medical.Dicom.Tags.Tag.ReferencedSOPInstanceUID);
        var reason = instance.GetSingleValue<ushort>(Aspose.Medical.Dicom.Tags.Tag.FailureReason);
        System.Console.WriteLine($"{uid}: 0x{reason:X4}");
    }
}

Methods that return DicomWebClientResponse, including SendAsync and rendered or binary retrievals, leave HTTP status handling to the caller. Check IsSuccessStatusCode and read the raw body if you need error details, or call EnsureSuccessStatusCode to throw HttpRequestException. This raw path does not capture a DicomWebClientException.ResponseBody automatically. The semantic readers also require a successful status.

Cancellation must cover both opening a response and consuming its body:

Operation Cancellation scope
Materialized result Its operation token covers sending the request and parsing the result.
DicomWebClientStream<T> The operation token covers sending and opening the response. Pass an enumeration token through WithCancellation or GetAsyncEnumerator(token) to cancel subsequent item reads.
Raw DicomWebClientResponse The operation token covers sending and acquiring the response. Pass a token to the semantic reader or ContentReader.CopyToAsync when consuming the body.
Notification connection The connection token covers the opening handshake. Each ReceiveAsync accepts its own receive token.

Dispose streamed and raw responses when reading ends or is canceled. A timeout for the whole download must remain active through body consumption; successfully opening a response does not mean that its payload has finished arriving.

Notifications

ConnectNotificationsAsync(requester, configure, token) on the client or Worklist opens the UPS WebSocket. Configure ClientWebSocketOptions when credentials or headers are needed. Dispose the connection after use. ReceiveAsync returns one parsed DICOM JSON event dataset or null after a close message; only one receive may be active. Binary, oversized, or malformed event messages fail. The server must have a subscription provider, an enabled notification route, and WebSocket hosting support.

State exposes the current System.Net.WebSockets.WebSocketState. Canceling a receive or failing to receive or parse an event aborts the connection. Dispose that connection and call ConnectNotificationsAsync again to resume receiving; another ReceiveAsync call on the aborted connection cannot resume the interrupted event. A normal close, indicated by null, also ends receiving on that connection.

await using Aspose.Medical.Dicom.Web.DicomWebNotificationConnection connection =
    await client.ConnectNotificationsAsync("READER");
Aspose.Medical.Dicom.Dataset? eventReport = await connection.ReceiveAsync();
if (eventReport is not null)
    System.Console.WriteLine(eventReport.Count);