我正在尝试将非常大的文件(> 2GB)上传到我的WebAPI应用程序(在.NET 4.5.2上运行,Windows 2012R2).
设置httpRuntime maxRequestLength属性是没有用的,因为它只能处理小于2GB的文件.
我目前正在使用自定义的MultipartFormDataStreamProvider来读取服务器上的整个流,并且我已经使用自定义WebHostBufferPolicySelector关闭了缓冲.
我发现ASP.NET(或WebAPI)使用Hood下的HttpBufferlessInputStream,它有一个名为_disableMaxRequestLength的字段.如果我将此值设置为true(通过反射),我可以流式传输任何大小的文件.
然而,摆弄这些内部的这些显然不是一个好方法.
用于请求的HttpRequest类有一个名为GetBufferlessInputStream的方法,该方法具有允许禁用maxRequestLength的重载.
我的问题是:如何让WebAPI使用此重载而不是标准的重载?
有没有办法替换Default HttpRequest或HttpContext类?或者我真的需要对整个事物使用反射吗?
这是我目前用来禁用maxRequestLength的代码:
private void DisableRequestLengthOnStream(HttpContent parent)
{
var streamContentProperty = parent.GetType().GetProperty("StreamContent", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (streamContentProperty == null) return;
var streamContent = streamContentProperty.GetValue(parent, null);
if (streamContent == null) return;
var contentProperty = typeof(StreamContent).GetField("content", BindingFlags.Instance | BindingFlags.NonPublic);
if (contentProperty == null) return;
var content = contentProperty.GetValue(streamContent);
if (content == null) return;
var requestLengthField = content.GetType().GetField("_disableMaxRequestLength", BindingFlags.Instance | BindingFlags.NonPublic);
if (requestLengthField == null) return;
requestLengthField.SetValue(content, true); …Run Code Online (Sandbox Code Playgroud) 我需要从Azure功能访问证书.
我按照Azure函数中的运行时错误加载证书中列出的步骤进行了操作,但它没有成功.
private static X509Certificate2 GetCertificate(string thumbprint, TraceWriter log)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadOnly);
log.Info("Enumerating certificates");
foreach (var cert in store.Certificates) {
log.Info(cert.Subject);
}
var col = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (col == null || col.Count == 0)
{
return null;
}
return col[0];
}
finally
{
store.Close();
}
Run Code Online (Sandbox Code Playgroud)
}
上传到Azure功能的两个证书和设置WEBSITE_LOAD_CERTIFICATES也被添加,并设置为*或所需证书的thumpbrint,但无济于事.
GetCertificate方法应该打印商店中所有证书的列表,但商店是空的.
关于如何解决这个问题的任何线索?
我正在使用SQL Server LocalDB作为网站.
安装SQL Server LocalDB并在安装过程中创建数据库.通过LocalDBCreateInstance,StartLocalDBInstance和LocalDBShareInstance创建共享实例.
如果实例启动,网站可以成功连接到数据库.
但是,如果实例已停止,则不会自动启动.从控制台应用程序访问数据库(当实例停止时)会导致实例启动.
所以这似乎是某种许可问题.
应用程序池在AppPool标识下运行.但是,将其切换到网络服务或本地系统并不能解决问题.
应用程序池配置为加载其用户配置文件,"setProfileEnvironment"属性也设置为true.(如此处和此处所述,这两个设置都是必需的).
我已使用Sysinternal Process Monitor监视数据库访问,但未能找到任何"拒绝访问"条目.
我也尝试从网站本身调用StartLocalDBInstance(在Global.asax中).调用成功返回(返回值为S_OK),但实例仍然停止.
我有通过 GET-Requests 的 API-Controller 服务文件。我正在使用 PushStreamContentResponse 并且效果很好。
我还可以在响应对象上设置 Content-Length-Header。
现在我也想支持 HEAD-Requests。我试过http://www.strathweb.com/2013/03/adding-http-head-support-to-asp-net-web-api/虽然这可能有效,但我需要一个我不知道的解决方案不需要实际处理请求:检索文件并流式传输它很昂贵,但获取元数据(长度等)实际上是无操作的。
但是,当我尝试设置 Content-Length 标头时,它将被 0 覆盖。
我添加了请求跟踪,我看到我的处理程序返回的消息显示有正确的 URL、Content-Disposition 和 Content-Length。
我还尝试使用自定义 HttpResponse 并实现 TryComputeLength。虽然确实调用了此方法,但结果在管道中的某个点被丢弃。
有什么方法可以使用 Web API 支持此功能吗?