首页 \ 问答 \ 使用实体框架C#限制与数据库的连接(Limit Connections to Database with Entity Framework C#)

使用实体框架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
更新时间:2023-08-08 22:08

最满意答案

安全警告

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的端点设置中添加具有相同端口(或范围)的端点 : 只需一次点击就无法添加端 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)