连接字符串的正确位置
使用与 SQL Azure 数据库通信的 Azure Web 角色。目前,当我为 SQL Azure 数据库生成 edmx 文件时,连接字符串 + 用户名密码将添加到 web.config 文件中。我进行了搜索,有几个关于如何加密 web.config/如何使用它在开发和产品之间切换的条目,但我正在考虑将 conn 字符串移出 web.config。
有没有办法可以将连接字符串移动到服务定义文件?这是推荐的方法吗?如果我将连接字符串移到其他地方,我仍然可以使用 edmx 和生成的 objectcontext 类(因为我现有的代码使用自动生成的实体类)。
Working with Azure web role that communicates to a SQL Azure database. Currently when I generate an edmx file for the SQL Azure database the connection strings + the username password are added to web.config file. I did a search and there were several entries on how to encrypt web.config/how to use that to switch between dev and prod but I am thinking of moving conn string out of web.config.
Is there a way by which I can move the connection string to the service definition file? Is that a recommended approach? If I move the connection string elsewhere can I still use the edmx and generated objectcontext classes (cause my existing code uses the automatically generated entity class).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好将连接字符串移至服务配置文件中。这允许您切换到不同的 SQL Azure 数据库而无需重新部署。当数据库崩溃或超时并且您在另一台服务器上准备好备份以供切换时,切换到不同的 SQL Azure 数据库而不重新部署会很有用。
但是,您需要通过单独提供连接字符串来初始化对象上下文。
使用 RoleEnvironment.IsAvailable 查明您是否在 Azure 下运行,并使用以下代码读取 .cscfg 中的设置:
var connectionString = RoleEnvironment.GetConfigurationSettingValue("ConnectionString");
It is best to move your connection strings into service config file. This allows you to switch over to a different SQL Azure database w/o redeployment. Switching to a different SQL Azure database w/o redeployment is useful when one has crashed or is timing out and you have a backup ready on a different server to switch over to.
You will need to initialize your object contexts by providing the connection string separately however.
Use the RoleEnvironment.IsAvailable to find out if you're running under Azure and the following code to read the setting in .cscfg:
var connectionString = RoleEnvironment.GetConfigurationSettingValue("ConnectionString");
我建议在服务配置文件 (.cscfg) 和 Web.config 这两个位置都包含连接字符串。我还建议从一开始就让您的 Web 角色能够在 Azure 环境之外运行。从长远来看,它将影响您的生产力。特别是在日常开发中,您会进行一些小的更改,并且需要在本地运行项目进行验证。目前,在 IIS、IIS Express 或 Cassini(Asp.net env 的代号)中本地运行服务比在本地 azure 模拟器(devFabric)中运行项目要快。
关于您关于存储用户名和密码的第二个问题。这完全取决于您正在寻找的安全级别。 .cscfg 中存储的信息通过 https 传输,并在 Azure 云中受到保护,就像保护应用程序一样。话虽这么说,我会将 TEST 帐户凭据存储在项目中进行测试,并且仅在部署到公共/生产服务时将 Production 存储帐户凭据放入 .cscfg 中。
I recommend having the connection string in both places, Service Configuration file (.cscfg) and Web.config. Where I also recommend from the beginning to have your web role able to run outside an Azure environment. It will impact your productivity in the long run. Especially with daily development where you do small changes and need to run the project locally to verify. Running your service locally in IIS, IIS express or Cassini (the codename for the Asp.net env) is currently faster than running your project in the local azure emulator (the devFabric).
Regarding your second question about storing the username and password. It all depends on the level of security that you're looking for. The information stored inside your .cscfg are transmitted over https and secured in the Azure cloud the same way your application is secured. That being said, I would store the TEST account credentials in the project for testing and would only put the PRODUCTION storage account credentials in the .cscfg at deployment time to the public/production service.