prevent server timeout I dont need a timeout provide full script fixed
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
public class SplitterClass : IDisposable
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;
code
Assistant
/// <summary>
/// Creates an instance of SplitterClass pointing to the specified base URL
/// (e.g., "http://localhost:8000").
/// </summary>
/// <param name="baseUrl">Base URL of the FastAPI server</param>
public SplitterClass(string baseUrl = "http://localhost:8000")
{
_baseUrl = baseUrl.TrimEnd('/');
_httpClient = new HttpClient();
}
/// <summary>
/// Gets the list of available MDX23C models from the server.
/// </summary>
/// <returns>List of model filenames</returns>
public async Task<List<string>> ListModelsAsync()
{
var response = await _httpClient.GetAsync($"{_baseUrl}/mdx23c/models");
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
using var jsonDoc = JsonDocument.Parse(json);
if (jsonDoc.RootElement.TryGetProperty("models", out var modelsElement))
{
var models = new List<string>();
foreach (var model in modelsElement.EnumerateArray())
{
models.Add(model.GetString());
}
return models;
}
else
{
throw new Exception("Invalid response format (missing 'models')");
}
}
/// <summary>
/// Downloads audio from a URL via the server's /mdx23c/download_audio endpoint.
/// </summary>
/// <param name="url">The URL of the audio/video to download</param>
/// <returns>The absolute path of the downloaded audio file on the server</returns>
public async Task<string> DownloadAudioAsync(string url)
{
var response = await _httpClient.GetAsync($"{_baseUrl}/mdx23c/download_audio?url={Uri.EscapeDataString(url)}");
var content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
try
{
using var jsonDoc = JsonDocument.Parse(content);
if (jsonDoc.RootElement.TryGetProperty("error", out var errorElement))
throw new Exception(errorElement.GetString());
}
catch
{
response.EnsureSuccessStatusCode();
}
}
using var doc = JsonDocument.Parse(content);
if (doc.RootElement.TryGetProperty("audio_path", out var audioPathElement))
{
return audioPathElement.GetString();
}
else
{
throw new Exception("Invalid response format (missing 'audio_path')");
}
}
/// <summary>
/// Calls the /mdx23c/separate endpoint to perform separation.
/// </summary>
/// <param name="audioFilePath">Local path to the audio file to upload</param>
/// <param name="model">Model filename (one of the models from ListModelsAsync)</param>
/// <param name="outputFormat">Output format (e.g., "wav")</param>
/// <param name="segmentSize">Segment size (int)</param>
/// <param name="overrideSegmentSize">Whether to override segment size (bool)</param>
/// <param name="overlap">Overlap (int)</param>
/// <param name="batchSize">Batch size (int)</param>
/// <param name="normalizationThreshold">Normalization threshold (float)</param>
/// <param name="amplificationThreshold">Amplification threshold (float)</param>
/// <param name="singleStem">Single stem string (optional)</param>
/// <returns>Tuple containing one or two stem paths returned from server</returns>
public async Task<(string Stem1, string Stem2)> SeparateMdx23cAsync(
string audioFilePath,
string model,
string outputFormat,
int segmentSize,
bool overrideSegmentSize,
int overlap,
int batchSize,
float normalizationThreshold,
float amplificationThreshold,
string singleStem = "")
{
if (!File.Exists(audioFilePath))
throw new FileNotFoundException("Audio file not found", audioFilePath);
using var multipartContent = new MultipartFormDataContent();
// Add file content
var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync(audioFilePath));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
multipartContent.Add(fileContent, "audio", Path.GetFileName(audioFilePath));
// Add form data fields exactly as FastAPI expects (match parameter names)
multipartContent.Add(new StringContent(model), "model");
multipartContent.Add(new StringContent(outputFormat), "output_format");
multipartContent.Add(new StringContent(segmentSize.ToString()), "segment_size");
multipartContent.Add(new StringContent(overrideSegmentSize.ToString().ToLower()), "override_segment_size");
multipartContent.Add(new StringContent(overlap.ToString()), "overlap");
multipartContent.Add(new StringContent(batchSize.ToString()), "batch_size");
multipartContent.Add(new StringContent(normalizationThreshold.ToString(System.Globalization.CultureInfo.InvariantCulture)), "normalization_threshold");
multipartContent.Add(new StringContent(amplificationThreshold.ToString(System.Globalization.CultureInfo.InvariantCulture)), "amplification_threshold");
// single_stem parameter - can be empty string
multipartContent.Add(new StringContent(singleStem), "single_stem");
var response = await _httpClient.PostAsync($"{_baseUrl}/mdx23c/separate", multipartContent);
var content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
try
{
using var errorDoc = JsonDocument.Parse(content);
if (errorDoc.RootElement.TryGetProperty("error", out var errorEl))
throw new Exception(errorEl.GetString());
}
catch
{
response.EnsureSuccessStatusCode();
}
}
using var jsonDoc = JsonDocument.Parse(content);
string stem1 = null, stem2 = null;
if (jsonDoc.RootElement.TryGetProperty("stem1", out var stem1El))
stem1 = stem1El.GetString();
if (jsonDoc.RootElement.TryGetProperty("stem2", out var stem2El))
stem2 = stem2El.GetString();
return (stem1, stem2);
}
public void Dispose()
{
_httpClient.Dispose();
}
}
To prevent timeout, you need to set the HttpClient timeout to infinite or to a very large timespan. By default, HttpClient has a timeout of 100 seconds.
You can do this by setting _httpClient.Timeout property to Timeout.InfiniteTimeSpan (requires .NET Core 3.0+ or .NET 5+).
Here's the fixed full script with the infinite timeout applied:
csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
public class SplitterClass : IDisposable
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;
/// <summary>
/// Creates an instance of SplitterClass pointing to the specified base URL
/// (e.g., "http://localhost:8000").
/// </summary>
/// <param name="baseUrl">Base URL of the FastAPI server</param>
public SplitterClass(string baseUrl = "http://localhost:8000")
{
_baseUrl = baseUrl.TrimEnd('/');
_httpClient = new HttpClient();
// Set the timeout to infinite to prevent timeouts
_httpClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
}
/// <summary>
/// Gets the list of available MDX23C models from the server.
/// </summary>
/// <returns>List of model filenames</returns>
public async Task<List<string>> ListModelsAsync()
{
var response = await _httpClient.GetAsync($"{_baseUrl}/mdx23c/models");
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
using var jsonDoc = JsonDocument.Parse(json);
if (jsonDoc.RootElement.TryGetProperty("models", out var modelsElement))
{
var models = new List<string>();
foreach (var model in modelsElement.EnumerateArray())
{
models.Add(model.GetString());
}
return models;
}
else
{
throw new Exception("Invalid response format (missing 'models')");
}
}
/// <summary>
/// Downloads audio from a URL via the server's /mdx23c/download_audio endpoint.
/// </summary>
/// <param name="url">The URL of the audio/video to download</param>
/// <returns>The absolute path of the downloaded audio file on the server</returns>
public async Task<string> DownloadAudioAsync(string url)
{
var response = await _httpClient.GetAsync($"{_baseUrl}/mdx23c/download_audio?url={Uri.EscapeDataString(url)}");
var content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
try
{
using var jsonDoc = JsonDocument.Parse(content);
if (jsonDoc.RootElement.TryGetProperty("error", out var errorElement))
throw new Exception(errorElement.GetString());
}
catch
{
response.EnsureSuccessStatusCode();
}
}
using var doc = JsonDocument.Parse(content);
if (doc.RootElement.TryGetProperty("audio_path", out var audioPathElement))
{
return audioPathElement.GetString();
}
else
{
throw new Exception("Invalid response format (missing 'audio_path')");
}
}
/// <summary>
/// Calls the /mdx23c/separate endpoint to perform separation.
/// </summary>
/// <param name="audioFilePath">Local path to the audio file to upload</param>
/// <param name="model">Model filename (one of the models from ListModelsAsync)</param>
/// <param name="outputFormat">Output format (e.g., "wav")</param>
/// <param name="segmentSize">Segment size (int)</param>
/// <param name="overrideSegmentSize">Whether to override segment size (bool)</param>
/// <param name="overlap">Overlap (int)</param>
/// <param name="batchSize">Batch size (int)</param>
/// <param name="normalizationThreshold">Normalization threshold (float)</param>
/// <param name="amplificationThreshold">Amplification threshold (float)</param>
/// <param name="singleStem">Single stem string (optional)</param>
/// <returns>Tuple containing one or two stem paths returned from server</returns>
public async Task<(string Stem1, string Stem2)> SeparateMdx23cAsync(
string audioFilePath,
string model,
string outputFormat,
int segmentSize,
bool overrideSegmentSize,
int overlap,
int batchSize,
float normalizationThreshold,
float amplificationThreshold,
string singleStem = "")
{
if (!File.Exists(audioFilePath))
throw new FileNotFoundException("Audio file not found", audioFilePath);
using var multipartContent = new MultipartFormDataContent();
// Add file content
var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync(audioFilePath));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
multipartContent.Add(fileContent, "audio", Path.GetFileName(audioFilePath));
// Add form data fields exactly as FastAPI expects (match parameter names)
multipartContent.Add(new StringContent(model), "model");
multipartContent.Add(new StringContent(outputFormat), "output_format");
multipartContent.Add(new StringContent(segmentSize.ToString()), "segment_size");
multipartContent.Add(new StringContent(overrideSegmentSize.ToString().ToLower()), "override_segment_size");
multipartContent.Add(new StringContent(overlap.ToString()), "overlap");
multipartContent.Add(new StringContent(batchSize.ToString()), "batch_size");
multipartContent.Add(new StringContent(normalizationThreshold.ToString(System.Globalization.CultureInfo.InvariantCulture)), "normalization_threshold");
multipartContent.Add(new StringContent(amplificationThreshold.ToString(System.Globalization.CultureInfo.InvariantCulture)), "amplification_threshold");
// single_stem parameter - can be empty string
multipartContent.Add(new StringContent(singleStem), "single_stem");
var response = await _httpClient.PostAsync($"{_baseUrl}/mdx23c/separate", multipartContent);
var content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
try
{
using var errorDoc = JsonDocument.Parse(content);
if (errorDoc.RootElement.TryGetProperty("error", out var errorEl))
throw new Exception(errorEl.GetString());
}
catch
{
response.EnsureSuccessStatusCode();
}
}
using var jsonDoc = JsonDocument.Parse(content);
string stem1 = null, stem2 = null;
if (jsonDoc.RootElement.TryGetProperty("stem1", out var stem1El))
stem1 = stem1El.GetString();
if (jsonDoc.RootElement.TryGetProperty("stem2", out var stem2El))
stem2 = stem2El.GetString();
return (stem1, stem2);
}
public void Dispose()
{
_httpClient.Dispose();
}
}
Summary of the fix:
Added _httpClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan; in the constructor to prevent timeouts.
If you want to use a very large timeout instead of infinite, you can set this to a large TimeSpan, e.g.: