I have a WCF data service that works fine. But now I want it to be able to use another database based on the parameter that is sent by the consumer of my data service.
Case study: A US client that uses a data service passes "USA" as a parameter, so the data service uses a US database. Another client from Belgium who uses the data service passes βBEβ as a parameter, so the data service uses the BE database. Of course, all databases have the same schema.
My client is currently using a data service, for example:
var qclient = new QueryServiceReference.Complaint_Entities(new Uri("http://localhost:60642/QueryService.svc"));
But how can I add a parameter to this? And how can I check this parameter in the data service?
So my question is: how do I pass a parameter to the WCF data service?
EDIT: I found a way, but I don't know if this is the best for this. Before executing the request, I add a parameter to the request header:
var qclient = new QueryServiceReference.Complaint_Entities(new Uri("http://localhost:60642/QueryService.svc")); qclient.SendingRequest += new EventHandler<System.Data.Services.Client.SendingRequestEventArgs>(qclient_SendingRequest); var cat = qclient.Categories.ToList(); static void qclient_SendingRequest(object sender, System.Data.Services.Client.SendingRequestEventArgs e) { e.RequestHeaders.Add("Culture", "nl-BE"); }
In the data service, I can use it to set the connection string:
protected override Complaint_Entities CreateDataSource() { var culture = HttpContext.Current.Request.Headers["Culture"];
Thanks L
source share