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

使用Nager Amazon Product Advertising NuGet包编译报错求助

解决Nager.AmazonProductAdvertising编译错误的方案

1. 补充必要的命名空间引用

你遇到的CS0246错误,是因为代码未引入目标类型所在的命名空间。在代码文件顶部添加以下引用:

using Nager.AmazonProductAdvertising;
using Nager.AmazonProductAdvertising.Model;

AmazonProductAdvertisingClient属于根命名空间,SearchRequest则在Model子命名空间下,添加后即可识别这些类型。

2. 修正AmazonAuthentication构造函数调用

CS1729错误是因为使用的包版本与示例代码不匹配。新版本的AmazonAuthentication需要三个参数:AccessKey、SecretKey和PartnerTag(亚马逊联盟ID),正确写法如下:

var auth = new AmazonAuthentication("你的AccessKey", "你的SecretKey", "你的PartnerTag");

如果是旧版本包,构造函数参数可能不同,直接查看当前安装的NuGet包版本说明即可确认。

3. 确保NuGet包安装完整

包安装不完整会导致程序集未被正确引用,按以下步骤检查:

  • 右键项目 → 管理NuGet程序包,确认Nager.AmazonProductAdvertising已成功安装,且版本支持.NET Framework 4.8(该包最低支持4.6.1,4.8完全兼容)。
  • 若发现引用缺失,先卸载包,再重新安装一次。

4. 适配版本的示例代码参考

如果示例代码基于旧版包编写,这里提供适配最新版本的基础运行示例:

using System;
using Nager.AmazonProductAdvertising;
using Nager.AmazonProductAdvertising.Model;

namespace AmazonAdsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var auth = new AmazonAuthentication("你的AccessKey", "你的SecretKey", "你的PartnerTag");
            var client = new AmazonProductAdvertisingClient(auth, AmazonEndpoint.US);
            
            var searchReq = new SearchRequest
            {
                Keywords = "Amazon Echo",
                SearchIndex = SearchIndex.Electronics
            };
            
            var searchResp = client.SearchItemsAsync(searchReq).GetAwaiter().GetResult();
            if (searchResp.SearchResult != null)
            {
                foreach (var item in searchResp.SearchResult.Items)
                {
                    Console.WriteLine(item.ItemInfo.Title.DisplayValue);
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Jason Kilhoffer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 12:23:29