Selecting connection from client using the parameters in the DataRequestCall

Hi,

I’m looking for a way to indicate the connection to use in the server, from the client. For years, ever since I started using DA, I’ve used the BeforeAcquireConnection method to use the session data to set the connection:

private void DataService_BeforeAcquireConnection(DataAbstractService sender, BeforeAcquireConnectionEventArgs e)
{
	e.ConnectionName = (string)Session["ConnectionName"];;
}

This works fine most of the time, except when I need to use multiple connections. In this case, I need to do something like:

remoteService.SetWorkingCompany("MyCompany1"); // Set the connnection name in the session
var data = ldaDataService.GetTable<MyTable>().ToList();
remoteService.SetWorkingCompany("MyCompany2"); // Set the connnection name in the session
var data = ldaDataService.GetTable<MyTable>().ToList();

This means I need to do an extra remote call before I can get the data. I want to start sending the connection info from the client, in the remote data adapter call, perhaps using a parameter in the DataRequestCall. For example, something like:

ldaDataService.DataRequestCall.ParameterByName("Company_GUID").Value = "xxx";
var data = ldaDataService.GetTable<MyTable>().ToList();

and then create an override in my data service that would get this parameter, and based on this parameter set the connection to be used in the call. I would also check access restrictions to make sure the client has access to that connection, which now is only made during login, but with this change, it would have to be made on every call to the new GetData method. But my main concern is how to send this connection info in the DataRequestCall to avoid the extra remote call before getting the data.

Could you provide some pointers or perhaps a quick example on how to achieve this?

Thanks,
Arturo.

Unfortunately the DataParameter approach won’t work for data updates.
I’ll create a connection switch sample for you after the weekend (in general it is easier to do than it looks like).

So, here is the sample: ConnectionSwitch.zip (120.1 KB)

Connection is selected by client-side code via providing its name as part of the DataService name like DataServvice.ConnectionName (dot is the separator here)

Server-side this composite service name is parsed in the IMessageAwareService implementation (btw v10 will have cleaner API for this interface). Then desired connection name is cached in the session and the currently active connection is switched to the requested one.

The BeforeAcquireConnectionHandler event handler ensures that the cached connection name is used in the subsequent calls to the server, removing the overhead of the SwitchConnection method.

Regards

1 Like

Awesome! I tested this method, and it works perfectly. It is quite a nice approach to solving this requirement. Just FYI, I had to change the client in the sample, to change the service’s name in the RemoteService, not in the data adapter, but other than that, it solves the problem superbly.

Looking forward to the improvements coming in v10.

Thanks again!

Arturo.