You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.Net环境创建Azure Media Services Transform返回Conflict冲突错误

问题原因

报Conflict(409)错误的核心原因如下:

  • 竞态条件:Azure Functions支持横向扩展,多个并发请求触发函数时,多个实例会同时检测到目标Transform不存在,同时发起创建请求,就会触发冲突错误。
  • 异步上下文不统一:代码中查询Transform使用了同步的Get方法,在Azure Functions的异步执行上下文里容易引发线程阻塞,甚至导致异常捕获不全。
  • Transform为可复用资源,无需每次函数执行都动态创建,重复的创建逻辑本身就会提升冲突概率。
可行解决方案

方案1:预创建Transform(最推荐)

Transform是Media Services中一次创建、可多次重复使用的资源,你可以提前通过Azure门户、Azure CLI或本地脚本一次性创建好需要的Transform,函数中直接查询使用即可,完全避免运行时创建的冲突问题。

方案2:优化现有代码的并发兼容

步骤1:将同步查询改为异步调用,统一异步上下文

步骤2:添加Conflict异常的重试逻辑,捕获到冲突时直接查询已有Transform,避免重复创建

修改后的代码示例如下:

public async Task<Transform> GetOrCreateTransformAsync(
            Dictionary<string, string> config, string transformName)
{
    IAzureMediaServicesClient client = await CreateMediaServicesClientAsync(config);
    Transform transform = null;
    // 先尝试查询已有Transform
    try
    {
        transform = await client.Transforms.GetAsync(
            config.GetValueOrDefault("ResourceGroup"), 
            config.GetValueOrDefault("AccountName"), 
            transformName);
        if (transform != null) return transform;
    }
    catch (ErrorResponseException ex) when (ex.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
    {
        // 不存在则继续走创建逻辑
    }

    // 构建Transform输出
    TransformOutput[] output = new TransformOutput[]
    {
        new TransformOutput
        {
            Preset = new BuiltInStandardEncoderPreset()
            {
                PresetName = EncoderNamedPreset.AdaptiveStreaming
            }
        }
    };

    // 尝试创建,捕获冲突异常时再次查询
    try
    {
        transform = await client.Transforms.CreateOrUpdateAsync(
            config.GetValueOrDefault("ResourceGroup"), 
            config.GetValueOrDefault("AccountName"), 
            transformName, 
            output);
    }
    catch (ErrorResponseException ex) when (ex.Response.StatusCode == System.Net.HttpStatusCode.Conflict)
    {
        // 冲突说明其他实例已经创建完成,直接查询返回即可
        transform = await client.Transforms.GetAsync(
            config.GetValueOrDefault("ResourceGroup"), 
            config.GetValueOrDefault("AccountName"), 
            transformName);
    }
    return transform;
}

方案3:添加分布式锁(高并发场景补充)

如果你的函数并发量极高,可以在创建Transform的逻辑前添加Azure Blob存储租约、Redis锁等分布式锁,保证同一时间只有一个实例可以执行创建操作,从根本上避免竞态条件。

内容的提问来源于stack exchange,提问作者Tharindu Sandaruwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:45:00