locked
ServiceOperation and InvokeOperation query RRS feed

  • Question

  • Hi, I'm reading the July 09 preview document and the bit about ServiceOperations being invoked with InvokeOperation.

    6.2.2.2 Usage shows:

    InvokeOperation<decimal> invokeOp = catalog.GetCompetitorsPrice(selectedProduct);

    compPrice.Text = invokeOp.Value.ToString();

    Now, I know the docs say this is still an async operation, but what happens here? Does it block? How do I know invokeOp.Value has loaded? Isn't there a risk that compPrice.Text execute straight away before the op completes? i usually use event handlers for async operations - is this something I've missed and can use beyond Silverlight or is it RIA only?

    Thanks, P.

    Saturday, August 8, 2009 3:30 AM

Answers

  • You would use the value in the callback like this:

     

    InvokeOperation<decimal> invokeOp = catalog.GetCompetitorsPrice(selectedProduct);
    invokeOp.Completed += new EventHandler( DecimalOperationCompleted );
    ...
    private void DecimalOperationCompleted (object sender, EventArgs e)
    {
       InvokeOperation<decimal> op = (InvokeOperation<decimal>)sender;
       compPrice.Text = op.Value.ToString();
    }
    
    
     
    Saturday, August 8, 2009 12:08 PM

All replies

  • Nothing is blocked.

    I'm assuming INotifyPropertyChanged is implemented to handle this stuff which is not limited to RIA Services.

    Saturday, August 8, 2009 9:09 AM
  • Okay, that makes sense as its a text property on a control and would pick up the property change. But what if it was, say, used as input to a method:

     DoSomething(invokeOp.Value.ToString())

     Then this probably wouldn't be a great idea I take it?

    P.

    Saturday, August 8, 2009 9:15 AM
  • You would use the value in the callback like this:

     

    InvokeOperation<decimal> invokeOp = catalog.GetCompetitorsPrice(selectedProduct);
    invokeOp.Completed += new EventHandler( DecimalOperationCompleted );
    ...
    private void DecimalOperationCompleted (object sender, EventArgs e)
    {
       InvokeOperation<decimal> op = (InvokeOperation<decimal>)sender;
       compPrice.Text = op.Value.ToString();
    }
    
    
     
    Saturday, August 8, 2009 12:08 PM
  •  Two quick questions raised by your answer:

     1) If the value being returned is an entity type is it accessed by casting the value in the op to that entity type?

    2) Can you explain why there isn't a window of danger between the invocation of the service and registering for the completion?  I.E., what happens if the service completes before one can add the event handler?

    Wednesday, November 25, 2009 3:26 PM
  • 1) You will get the answer back from the InvokeOperation.Value. If you use the generic version of the InvokeOperation then the value is already the correct type.

    2) If your code is running in the UI thread (as it usually is) then it is impossible for the Invoke to complete before the event is attached as you are in the thread that is going to process the return. That being said, I advocate always passing a callback instead of using the event. It bypasses the "window of danger" element and also prevents a memory leak if you don't remove the event handler.

    catalog.GetCompetitorsPrice(selectedProduct, DecimalOperationCompleted, null);

    ...
    private void DecimalOperationCompleted (InvokeOperation<decimal> op)
    {
       compPrice.Text = op.Value.ToString();
    }

    Wednesday, November 25, 2009 3:43 PM

  •  1) If the value being returned is an entity type is it accessed by casting the value in the op to that entity type?

    The documentation states you can only pass and return simple types like string, int, bool and no entities (which is a showstopper for me).

    Thursday, November 26, 2009 1:59 AM

  •  1) If the value being returned is an entity type is it accessed by casting the value in the op to that entity type?

    The documentation states you can only pass and return simple types like string, int, bool and no entities (which is a showstopper for me).

    True for the VS2008 version, not true for the VS2010 version.

    Thursday, November 26, 2009 2:53 AM

  • True for the VS2008 version, not true for the VS2010 version.

    I tried it with VS2010 and the latest WCF services and indeed I can pass and return an entity via an InvokeOperation. I noticed that associations are not included when the entity arrives on the server, but that's no different from the regular pattern for submitting changes (where the object graph is not restored on the server either --> entity by entity processing).  

     

    Thursday, November 26, 2009 1:44 PM
  • Right, the ability of the Invoke to return an Entity is useful for returning complex data that is not going to be tracked by the DomainContext. Trying to use the Invoke to load an actual Entity that is going to be modified and edited is not a good idea.

    Thursday, November 26, 2009 1:57 PM
  • I guess it's actually not supported, see http://forums.silverlight.net/forums/p/145466/323918.aspx

    mmm... here it states it *is* supported indeed: http://betaforums.silverlight.net/forums/p/146194/326375.aspx. Some consistent documentation would help ;-)

    Thursday, November 26, 2009 2:09 PM