如何在不硬编码 URI 的情况下实例化 WCF DataServices 客户端
我对 WCF DataServices (OData) 相当陌生,我需要知道在客户端上实例化实体容器而不对 URI 进行硬编码的最佳方法。看起来 MSDN 上的所有示例都描述了像这样实例化客户端:
Uri uri = new Uri("http://www.someservice.svc");
DataServiceContext svc = new DataServiceContext(uri);
但是,我知道我一定在某个地方遗漏了一些东西,因为像这样硬编码服务地址没有任何意义。一方面,当您从开发转移到测试、QA 到生产时,每个环境可能有不同的 URI,如何动态更改地址?
感谢您对此的任何见解。
I'm fairly new to WCF DataServices (OData) and I need to know the best way to instantiate the entity container on the client without hard-coding the URI. It seems like all of the examples on MSDN describe instantiating the client like this:
Uri uri = new Uri("http://www.someservice.svc");
DataServiceContext svc = new DataServiceContext(uri);
However, I know I must be missing something somewhere, because it doesn't make any sense to hard-code a service address like this. For one thing, how do you dynamically change the address when you move from Development to Test to QA to Production, when each environment is likely to have a different URI?
Thanks for any insights into this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Silverlight 应用程序,您可以使用 application.current.host 访问 xap 的 Uri
然后你可以添加一个相对Uri来获取服务Uri:
Uri base = application.current.host;
Uri relService = new Uri("..\someservice.svc", System.UriKind.Relative);
Uri 服务 = new Uri(base, relService);
DataServiceContext svc = new DataServiceContext(service);
If you are working with a Silverlight app you can access the Uri of the xap with application.current.host
Then you can add a relative Uri to get the service Uri:
Uri base = application.current.host;
Uri relService = new Uri("..\someservice.svc", System.UriKind.Relative);
Uri service = new Uri(base, relService);
DataServiceContext svc = new DataServiceContext(service);
将您的 DataService URL 放入您的
Settings
文件或只是简单的app.config
中:并在您的
app.config
(或web.config)中: config
对于网络应用程序):或者从数据库配置表中获取它......或或或或或......有很多选择!
URI 只是一个字符串 - 您可以从您可能拥有的任何配置源中获取它。
Put your DataService URL into e.g. your
Settings
file or just plainapp.config
:And in your
app.config
(orweb.config
for a web app):Or grab it from a database config table..... or or or or or..... plenty of choices!
The URI is just a string - you can grab that from just about any configuration source you might have.