匹配的最佳重载方法有一些无效参数

Posted

技术标签:

【中文标题】匹配的最佳重载方法有一些无效参数【英文标题】:The best overloaded method match for has some invalid arguments 【发布时间】:2013-01-07 05:45:30 【问题描述】:

我正在使用 VS 2010 C#。我的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using MarketplaceWebServiceOrders;
using MarketplaceWebServiceOrders.Model;

namespace FetchNewOrdersJob

    public class MarketplaceWebServiceOrders
    
        private volatile bool isRunning;
        private OrderFetcher orderFetcher;

        private TimeSpan _checkOrdersInterval = TimeSpan.FromMinutes(15.0);
        /// <summary>
        /// Gets or sets the order check interval.  Defaults to 15 minutes.
        /// </summary>
        public TimeSpan CheckOrdersInterval
        
            get  return _checkOrdersInterval; 
            set  _checkOrdersInterval = value; 
        

        /// <summary>
        /// Internal method to handle an order.
        /// </summary>
        protected virtual void HandleOrder(Order order)
        
            Console.WriteLine("Processing Order:");
            Console.WriteLine("---------------------------------------------------");
            Console.WriteLine(order.ToString());
            // Fetch the order items in each order
            orderFetcher.FetchOrderItems(order.AmazonOrderId, delegate(OrderItem item)
            
                Console.WriteLine("\tProcessing Order Item");
                Console.WriteLine("\t---------------------------------------------------");                // Process order item here.
                Console.WriteLine("\t" + item.ToString().Replace("\n", "\n\t"));
            );

            Console.WriteLine("=================================================");
            Console.WriteLine();
        

        /// <summary>
        /// Method to continuously check orders over an interval, and list OrderItems for those Orders.
        /// </summary>
        private void OrdersJobThread(object obj)
        
            orderFetcher.ProcessOrder += HandleOrder;

            if (this.CheckOrdersInterval == TimeSpan.MinValue)
            
                throw new ArgumentException("The CheckOrdersInterval TimeSpan cannot be zero.", "CheckOrdersInterval");
            

            DateTime startCheckInterval = DateTime.Now.Subtract(CheckOrdersInterval);

            // Continue forever until the isRunning flag is cleared.
            while (isRunning)
            
                try
                
                    // Check the orders for this interval.
                    DateTime checkInterval = startCheckInterval;
                    startCheckInterval = DateTime.Now.Subtract(TimeSpan.FromMinutes(3.0));

                    Console.WriteLine("Fetching orders from " + checkInterval.ToString() + " to " + startCheckInterval.ToString());
                    orderFetcher.FetchOrders(checkInterval, startCheckInterval);

                    // Wait for the next interval.
                    Console.WriteLine("Fetch complete.  Sleeping until next interval.");
                    while (isRunning && DateTime.Now.Subtract(startCheckInterval) < CheckOrdersInterval)
                    
                        Thread.Sleep(1000);
                    
                
                catch(Exception err)
                
                    Console.WriteLine("Error: " + err.Message + ".  Orders job thread is exiting.");
                    isRunning = false;
                
            
        

        /// <summary>
        /// Sample code to invoke the OrderFetcher.
        /// </summary>
        /// <param name="service">MarketplaceWebServiceOrders object.</param>
        /// <param name="sellerId">The seller Id.</param>
        /// <param name="marketplaceIdList">List of marketplaces passed in to the GetOrders call.</param>
        public static void InvokeOrderFetcherSample(
            MarketplaceWebServiceOrders service,
            string sellerId,
            string [] marketplaceIdList
        )
        
            // Create a FetchOrderUpdates job with the default time span.
            MarketplaceWebServiceOrders job = new MarketplaceWebServiceOrders();
            job.isRunning = true;
            job.orderFetcher = new OrderFetcher(service, sellerId, marketplaceIdList);

            Thread jobThread = new Thread(job.OrdersJobThread);
            jobThread.IsBackground = true;
            jobThread.Start();

            // Pause on the main thread for one hour or until the thread exits, then end the job.
            jobThread.Join(1000 * 60 * 60);
            job.isRunning = false;

            // Block until the thread terminates to prevent any requests in progress from being aborted.
            while (jobThread.IsAlive)
            
                Thread.Sleep(1000);
            
        
    

我遇到两个错误,但似乎无法弄清楚

    参数 1:无法从“FetchNewOrdersJob.MarketplaceWebServiceOrders”转换为“MarketplaceWebServiceOrders.MarketplaceWebServiceOrders”

    'MarketplaceWebServiceOrders.OrderFetcher.OrderFetcher(MarketplaceWebServiceOrders.MarketplaceWebServiceOrders, string, string[])' 的最佳重载方法匹配有一些无效参数

错误的行号是112:job.orderFetcher = new OrderFetcher(service, sellerId, marketplaceIdList);

OrderFetcher.cs

using System;
using System.Collections.Generic;
using System.Text;

using MarketplaceWebServiceOrders.Model;

namespace MarketplaceWebServiceOrders
    
    /// <summary>
    /// Sample helper class to Fetch Orders and OrderItems using the Amazon MWS Orders API.
    /// </summary>
    public class OrderFetcher
    
        public delegate void RetriableMethodCall();
        public delegate void ProcessOrderHandler(Order order);
        public delegate void ProcessOrderItemHandler(OrderItem orderItem);

        /// <summary>
        /// Default amount of time, in milliseconds, to sleep if a request is throttled; default to 1 per 10 minutes.
        /// </summary>
        public const int DEFAULT_THROTTLED_WAIT_TIMEOUT = 10 * 60 * 1000;

        /// <summary>
        /// Default throttling limit for ListOrders calls; default to 1 per 12 seconds.
        /// </summary>
        private const int LIST_ORDERS_DEFAULT_THROTTLE_LIMIT = 12 * 1000;

        /// <summary>
        /// Default throttling limit for ListOrderItems calls; default to 1 per 100 minutes.
        /// </summary>
        private const int LIST_ORDER_ITEMS_DEFAULT_THROTTLE_LIMIT = 10 * 60 * 1000;

        private MarketplaceWebServiceOrders mwsService;
        private string mwsSellerId;
        private string[] mwsMarketplaceIdList;
        private DateTime lastServiceCall = DateTime.MinValue;

        private ProcessOrderHandler _processOrder;
        /// <summary>
        /// Event called when an order is received for processing.
        /// </summary>
        public event ProcessOrderHandler ProcessOrder
        
            add  _processOrder += value; 
            remove  _processOrder -= value; 
        

        /// <summary>
        /// Creates a new instance of the OrderFetcherSample class.
        /// </summary>
        /// <param name="service"></param>
        public OrderFetcher(MarketplaceWebServiceOrders service, string sellerId, string[] marketplaceIdList)
        
            mwsService = service;
            mwsSellerId = sellerId;
            mwsMarketplaceIdList = marketplaceIdList;
        

        /// <summary>
        /// Fetches all orders created between the starting time and the server's
        /// local system time minus two minutes.
        /// <param name="startTime">The starting time period of orders to fetch.</param>
        public void FetchOrders(DateTime startTime)
        
            FetchOrders(startTime, DateTime.MinValue);
        

        /// <summary>
        /// Fetches all orders created in the given time period and processes them locally.        
        /// <param name="startTime">The starting time period of orders to fetch.</param>
        /// <param name="endTime">The ending time period of orders to fetch.</param>
        public void FetchOrders(DateTime startTime, DateTime endTime)
        
            ListOrdersRequest request = new ListOrdersRequest();
            request.CreatedAfter = startTime;
            if (endTime != DateTime.MinValue)
            
                request.CreatedBefore = endTime;
            
            request.SellerId = mwsSellerId;
            request.MarketplaceId = new MarketplaceIdList();
            request.MarketplaceId.Id = new List<string>();
            foreach (string marketplaceId in mwsMarketplaceIdList)
            
                request.MarketplaceId.Id.Add(marketplaceId);
            

            List<Order> orderList = new List<Order>();
            ListOrdersResponse response = null;
            OrderFetcher.InvokeRetriable(LIST_ORDERS_DEFAULT_THROTTLE_LIMIT, delegate()
            
                response = mwsService.ListOrders(request);
                ProcessOrders(response.ListOrdersResult.Orders.Order);
            );

            String nextTokenString = response.ListOrdersResult.NextToken;

            while (!string.IsNullOrEmpty(nextTokenString))
            
                // If NextToken is set, continue looping through the orders.
                ListOrdersByNextTokenRequest nextRequest = new ListOrdersByNextTokenRequest();
                nextRequest.NextToken = nextTokenString;
                nextRequest.SellerId = mwsSellerId;

                ListOrdersByNextTokenResponse nextResponse = null;
                OrderFetcher.InvokeRetriable(LIST_ORDERS_DEFAULT_THROTTLE_LIMIT, delegate()
                
                    nextResponse = mwsService.ListOrdersByNextToken(nextRequest);
                    ProcessOrders(nextResponse.ListOrdersByNextTokenResult.Orders.Order);
                );

                nextTokenString = nextResponse.ListOrdersByNextTokenResult.NextToken;
            
        

        /// <summary>
        /// Method called by the FetchOrders method to process the orders.
        /// </summary>
        /// <param name="orders">List of orders returned by FetchOrders</param>
        protected virtual void ProcessOrders(List<Order> orders)
        
            foreach (Order order in orders)
            
                if (_processOrder != null)
                
                    _processOrder(order);
                
            
        

        /// <summary>
        /// Fetches the OrderItems for the specified orderId.
        /// </summary>
        public void FetchOrderItems(string orderId, ProcessOrderItemHandler handler)
        
            if (handler == null) throw new ArgumentNullException("handler");

            ListOrderItemsRequest request = new ListOrderItemsRequest();
            request.SellerId = mwsSellerId;
            request.AmazonOrderId = orderId;

            ListOrderItemsResponse response = null;
            OrderFetcher.InvokeRetriable(LIST_ORDER_ITEMS_DEFAULT_THROTTLE_LIMIT, delegate()
            
                response = mwsService.ListOrderItems(request);
                foreach (OrderItem orderItem in response.ListOrderItemsResult.OrderItems.OrderItem)
                
                    handler(orderItem);
                
            );

            String nextTokenString = response.ListOrderItemsResult.NextToken;

            while (!string.IsNullOrEmpty(nextTokenString))
            
                // If NextToken is set, continue looping through the orders.
                ListOrderItemsByNextTokenRequest nextRequest = new ListOrderItemsByNextTokenRequest();
                nextRequest.NextToken = nextTokenString;
                nextRequest.SellerId = mwsSellerId;

                ListOrderItemsByNextTokenResponse nextResponse = null;
                OrderFetcher.InvokeRetriable(LIST_ORDER_ITEMS_DEFAULT_THROTTLE_LIMIT, delegate()
                
                    nextResponse = mwsService.ListOrderItemsByNextToken(nextRequest);
                    foreach (OrderItem orderItem in nextResponse.ListOrderItemsByNextTokenResult.OrderItems.OrderItem)
                                            
                        handler(orderItem);
                    
                );

                nextTokenString = nextResponse.ListOrderItemsByNextTokenResult.NextToken;
            
        

        /// <summary>
        /// Invokes a method in a retriable fashion.
        /// </summary>
        /// <param name="throttledWaitTime">The amount of time to wait if the request is throttled.</param>
        /// <param name="method">The method to invoke.</param>
        public static void InvokeRetriable(RetriableMethodCall method)
        
            InvokeRetriable(DEFAULT_THROTTLED_WAIT_TIMEOUT, method);
        

        /// <summary>
        /// Invokes a method in a retriable fashion.
        /// </summary>
        /// <param name="throttledWaitTime">The amount of time to wait if the request is throttled.</param>
        /// <param name="method">The method to invoke.</param>
        public static void InvokeRetriable(int throttledWaitTime, RetriableMethodCall method)
        
            bool retryRequest = false;
            do
            
                retryRequest = false;
                try
                
                    // Perform some action
                    method.Invoke();
                
                catch (MarketplaceWebServiceOrdersException ordersErr)
                
                    // If the request is throttled, wait and try again.
                    if (ordersErr.ErrorCode == "RequestThrottled")
                    
                        Console.WriteLine("Request is throttled; waiting...");
                        retryRequest = true;
                        System.Threading.Thread.Sleep(throttledWaitTime);
                    
                    else
                    
                        // On any other error, re-throw the exception to be handled by the caller
                        throw;
                    
                
             while (retryRequest);
        
    

【问题讨论】:

出现错误的代码行是什么?请具体,否则你会被否决。 很抱歉。谢谢指出 第 112 行 job.orderFetcher = new OrderFetcher(service, SellerId, marketplaceIdList); 如果您还可以显示OrderFetcher 的构造函数,那将很有帮助。 我在问题中添加了 OrderFetcher.cs 代码 【参考方案1】:

我认为这可能与您导入(我假设)Web 服务对象的方式有关。如果它是 WCF 服务,请确保您的“模型”在不同的项目中被引用,并且在添加服务引用时,请确保选中“重用现有类型”选项。

【讨论】:

好的,我解决了这个问题。我在“Samples”文件夹中有所有 cs 文件的副本,除了 .Sample 之外,每个文件的名称都相同。我删除了示例文件夹,它解决了问题。【参考方案2】:

您在当前的FetchNewOrdersJobMarketplaceWebServiceOrders 命名空间中都有类MarketplaceWebServiceOrders(最后一个命名空间也与您的类同名)。看起来您的 OrderFetcher 类是在 MarketplaceWebServiceOrders 命名空间中声明的。它期望作为参数MarketplaceWebServiceOrders,它也在该命名空间中声明。但是您正在尝试传递在当前 FetchNewOrdersJob 命名空间中声明的 MarketplaceWebServiceOrders 类。

重命名您的 FetchNewOrdersJob.MarketplaceWebServiceOrders 类以避免这些冲突。

【讨论】:

我没有 FetchNewOrdersJob.MarketplaceWebServiceOrders 类。但是,在另一个构建之后,所有错误都以某种方式解决了。我得到一个不同的......找不到类型或命名空间 OrderFetcher 好的,我解决了这个问题。我在“Samples”文件夹中有所有 cs 文件的副本,除了 .Sample 之外,每个文件的名称都相同。我删除了示例文件夹,它解决了问题。 @webby68 那么我认为你应该关闭这个问题:)

以上是关于匹配的最佳重载方法有一些无效参数的主要内容,如果未能解决你的问题,请参考以下文章

最佳重载方法匹配...有一些无效参数

C# 匹配的最佳重载方法...有一些无效参数

需要帮助 'string.endswith(string)' 的最佳重载方法匹配有一些无效参数

最佳重载方法匹配有一些无效参数 C#

错误:“最佳重载方法匹配”和“参数 1:无法从 'string' 转换为 'int'”

说出几条 Java 中方法重载的最佳实践?