使用实体框架C#限制与数据库的连接(Limit Connections to Database with Entity Framework C#)
我有一个Azure数据库,限制为60个并发连接。 我的问题是我有几个爬虫填充这个数据库,供我们的网站使用Entity Framework使用。
多个爬虫不需要很长时间就可以耗尽所有连接并排除错误。 我已尝试在连接字符串中设置最大池大小值,但这似乎不会对实际数据库连接强制实施任何限制。
我可以将
DbContext
包装成单例,但是我会将整个爬虫限制为单个连接。有没有其他方法可以实现这一目标?
I have an Azure database that is limited to 60 concurrent connections. My issue is that I have several crawlers that populate this database to be consumed by our website using Entity Framework.
It does not take long for multiple crawlers to exhaust all of the connections and error out. I have tried setting the max pool size value in the connection string, but this does not seem to enforce any restrictions on the actual database connections.
I could wrap the
DbContext
in a singleton, but then I would be limiting the entire crawler to a single connection.Are there any other ways to achieve this?
原文:https://stackoverflow.com/questions/38745658
最满意答案
安全警告
ServiceControl没有内置的安全层,因此如果您将API URL暴露给Internet,那么任何可以连接到端口33333的人都可以访问ServiceControl中存储的所有消息。这就是默认情况下它仅限于localhost的原因。
我不能强调它不应该在生产系统上完成
对于Azure,更安全的方法是使用点到站点VPN连接之类的东西。 (请参阅: https : //msdn.microsoft.com/en-us/library/azure/jj156206.aspx )但这可能需要进行一些重新配置。
如果您仍然希望以不安全的方式公开URL,那么您将如何处理它:
1.将App.config中的主机名设置为通配符:
<add key="ServiceControl/HostName" value="*" />
2.更新URLACL以响应通配符。
您可以通过在cmd提示符下发出以下命令来查看URLACL设置:
netsh http show urlacl
如果您有端口http:// localhost:33333 / api /或http://serviceins.cloudapp.net:33333/api/的现有设置,请使用以下命令将其删除:
netsh http delete urlacl URL=http://localhost:33333/api/ netsh http delete urlacl URL=http://serviceins.cloudapp.net:33333/api/
添加通配符URLACL
netsh http add urlacl URL=http://*:33333/api/ User=Users
通过show命令检查它,它应该有这样的条目
Reserved URL : http://*:33333/api/ User: BUILTIN\Users Listen: Yes Delegate: No SDDL: D:(A;;GX;;;BU)
3. Windows防火墙
将入站规则添加到Windows防火墙。 默认情况下,端口33333将被阻止传入连接。 您可以使用以下命令通过Admin Powershell执行此操作(我假设您的VM是Win2012)
New-NetFirewallRule -Name ServiceControl -Direction Inbound -Protocol TCP -LocalPort 33333 -Action Allow -Enabled True
4.添加Azure端点
您还需要打开Azure端点连接以允许连接到端口33333.这实际上是另一个防火墙。 而不是记录下来,我会在这里向您推荐微软自己的doco: http : //azure.microsoft.com/en-us/documentation/articles/virtual-machines-set-up-endpoints/
作为端点配置的一部分,您可以通过限制允许连接到端口的IP范围来添加一些安全性。 这只有在你拥有静态IP时才有用。
Security Warning
ServiceControl has no built in security layer so if you exposing the API URL to the Internet then all of the messages stored in ServiceControl will be accessible by anyone who can connect to port 33333. This is why it's restricted to localhost by default.
I can't stress enough that it should not be done on a production system
For Azure a more secure method would be to use something like a point to site VPN connection. (See: https://msdn.microsoft.com/en-us/library/azure/jj156206.aspx) but this may require a bit of reconfiguration.
If you are still keen to expose the URL in an insecure way here is how you would go about it:
1. Set the hostname in the App.config to a wildcard:
<add key="ServiceControl/HostName" value="*" />
2. Update the URLACL to respond to the wildcard.
You can view the URLACL settings by issuing this command at cmd prompt:
netsh http show urlacl
If you have an existing setting for port http://localhost:33333/api/ or http://serviceins.cloudapp.net:33333/api/ remove them using:
netsh http delete urlacl URL=http://localhost:33333/api/ netsh http delete urlacl URL=http://serviceins.cloudapp.net:33333/api/
Add the wildcard URLACL
netsh http add urlacl URL=http://*:33333/api/ User=Users
Check it via the show command and it should have an entry like this
Reserved URL : http://*:33333/api/ User: BUILTIN\Users Listen: Yes Delegate: No SDDL: D:(A;;GX;;;BU)
3. Windows Firewall
Add an inbound rule to the Windows Firewall. By default the port 33333 will be blocked for incoming connections. You can do this via an Admin Powershell using the following command (I'm assuming you're VM is Win2012)
New-NetFirewallRule -Name ServiceControl -Direction Inbound -Protocol TCP -LocalPort 33333 -Action Allow -Enabled True
4. Add an Azure Endpoint
You'll also need to open up an Azure Endpoint connection to allow connection to port 33333. This is essentially another firewall. Rather than document this I'll refer you to Microsoft's own doco here: http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-set-up-endpoints/
As part of the endpoint configuration you can add some security by limiting the IP range that is allowed to connect to the port. This is really only useful if you've got a static IP.
相关问答
更多-
正确识别设置上下文路径导致问题。 这结果是一个错误,现在已经修复。 Nexus 3(Milestone 6)的下一个版本将包含该修复,并允许您使用上下文路径。 但请记住,上下文路径不会成为Docker URL的一部分,因为docker注册表格式以及docker客户端不支持上下文路径。 因此,对于Docker特定用途,您将继续使用已记录的端口。 更新:里程碑6版本已发布并包含相关修复。 As you correctly identified setting the context-path caused th ...
-
您可能没有在SQL VM的受信任的根存储中安装CA签名的证书。 如果连接字符串中的Encrypt=True ,则将其设置为关闭(不推荐),或在连接字符串中添加以下内容: TrustServerCertificate=True 如果您没有为其使用安装一个,则SQL Server将创建一个自签名证书,但不会被调用者信任,因为它不是CA签名的,除非您通过连接字符串信任任何服务器证书默认。 长期来看,我建议从已知的可信CA(相当便宜)购买CA签名的证书,并将其安装在VM上。 您可以在“加密层次结构”主题和“使用无 ...
-
RDP(远程桌面协议)是用于Windows的专有协议。 我们如何使用RDP远程连接到Linux VM(虚拟机)? 因此您无法远程连接Linux VM 您需要在要用于登录虚拟机的计算机上安装SSH客户端。 您可以选择许多SSH客户端程序。 请点击以下链接: - https://azure.microsoft.com/en-in/documentation/articles/virtual-machines-linux-classic-log-on/ https://azure.microsoft.com/en ...
-
你看过这篇文章吗? 可能需要以不同方式配置连接: 这篇文章是: https : //azure.microsoft.com/en-us/documentation/articles/expressroute-coexist/ Have you seen this article? The connection may need to be configured differently: Here is the article: https://azure.microsoft.com/en-us/docume ...
-
我试图连接到amam10x64.westeurope.cloudapp.azure.com但没有成功。 如果要将此VM与DNS连接,我们应该为此VM设置FQDN,请参阅此链接 。 将文件复制到VM或从VM复制文件。 我的希望是我可以使用以下代码段: 也许我们可以使用winrm来做到这一点。 关于如何使用winrm连接Azure VM,请参阅此答案 。 在VM上运行命令。 希望我能从Pt使用相同的命令。 2。 我们可以使用此脚本通过Winrm连接Azure VM: $username = 'jason' $p ...
-
Microsoft在8/24上对标准映像进行了更改,导致WinRM SSL与自签名证书的连接中断。 如果您使用Server 2012 R2的7月26日映像,它将起作用。 图像名称是: a699494373c04fc0bc8f2bb1389d6106__Windows-服务器-2012-R2-20150726-en.us-127GB.vhd Microsoft made changes to the standard image on 8/24 which caused the WinRM SSL conne ...
-
我将在app1属性中配置app2 Private IP 如果要在app1属性中配置app2私有IP,我们应该在不同区域之间部署VPN。 I will configure app2 Private IP in app1 properties If you want to configure app2 private IP in app1 properties, we should deploy a VPN between different regions.
-
这是一个非常有问题的问题,但我认为有一个更好的步骤让你看看: 在Azure Classic门户中配置VNet到VNet连接 要么 使用Azure Resource Manager和PowerShell为同一订阅中的虚拟网络配置VNet到VNet连接 选择你的毒药...如果你仔细按照步骤,我已经按照预期验证了这两项工作。 That's a pretty loaded question, but I think there is a better walk through for you to have a l ...
-
安全警告 ServiceControl没有内置的安全层,因此如果您将API URL暴露给Internet,那么任何可以连接到端口33333的人都可以访问ServiceControl中存储的所有消息。这就是默认情况下它仅限于localhost的原因。 我不能强调它不应该在生产系统上完成 对于Azure,更安全的方法是使用点到站点VPN连接之类的东西。 (请参阅: https : //msdn.microsoft.com/en-us/library/azure/jj156206.aspx )但这可能需要进行一些 ...
-
是的,这看起来像一个缺失的端点(如果您想要启用防火墙,则为+入站规则)。 在活动模式下,服务器连接回特定端口上的客户端(由客户端传递,PORT命令) - 更长的解释 要在azure上使用活动模式,您必须: 限制你的FTP客户端使用特定的端口(或范围)和公共IP(它在你的VM的壁纸上),例如FileZilla: 每个客户都应该选择这个。 除非你运气不好并使用.NET Framework FTPWebRequest 。 在Azure VM的端点设置中添加具有相同端口(或范围)的端点 : 只需一次点击就无法添加端 ...