Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Passing data between EWB client and a web server via C# - an example
Tue, Sep 27 2016 4:27 AMPermanent Link

Bill

I’m working in this scenario:
1) There is (‘ll be) a Web app for an intranet written with EWB;
2) There is a web server backend written in MS VS 2010;
3) On server side, for customer specification and limits on administrative operations on MS IIS, it’s allowed only write backend in MS VS and isn’t possible use other tecnologies then aspx pages.

I have had some problems in communications with EWB rich client to solve:
1) Querying database(s) that is done by an http GET request;
2) Updating database(s) that is done by an http POST request.

In this situation, I would like to share my experience for helping other people who has same problem and for finding better (or more concise solutions in C#).
Following some code to have a starting point (maybe, it could be use by Tim or someone to build an example to study on). Feel free to give some suggestions.
Tue, Sep 27 2016 4:28 AMPermanent Link

Bill

1. Server side web application
I have a standard web page where I give some general informations on the web app and I set redirect to the starting page of our rich client produced by EWB.

1 Server side web application: the entry point (an empty web form) for getting database data.

I have an asp.net (empty) web form where I set only the Page_Load method as follows and call some methods to produce customized json answer.
In this method, if it’s an http GET request, parameters are read from querystring and the json answer is produced; if it’s an http POST request (and thus an EWB Database.Commit, it’s read transferred data and is taken some action)
Please note that if request isn’t well formed or user isn’t allowed in the operation, a page redirect to the HomePage is done.


1.1 Server side web application: the entry point for requesting and updating data

   protected void Page_Load(object sender, EventArgs e)
   {
     if (!IsPostBack)
     {
    //verifying some way if user can ask info…
       if (!utenteCorrente.BlockingError)
       {
         switch (Request.RequestType) { //Understanding what EWB client is requesting
           case ("GET"):// It’s a reading data from DB request
             string json = "";
             string nmDs = Request.QueryString["dataset"];
             switch (nmDs){
        case (“MyDataset or MyVirtualQueryName”):
           json = BuildJsonForMyDataset();
           break;   
               case ("Segnalazione"):
                 json = BuildJsonQuerySegnalazione();
                 break;
         // and so on   
             }
             if (json == "") { // request not wellformed? redirect
               Response.Redirect("~/Default.aspx");
             }
             // Producing JSON response…
             Response.ContentType = "application/json";
             Response.Write(json);
             Response.End();
             break;
           case ("POST")://It’s an EWB Database.Commit command
             UpdateMyDataset(Request.InputStream);
             break;
           default:
             Response.Redirect("~/Default.aspx");
             break;
         }
       } else { //User not allowed.
         Response.Redirect("~/Default.aspx");
       }
     } else { // it’s a PostBack (some hacking trying?)
       Response.Redirect("~/Default.aspx");
     }
   }
Tue, Sep 27 2016 4:29 AMPermanent Link

Bill

Producing JSON data for an easy query from a LINQ query using NewtonSoft Json.Net popular free library.

   protected string BuildJsonQuerySegnalazione()
   {
     string risultato = "";
     string sIdSegnalazione = Request.QueryString["Id"].Replace("'", "");
     int IdSegnalazione = 0;
     if (Int32.TryParse(sIdSegnalazione, out IdSegnalazione))
     {
       MyDbDataContext db = new MyDbDataContext();
       var queSegn =
         from seg in db.Segnalazione
         where seg.Id == IdSegnalazione
         select new
         { // needed to avoid "circular reference error"
           Id = seg.Id,
           UserName = seg.Utente,
           PhoneNo = seg.Telefono,
           Email = seg.EMail,
           DescriptionOfProblem = seg.Descrizione
         };
       risultato = "{ \"rows\":" + JsonConvert.SerializeObject(queSegn) + "}";
     }
     return risultato;
   }//end;
Tue, Sep 27 2016 4:30 AMPermanent Link

Bill

Interpreting POST request

When the EWB client is asking a Database.Commit(), it’s little more complicated situation.
In fact, you have to read json request and do some actions.
First of all, you have to get input stream received from the server.
I write this general class (Operation) for getting data and a static class for setting values found in the EWB database.commit. As a commit could be composed by some operations, we have to get them in a sequence. Note that it’s impossible using .Net List as they refuse null fields (and in some requests from EWB we receive null fields), so dynamic array is used.
As before, it’s used Json.Net (de facto standard in .net world) to serialize and deserialize data.

 public class Operation
 {
   public string dataset { get; set; }
   public int operation { get; set; }
   public JToken beforerow { get; set; }
   public JToken afterrow { get; set; }
 }


public static class ElaboraOperazioni
 {
   /// <summary>
   /// Static method to produce an array of (updates) operations from json string as setted by EWB client
   /// </summary>
   /// <param name="json"> A string containing json formatted query</param>
   /// <returns></returns>
   public static Operation[] LoadFromJson(string json)
   {
     Operation[] result = null;

     JObject updateObjs = JObject.Parse(json);
     IList<JToken> jsonOps = updateObjs["operations"].Children().ToList();
     int numOfOps = jsonOps.Count();
     result = new Operation[numOfOps];
     int contatore = 0;
     foreach (JToken opSingJson in jsonOps)
     {
       Operation tmpOp = new Operation();
       tmpOp.dataset = (string)opSingJson["dataset"];
       tmpOp.operation = (int)opSingJson["operation"];
       tmpOp.beforerow = opSingJson["beforerow"];
       tmpOp.afterrow = opSingJson["afterrow"];
       result[contatore] = tmpOp;
     }
     return (result);
   }
   // ===============================

   /// <summary>
   /// Static method to produce an array of (updates) operations from a stream containing json command as setted by EWB client
   /// </summary>
   /// <param name="inpStream"></param>
   /// <returns></returns>
   public static Operation[] LoadFromHttpStream(System.IO.Stream inpStream)
   {
     string jsonIn = "";
     using (var reader = new System.IO.StreamReader(inpStream))
     {
       // TODO: see if reader is at start of the stream
       jsonIn = reader.ReadToEnd();
     }
     return (LoadFromJson(jsonIn));
   }
   // ===============================
 } // end ==> public static class ElaboraOperazioni



1.4 Updating dataset
Now it’s necessary write a customized class (or two) to get deserialized values for named dataset in every operation.
For instance you can have

public class JsSegnalazione_row
{
 public int Id { get; set; }
 public string UserName { get; set; }
 public object Phone { get; set; }
 public object EMail { get; set; }
 public object DescriptionOfProblem { get; set; }

 public JsSegnalazione_row() //constructor
 {
   Id = 0;
   UserName = "";
   PhoneNo = "";
   EMail = "";
   DescriptionOfProblem = "";
 }
}

public class JsSegnalazione_Operation
{
 public string dataset { get; set; }
 public int operation { get; set; }
 public JsSegnalazione_row beforerow { get; set; }
 public JsSegnalazione_row afterrow { get; set; }
}

Objects of the above classes, could be setted from generic type Operation.
Finally, we have to do db updates getting values and produced a json response to EWB client.
Tue, Sep 27 2016 4:31 AMPermanent Link

Matthew Jones

Bill wrote:

> In this situation, I would like to share my experience for helping
> other people who has same problem and for finding better (or more
> concise solutions in C#).

I think that would be good. I'm doing a system with a C# back end using
the self-hosted web service, and it all appears to be going well. It
isn't ASP.NET though, so that will ahve differing quirks I'm sure. The
key though from EWB's point of view is that the TServerRequest does
pretty much all one needs in the browser.

--

Matthew Jones
Tue, Sep 27 2016 5:10 AMPermanent Link

Matthew Jones

Bill wrote:

> JSON data for an easy query from a LINQ query

I really must learn this part. Right now I have objects for inbound and
outbound. Worth also mentioning that you can use attributes when you
are serialising objects to not output certain parameters. Also, you can
get an event when you are finished serialising to an object.

Thanks for posting these samples.

--

Matthew Jones
Mon, Oct 3 2016 10:33 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Bill,

Thanks for the code, it is very helpful.

Tim Young
Elevate Software
www.elevatesoft.com
Image