CXuesong / WikiClientLibrary

/*🌻*/ Wiki Client Library is an asynchronous MediaWiki API client library targeting modern .NET platforms
https://github.com/CXuesong/WikiClientLibrary/wiki
Apache License 2.0
82 stars 16 forks source link

"Value was either too large or too small for an Int32." #39

Closed aquilla12 closed 6 years ago

aquilla12 commented 6 years ago

I'm getting a Int32OverflowExeption when accessing Wikia through your client. Here's my code:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using System.Linq;
using WikiClientLibrary.Client;
using WikiClientLibrary.Sites;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Wikia;
using Discord.Commands;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using WikiClientLibrary.Wikia.Sites;

namespace EC_Discord_bot_CS
{
    class Program
    {
        public static WikiaSite site;

        private CommandService commands;

        private IServiceProvider services;

        private DiscordSocketClient discordClient;

        static void Main(string[] args)
        {
            new Program().MainAsync().Wait();
        }

        public async Task MainAsync()
        {
            #region wikia client
            var wikiaClient = new WikiClient
            {
                ClientUserAgent = "Edge-bot"
            };

            try
            {
                WikiaSiteOptions options = new WikiaSiteOptions()
                {
                    ApiEndpoint = "http://theedgechronicles.wikia.com/api.php",
                    WikiaApiRootUrl = "http://theedgechronicles.wikia.com/api/v1",
                };
                site = new WikiaSite(wikiaClient, options, "username", "password"); //yes, I used my real username and password
                await site.Initialization; //Exception here
            }
            catch (Exception EX)
            {
                Console.WriteLine(EX.Message);
            }
            #endregion
     }
}

Here's the Exception message:

{System.OverflowException: Value was either too large or too small for an Int32.
   at System.Convert.ThrowInt32OverflowException()
   at System.Convert.ToInt32(Int64 value)
   at Newtonsoft.Json.JsonReader.ReadAsInt32()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
   at Newtonsoft.Json.Linq.JToken.ToObject[T]()
   at WikiClientLibrary.Wikia.Sites.WikiaSite.<RefreshSiteInfoAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at WikiClientLibrary.Sites.WikiSite.<>c__DisplayClass18_0.<<-ctor>g__InitializeAsync|1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at EC_Discord_bot_CS.Program.<MainAsync>d__5.MoveNext() in C:\Users\ADMIN\Documents\Visual Studio 2017\Projects\EC Discord bot CS\EC Discord bot CS\Program.cs:line 53}

I'm stumped as to how to fix this, as the Exception seems to coming from a value that Newtonsoft tried to parse to an Int32 during the Initialization call.

CXuesong commented 6 years ago

Released v0.6.0. You may update your package dependencies, and try again ^_^

Btw

aquilla12 commented 6 years ago

Thank you very much! It's now working perfectly. Can I dispose the WikiClient after I initialize the WIkiaSite or do I need to keep it until I'm finished making requests?

CXuesong commented 6 years ago

You need to dispose WikiClient after finishing making all your requests to MediaWiki site; before then, keep it alive. Actually, it's the underlying HttpClient that needs disposal.