Search code examples
c#workflow-foundation-4refreference-type

Reference-Type passed by ref to another method does not return with updated values


I'm facing the following issue:

I need to make a call from my controller into my domain layer; which calls a web service method that takes in the request by reference (ref).

Controller code:

//BusinessEntityObject is a Reference-Type (BusinessEntity) object
var request = View.BusinessEntityObject; 
_workflowService.PerformAction(request);
if(request.Errors.Count != 0)
{
    View.Errors = request.Errors;
    return false;
}

Domain Layer (WorkflowService.cs class):

public void PerformAction(BusinessEntity request)
{
    //TryAction(System.Action action) basically wraps action in try catch and handles exceptions 
    TryAction(() =>
             {
                 _wcfClient.RequestSomething(ref request);
             });
}

IF _wcfClient.RequestSomething modifies the Errors collection upon return the request object has this errors updated errors collection. However once control is returned back to the controller & the errors collection is checked, then my updates are gone.

Edit00: ohh and shameless plug, I'm at rep 14 and I've tried to bump up a buch of questions/answers that were useful to me and it says that I can't becuase my level is low.

Edit01:Thanks so much Dylan, see always good to have a site like this to point out very small things one might miss out on. The updated code that returned the value to me looks as follows:

Domain Layer (WorkflowService.cs class):

public BusinessEntity PerformAction(BusinessEntity request)
{
    //TryAction(System.Action action) basically wraps action in try catch and handles exceptions 
    TryAction(() =>
             {
                 _wcfClient.RequestSomething(ref request);
                 return request;
             });
}

Solution

  • When you pass an object over to a WCF service it is serialized, sent across the wire, then deserialized on the server. Passing it "by ref" doesn't change anything in this case, if the server makes changes to it, it will not be sent back to the caller. Only the return value of the WCF call is serialized and sent back.

    I would suggest, if you need the WCF service to return any data you package it up into the return value.