【问题标题】:Modify connectionString in WCF Data Services 5.6在 WCF 数据服务 5.6 中修改 connectionString
【发布时间】:2015-01-07 19:28:26
【问题描述】:

我正在使用 Oracle DataAccess(最后一个),里面有很多数据库用户。因此,我想在我的客户端应用程序 (WPF) 中创建 connectionString 并将其传递给服务器。我在我的服务器中执行以下操作:

protected override POSContext CreateDataSource()
{
    HttpRequest req = HttpContext.Current.Request;

    if (req.Headers != null && Array.FindIndex(req.Headers.AllKeys, c=>c.Equals("db", StringComparison.OrdinalIgnoreCase)) > 0)
    {
        string database = req.Headers["db"]; 
        string user = req.Headers["user"];
        string pass = req.Headers["pass"];

        StringBuilder conexion = new StringBuilder();
        conexion.Append("DATA SOURCE=");
        conexion.Append(database);
        conexion.Append(";USER ID=");
        conexion.Append(user);
        conexion.Append(";PASSWORD=");
        conexion.Append(pass);

        if (!string.IsNullOrEmpty(conexion.ToString()))
            return new POSContext(conexion.ToString());
    }

    return null;//new POSContext();
}  

在客户端,我有以下代码:

var context = new POS.DataServices.POSContext(new Uri(Storage.Current.UrlService)); // Something like this http://localhost/POService.svc
context.BuildingRequest += (s, args) => //Or SendingRequest2 produces the same result
{
    args.Headers.Add("db", Storage.Current.Configuraciones.DB);
    args.Headers.Add("user", Storage.Current.Configuraciones.UserName);
    args.Headers.Add("pass", Storage.Current.Configuraciones.Password);
};

MessageBox.Show(proxy.POS_CIUDAD.ToList().FirstOrDefault().CIU_DESC); //Alert the city name  

调试应用程序时出现错误(请求错误)。我的客户端代码尝试先显示消息,然后转到 BuildingRequest。如何在实体调用之前传递 connectionString?

【问题讨论】:

  • 有人吗?用 Oracle 做不到吗?

标签: c# wpf oracle odata wcf-data-services


【解决方案1】:

CreateDataSource 在服务启动时被调用一次,这就是你在那里得到错误的原因。

我认为您可以尝试以下方法:

  1. 在 POSContext 上添加一个连接字符串属性,并给 POSContext 一个无参数构造函数。那就是可以稍后设置连接字符串。也许您需要确保仅在连接字符串准备好时才设置连接。
  2. 重写 OnStartProcessingRequest 方法,并更改 CurrentDataSource 上的连接字符串(POSContext 类)

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多