Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Json error: A method was called at an unexpected time

09.08.2018 (πŸ‘14027)


 

Problem:

When evaluating an Api interface which returns a pure value list, the error comes that the method was called at an unexpected time.

 

Solution:

When evaluating Json returns, the return values ​​are evaluated using JsonObjects built-in data models.

If they are pure strings or numbers, then you must evaluate the values ​​with getNumber ().

 

 

Json error:

System.InvalidOperationException

  HResult=0x8000000E

  Message=
A method was started at an unexpected time

Eine Methode wurde zu einem unerwarteten Zeitpunkt aufgerufen.

 

Dies ist kein Objektwert. Verwenden Sie die ValueType-Eigenschaft zum Abrufen des Typs.

  Source=Windows.Data

  StackTrace:

   at Windows.Data.Json.JsonValue.GetObject()

..

 

Error in the conversion

There are pure numerical values ​​of the type Long.

JsonObject jsonObject = jsonRow.GetObject();

 

Name

Value

Type

β–Ά

jsonArray

{[10073,10072,10071,10070,10069,10068,10067,10066,10065,10064,10063]}

Windows.Data.Json.JsonArray

β–Ά

jsonRow

{10073}

Windows.Data.Json.IJsonValue {Windows.Data.Json.JsonValue}

jsonRow.GetNumber()

10073

double

 

The conversion from an asp api to the return of a data model has so far been converted like this code.

The data model is read into a Json object and the individual field value is converted with jsonObject ["field"]. ToString ()

JsonObject jsonObject = jsonRow.GetObject();

 

//< values >

string sID_on_Server = jsonObject["idProject"].ToString();

//</ values >

 

long ID_on_Server = 0;

try

{

    ID_on_Server = Convert.ToInt64(sID_on_Server);

}

catch { }

 

 

 

Solution code

In the new code, the value is read directly as a double from the json variable with .GetNumber ()

double dblValue = jsonRow.GetNumber();

long ID_on_Server = Convert.ToInt64( dblValue);

 

 

Api on the server side:

On the Asp.Net Core MVC WebApi page will return a list of long values.

Without data model but only number series.

List<long> dataList = query.ToList();

 

 

// GET: /api/index

public async Task<List<long>> Index()

{

    //-------------< Index >------------- 

    //< get UserClaim Info >

    //*get User from Token

    var userClaim_in_Token = HttpContext.User.Claims.Where(c => c.Type == ClaimsIdentity.DefaultNameClaimType).FirstOrDefault(); //User as Name

    if(userClaim_in_Token==null)

    {

        return null;

    }

    string sEmail = userClaim_in_Token.Value;

    //</ get UserClaim Info >

 

    //< check user >

    long IDCurrent_User = await Get_UserID(sEmail);

    if (IDCurrent_User == 0) { return null; }

    //</ check user >

 

 

    //--< Get Linq.Query >--

    //*gets last 10 Projects with View_Sum

    var query = (from project in _dbContext.tbl_Projects

                 where project.IDOwner == IDCurrent_User

                 orderby project.IDProject descending

                 select  project.IDProject).Take(3000);

    //--</ Get Linq.Query >--

 

 

    //----< fill Data_to_View >----

    List<long> dataList = query.ToList();

    

 

    //< out >

    //*output to client

    return dataList;

    //</ out >

    //-------------</ Index >------------- 

}