Question

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;
             });
}
Was it helpful?

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.

OTHER TIPS

You're missing a ref modifier on your PerformAction method.

public void PerformAction(ref BusinessEntity request)
{
    TryAction(() => _wcfClient.RequestSomething(ref request));
}

However, making this change will prevent your code from compiling. You'll get the following error:

Cannot use ref or out parameter 'request' inside an anonymous method, lambda expression, or query expression

You'll have to do something like this to make it work:

public void PerformAction(ref BusinessEntity request)
{
    var r = request;
    TryAction(() => _wcfClient.RequestSomething(ref r));
    request = r;
}

All up it seems a bit flakey passing the request by reference. It would be better to return a new (or the same) instance and do an assignment at the outer level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top