2

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);
            }
        }
    }
}

2つのエラーが発生し、これを理解できないようです

  1. 引数 1: 'FetchNewOrdersJob.MarketplaceWebServiceOrders' から 'MarketplaceWebServiceOrders.MarketplaceWebServiceOrders' に変換できません

  2. 「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);
        }
    }
}
4

2 に答える 2

2

これは、Web サービス オブジェクトをインポートした方法 (と思われます) に関係していると思います。WCF サービスの場合は、「モデル」がさまざまなプロジェクトで参照されていることを確認し、サービス参照を追加するときは、「既存の型を再利用する」オプションを確認してください。

于 2013-01-23T16:58:33.480 に答える
1

現在の名前空間と名前空間MarketplaceWebServiceOrdersの両方にクラスがあります(最後の名前空間もクラスと同じ名前です)。クラスが名前空間で宣言されているようです。そして、その名前空間でも宣言されている引数として期待します。しかし、現在の名前空間で宣言されているクラスを渡そうとしています。FetchNewOrdersJobMarketplaceWebServiceOrdersOrderFetcherMarketplaceWebServiceOrdersMarketplaceWebServiceOrdersMarketplaceWebServiceOrdersFetchNewOrdersJob

FetchNewOrdersJob.MarketplaceWebServiceOrdersこれらの競合を避けるために、クラスの名前を変更してください。

于 2013-01-23T16:53:38.437 に答える