none
Client side event from server side object RRS feed

  • Question

  • Hi,

    I call a web api and pass over an object from the client, the object on the client side has an event, which is triggered when one of the object properties changes. However, the event (client side) never gets triggered when the objects property changes (the server side). Is there anyway of resolving this?

    My web api call is as follows:

                                            Uri uri = new Uri(url);
                        client.Headers["content-type"] = "application/json";

    var json = JsonConvert.SerializeObject(rw, Formatting.Indented); var reqString = Encoding.Default.GetBytes(json); var result = client.UploadData(url, "POST", reqString);

    Regards,

    Ben


    • Edited by loftty Friday, April 3, 2020 8:41 AM
    Friday, April 3, 2020 8:40 AM

Answers

  • Events don't work that way. An event is only usable inside the same process. When you call an external API your object's fields are converted to a raw format and sent across the wire. What the server does on its end with that data has absolutely nothing to do with your code and none of your code will ever execute over there. The remote end could leave it in its raw format, convert it to an internal type (most common) or do whatever else it wants. You have zero control over any of this.

    Therefore your requirements that you be able to pass an object to a remote server and get notified when the server changes a value on its copy cannot work. There is no workaround. The only way to "know" that the server modified a value is to have the server return to you its version of the data and then you deserialize the data back into a new object and then compare what you sent with what you received. Again, you cannot use .NET events in a client/server architecture like that.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by loftty Friday, April 3, 2020 2:07 PM
    Friday, April 3, 2020 1:39 PM
    Moderator

All replies

  • Hello,

    Code shown does not show any subscriptions to any events, how are you setting up an event and what event? If this is ASP.NET based solution use the following forum.

    https://forums.asp.net/


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 3, 2020 10:25 AM
    Moderator
  • Hi, thanks for your reply.

    This is a windows form app. The rw object has a property change event subscribed to it. The object is then passed via the web api call, which changes a properties value. The event on the client side (rw) never gets called when changing the property value on the server.

    regards,

    Ben

    Friday, April 3, 2020 10:52 AM
  • Hi, thanks for your reply.

    This is a windows form app. The rw object has a property change event subscribed to it. The object is then passed via the web api call, which changes a properties value. The event on the client side (rw) never gets called when changing the property value on the server.

    regards,

    Ben

    Yet you have not shown any code pertaining to the event subscription, only being used in one instance so I can't assist with just this information alone.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 3, 2020 11:36 AM
    Moderator
  • Ok, code with event :) 

    client.Headers.Add("Authorization", "Basic " + auth); var url = ApiAddress.ApiUri + "/api/runworkertest"; Uri uri = new Uri(url); client.Headers["content-type"] = "application/json";

    rw.ValueChanged += Rw_ValueChanged;

    var json = JsonConvert.SerializeObject(rw, Formatting.Indented); var reqString = Encoding.Default.GetBytes(json); var result = client.UploadData(url, "POST", reqString); var rwresult = Encoding.Default.GetString(result);

    Which has the following event:

            private void Rw_ValueChanged(object sender, PropertyChangedEventArgs e)
            {
      
            }
    And the web api
            [HttpPost]
            [Route("runworkertest")]
            public string RunWorkerTest([FromBody] RunWorker runworker)
            {
                runworker.PositionValue = "Hello";
            }
    Regards,


    Ben

    Friday, April 3, 2020 12:19 PM
  • Events don't work that way. An event is only usable inside the same process. When you call an external API your object's fields are converted to a raw format and sent across the wire. What the server does on its end with that data has absolutely nothing to do with your code and none of your code will ever execute over there. The remote end could leave it in its raw format, convert it to an internal type (most common) or do whatever else it wants. You have zero control over any of this.

    Therefore your requirements that you be able to pass an object to a remote server and get notified when the server changes a value on its copy cannot work. There is no workaround. The only way to "know" that the server modified a value is to have the server return to you its version of the data and then you deserialize the data back into a new object and then compare what you sent with what you received. Again, you cannot use .NET events in a client/server architecture like that.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by loftty Friday, April 3, 2020 2:07 PM
    Friday, April 3, 2020 1:39 PM
    Moderator
  • Hi Michael,

    I did think that was the case, just wondered if there was some special way or not.

    Regards,

    Ben

    Friday, April 3, 2020 2:07 PM