如何将参数传递给 WCF 数据服务并根据它切换连接字符串?

发布于 2024-12-20 07:22:13 字数 1243 浏览 1 评论 0原文

我有一个 WCF 数据服务,运行良好。但现在我希望它能够根据数据服务的使用者发送的参数使用不同的数据库。

具体示例:来自美国的使用数据服务的客户端将“US”作为参数传递,因此数据服务使用US数据库。另一个使用数据服务的比利时客户端将“BE”作为参数传递,因此数据服务使用BE 数据库。当然,所有数据库都有相同的模式。

目前我的客户使用的数据服务如下:

var qclient = new QueryServiceReference.Complaint_Entities(new Uri("http://localhost:60642/QueryService.svc"));

但是如何向其添加参数呢?那么如何在数据服务中查看这个参数呢?

所以我的问题是:如何将参数传递给 WCF 数据服务?

编辑:我找到了一种方法,但我不知道这是否是最好的方法。在执行请求之前,我将参数添加到请求标头:

 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");            
}

在数据服务中,我可以使用它来设置连接字符串:

protected override Complaint_Entities CreateDataSource()
{
    var culture = HttpContext.Current.Request.Headers["Culture"];   
    // set connectionString based on culture
    return new Complaint_Entities(connectionString);    
}

谢谢, L

I have a WCF data service, which works fine. But now I want it to be able to use a different database based on a parameter that is sent by the consumer of my data service.

Concrete example: a client from US that uses the data service passes 'US' as a parameter, and so the data service uses the US database. Another client from Belgium that uses the data service passes 'BE' as a parameter, so the data service uses the BE database. Of course all databases have same schemas.

Currently my client uses the data service like:

var qclient = new QueryServiceReference.Complaint_Entities(new Uri("http://localhost:60642/QueryService.svc"));

But how would it be possible to add a parameter to this? And how can I check this parameter in the data service?

So my question is: how to pass a parameter to a WCF data service?

EDIT: I found a way, but I don't know if it's the best method to do it. Before doing the request, I add the 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 then 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇纵 2024-12-27 07:22:13

终于找到了如何做,这就是方法。

客户端必须在执行查询之前发送参数,可以通过将其添加到请求标头来实现,如下所示:

  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();

在数据服务中,您可以通过重写 CreateDataSource 方法根据此参数设置要使用的连接字符串:

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);
    }
}

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

The client has to send the parameter before doing the query, which he can do 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 then can set the connection string to 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);
    }
}
像你 2024-12-27 07:22:13

是的,这是完全可能的。您可以为每个连接字符串添加一个名称,然后根据传递的变量选择该连接字符串。

//在你的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>

//在你的代码中

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
}

Yes this is entirely possible. You can add a name to each connection string you have and then select that connectionstring based on a 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
}
旧时模样 2024-12-27 07:22:13

如果您谈论的是创建自己的连接的普通数据服务,则可以。在 web.config 中,将服务器名称保留为令牌(如 %DBSERVER%),并根据请求将其替换为 IP 地址或服务器名称。

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

趴在窗边数星星i 2024-12-27 07:22:13

如果您作为开发人员不在其中编写代码,那么 WCF 将不会执行任何操作。因此,您负责 WCF 中的一切

为此,只需检查 URL 中的变量:

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

现在,您可以根据 countryCode 的值从 web.config 文件加载连接字符串,并相应地设置数据访问层的连接字符串。

WCF does nothing if you as the developer don't write code in it. Thus, you are in charge of everything in WCF.

To do this, simply check the variable in URL:

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

Now, you can load a connection string from your web.config file, based on the value of countryCode and set the connection string of your Data Access Layer accordingly.

铁憨憨 2024-12-27 07:22:13

这相当简单,您所要做的就是根据参数值创建适当的数据上下文:

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

但是,有一个警告,那就是身份验证

您必须设计一种机制来验证您的请求,以便美国用户无法向 BE 数据库发出请求,反之亦然。

That is fairly easy, all you have to do is to create a proper data context depending on the parameter value:

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

However, there's a caveat and it's authentication.

You have to design a mechanism to authenticate your requests so that it is impossible for US users to craft requests to BE database and vice versa.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文