更改数据源 WCF 数据服务

发布于 2024-08-26 09:41:55 字数 79 浏览 10 评论 0原文

有人知道如何动态更改 DataServices 的当前数据源吗?

示例 我想咨询服务并传递一个参数说要建立另一个连接,是否可能?

Anybody know how to change the current data source of a DataServices on the fly?

Example I want to consult the service and pass a parameter saying to pick up another connection, Is Possible?

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

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

发布评论

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

评论(3

橘寄 2024-09-02 09:41:55

它应该非常简单,您可以使用一个覆盖方法来创建数据源。

//
// Summary:
//     Creates a data source of the template class that will be used by the data
//     service.
//
// Returns:
//     An instance of the data source.
protected virtual T CreateDataSource();

当被覆盖时,它应该看起来像这样。

protected override MyModel CreateDataSource()
{
    MyModel modal = new MyModal();

    return modal;
}

这将能够创建具有不同连接字符串的数据源。

下面我添加了一系列操作,您可以将此信息传递给 DataService,以便您可以动态更改数据源的连接。

1.请求标头

如果您在 DataServiceContext 上订阅 SendingRequest,则可以在客户端将额外的信息传递给数据服务,这允许您向 Web 请求添加额外的请求标头。例如,完成此操作后,您可以通过访问 CreateDataSource 方法上的 System.Web.HttpContext.Current.Request.Headers 来读取它们。

如果能够访问内部 IDataService 接口就好了,该接口公开了执行此类操作所需的所有信息。

2.查询字符串

不执行 SendingRequest 和标头的另一种方法是添加一个 QueryString 参数,您可以在特定值中检查该参数并相应地初始化数据源。

3.身份

您可以使用用户凭据来选择要使用的数据源。

同样,这些只是关于如何实现数据源选择的想法。

您可以使用上述策略做很多事情,我希望这能回答您的问题,并帮助您解决问题

PS:只要您所说的“更改数据源”的意思是将模型的连接字符串更改为另一个数据库以上将起作用。

It should be really simple there is a override method you can use that creates a data source.

//
// Summary:
//     Creates a data source of the template class that will be used by the data
//     service.
//
// Returns:
//     An instance of the data source.
protected virtual T CreateDataSource();

When overridden it should look like this.

protected override MyModel CreateDataSource()
{
    MyModel modal = new MyModal();

    return modal;
}

That would give the ability to create a data source with a different connection string.

Below I have added a list of things you can do to pass this information to DataService so you can change the connection of your data source on the fly.

1. Request Headers

You can on the client side pass extra information to data services if you subscribe the SendingRequest on the DataServiceContext, which allows you to add extra request headers to your web requests. Once you do that for example you can read them by accessing the System.Web.HttpContext.Current.Request.Headers on the CreateDataSource method.

It would have been nice to be able to get access to the internal IDataService interface which exposes all the information you need to do this sort of thing.

2. Query String

Another way with out doing the SendingRequest and the headers is to add a QueryString parameter that you can check for in the specific value and initialize the data source accordingly.

3. Identity

You can use the users credentials to chose what data source you want to use.

Again these are just ideas about how to achieve this selection of data sources.

You can do a number of things with the strategies above I Hope this answer your question and it helps you with your problem

PS.: as long as what you mean by "change the data source" is change the connection string of your model to another database the above will work.

别靠近我心 2024-09-02 09:41:55

如果您想在 URL 中传递所需的连接,我认为您唯一的选择是创建两个 DataServiceHost。

http://server/datasource1/service.svc

http://server/datasource2/service.svc

If you want to pass the desired connection in the URL I would think your only option is to create two DataServiceHosts.

http://server/datasource1/service.svc

and

http://server/datasource2/service.svc
〗斷ホ乔殘χμё〖 2024-09-02 09:41:55

尝试重写 DataService 类中的 CreateDataSource 函数。

这将允许您使用任何连接字符串创建自己的数据源。

Try overriding the CreateDataSource function in the DataService class.

This will allow you to create your own DataSource with any connection string.

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