How to serialize objects in flex for c # backend

We are developing an application with a Flex interface and a C # core server connected through web services. We use the FLex Builder 3 Web Services Manager to automatically create webservice classes. The problem arises when Flex serializes our objects, for example, when we have the Number property without a value, it serializes as NaN, and our backend does not know about NaN. Another example is when we serialize a circular reference, we get a stack exception in the stream. I know, for example, in java, you can configure the way xstream works with a circular reference ... So, the question is, can we change the way serialization of Flex objects so that C # can work with this interface? Is there any problem we need to know about how to serialize objects flexibly? Thank you Jorge

+3
source share
4 answers

I think you should consider a slightly different design, we also had a similar problem, but we developed the following solution.

Primitive Arguments Only

Define only primitive types in web service method arguments (string, int), etc.

For example, you have a class,

class Person{
    long PersonID;
    long MembershipID;
    string Name;
}

And you want to change the "Name" property to a backend, however, you should not trust your Frontend, you should refer to the fact that the interface can easily be hacked and send incorrect data, for example, MemberhipID in this case the field should not be changed using the interface.

, , - WSDL .

, .

[WebMethod] 
public void SavePerson(Person p){
    p.Save(); <-- this is dangerous
}

[WebMethod] 
public void SavePersonName(long personID, string name){
    Person p = GetPersonByID(personID);
    p.Name = name;
    p.Save();
}

, , , , , Wrapper.

Wrapper

// I will only define the fields I want to pass to server
// this needs to be done at backend level
class PersonAddressWrapper{
    long PersonAddressID;
    string AddressLine1;
    string AddressLine2;
    string ZipCode;
    string City;
    string Country;
}

class PersonWrapper{
    long PersonID;
    string Name;
    PersonAddressWrapper[] Addresses; 
}

[WebMethod]
public void SavePerson(PersonWrapper pw){
    Person p = GetPersonByID(pw.PersonID);
    p.Name = pw.Name;
    p.Save();
    foreach(PersonAddressWrapper paw in pw.Addresses){
        PersonAddress pa = GetPersonAddressByID(paw.PersonAddressID);
        Copy(pa,paw);
        pa.Save();
    }
}

, , - / , .

, , , .

+1

, Flex Blueider Web Service Manager, Proxy ActionScript -. -. , # ( NaN).

, , - XML XML. Adobe .

0

, , :

, , , ( , , ):

:

, :

  • ActionScript

, :

  • Java,
  • - .Net, . , SDK, .
0

, .

FlexBuilder3, . "call()" "Base [ServiceName]".

, , "soap"

message.body = soap.toString();

In my case, I made a cleanXML function (see below). This function removes all XML elements that are "NaN" or that have the xsi attribute: nil = 'true'

private function cleanXML(xml:XML, qn:QName):void{
    if (XMLList(xml).hasSimpleContent()){
        if ((xml.valueOf() == "NaN") || (xml.attribute(qn) == true)){
            delete xml.parent().children()[xml.childIndex()];
        }
    }else{      
        for each (var item:XML in xml.children()){
            cleanXML(item, qn);
        }   
    }
} 

Thus, the mentioned section of the call () function:

    var soapList:XML;
    soapList = soap as XML;

    var qn:QName = new QName(soapList.namespace("xsi"), "nil");
    cleanXML(soapList, qn);         

    message.body = soapList.toString();

I know that this solution is not very good, because in my situation it works great!

0
source

Source: https://habr.com/ru/post/1718585/


All Articles