How to pass parameter to WCF data service and switch connection string on it?

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"]; // set connectionString based on culture return new Complaint_Entities(connectionString); } 

Thanks L

+4
source share
5 answers

Finally found how to do it, here's how.

The client must send a parameter before executing the request, which it can make by adding it to the request header as follows:

  var client = new QueryServiceReference.Complaint_Entities(new Uri("http://localhost:60642/QueryService.svc")); client.SendingRequest += (o, eventArgs) => eventArgs.RequestHeaders.Add("Culture", "nl-BE"); var result = client.Categories.ToList(); 

In the data service, you can set the connection string that will be used depending on this parameter by overriding the CreateDataSource method:

 public class QueryService : DataService<Complaint_Entities> { protected override Complaint_Entities CreateDataSource() { var culture = HttpContext.Current.Request.Headers["Culture"]; string connectionStringName = string.Format("name=Complaint_Entities_{0}", culture); return new Complaint_Entities(connectionStringName); } } 
+3
source

Yes, it is quite possible. You can add a name to each connection string, and then select that connection string based on the passed variable.

// In your web.config

 <connectionStrings> <add name="OracleDefault" connectionString="DATA SOURCE=G14DEV;PASSWORD=Password;USER ID=UserId; Provider=OraOLEDB.Oracle;" providerName="Oracle.DataAccess.Client"/> <add name="ApplicationDefault" connectionString="DATA SOURCE=G14DEV;PASSWORD=Password;USER ID=UserId; Provider=OraOLEDB.Oracle;" providerName="Oracle.DataAccess.Client"/> <connectionStrings> 

// In your code

 public void Connect(string user) { switch(user) { case "Default": { m_connectionString = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ConnectionString; break; } case "Oracle": { m_connectionString = ConfigurationManager.ConnectionStrings["OracleDefault"].ConnectionString; break; } } //now connect to the database } 
+1
source

You can, if you are talking about a normal data service, where you create your own connection. In the web.config file, keep the server name as a token, for example% DBSERVER%, and replace it with the name ipaddress or server based on the request.

0
source

WCF does nothing if the developer does not write code in it. So you are in charge of everything in WCF .

To do this, simply check the variable in the url:

 string countryCode = Request["country-code"]; // countryCode may be something like 'US' 

Now you can load the connection string from the web.config file based on the countryCode value and set the connection string of your data access level accordingly.

0
source

This is pretty simple, all you have to do is create the right data context depending on the parameter value:

 switch ( parameterValue ) { case "US" : datacontext = new ....( "US connectionstring" ); case "EN" : datacontext = new ....( "BE connectionstring" ); } 

However, there is a disclaimer and authentication .

You need to develop an authentication mechanism for your requests so that US users cannot process requests to the BE database and vice versa.

0
source

Source: https://habr.com/ru/post/1385523/


All Articles