- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经为 js 文件实现了一个 HttpHandler。这个HttpHandler的工作就是本地化js文件的内容。因此处理程序选择 js 文件的内容,将它们本地化并写入响应对象。但问题是处理程序在用于长文件时会导致“远程主机关闭连接”。以下是 HttpHandler 的代码
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using System.Xml.Linq;
namespace ProjectLocalization
{
/// <summary>
/// HTTP Handler to handle .js file localized resources by replacing Resources tag
/// </summary>
public class JSFileResourceHandler : IHttpHandler, IRequiresSessionState
{
// Regex pattern to extract the resource place holders.
private const string RESOURCEPATTERN = @"<\x25{1}\x24{1}\s*Resources\s*:\s*(?<declaration>\w+\s*,\s*\w+)\s*%>";
//Regex pattern to extract resource location settings.
private const string SETTINGSPATTERN = @"<resourcesettings>(?>.|\n)+?resourceSettings>";
//Caches the default culture set when the handler got instantiated.
private CultureInfo defaultCulture;
public delegate CultureInfo OnApplyCulture();
public static event OnApplyCulture ApplyCulture;
// Initializes a new instance of the class.
public JSFileResourceHandler()
{
defaultCulture = Thread.CurrentThread.CurrentCulture;
}
// Gets a value indicating whether another request can use
// the True if the instance is reusable; otherwise, false.
public bool IsReusable
{
get { return true; }
}
// Enables processing of HTTP Web requests by a custom HttpHandler
// that implements the interface.
// HttpContext: its object that provides references to the intrinsic server objects (for example, Request,
// Response, Session, and Server) used to service HTTP requests.
public void ProcessRequest(HttpContext context)
{
context.Response.Buffer = false;
// Retrieve culture information from session
if (ApplyCulture != null)
{
//Call ApplyCulture(delegate), is ApplyCutlture Return NULL then default culture will be set.
CultureInfo culture = ApplyCulture() ?? defaultCulture;
if (culture != null)
{
// Set culture to current thread
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture.Name);
Thread.CurrentThread.CurrentUICulture = culture;
}
}
string physicalFilePath = context.Request.PhysicalPath;
string fileContent = string.Empty;
string convertedFile = string.Empty;
// Determine whether file exists
if (File.Exists(physicalFilePath))
{
// Read content from file
using (StreamReader streamReader = File.OpenText(physicalFilePath))
{
fileContent = streamReader.ReadToEnd();
if (!string.IsNullOrEmpty(fileContent))
{
// Load resource location types
//Dictionary<string, System.Type> locationTypes = GetResourceLocationTypes(fileContent);
Boolean IsLocalized = IsLocalizationImplemented(fileContent);
// Find and replace resource place holders
convertedFile = IsLocalized ? ReplaceResourcePlaceholders(fileContent) : fileContent;//, locationTypes);
}
}
}
context.Response.ContentType = "application/javascript";
context.Response.Output.Write(convertedFile);
context.Response.Flush();
}
private static bool IsLocalizationImplemented(string strFileContent)
{
try
{
bool IsLocalize = false;
Match settingsMatch = Regex.Match(strFileContent, SETTINGSPATTERN, RegexOptions.IgnoreCase);
while (settingsMatch.Success)
{
string Value = settingsMatch.Groups[0].Value.Replace("///", String.Empty).Replace("//", String.Empty);
XElement Settings = XElement.Parse(Value);
//Load Value that it is localized or not.
if (Settings.Element("Resource").Attribute("IsLocalize") != null)
{
String strIsLocalized = Settings.Element("Resource").Attribute("IsLocalize").Value;
settingsMatch = settingsMatch.NextMatch();
if (strIsLocalized.ToUpper() == @"TRUE") IsLocalize = true;
}
}
return IsLocalize;
}
catch
{
return false;
}
}
//Replaces the resource placeholders.
//fileContent:Content of the file
//return:File content with localized strings
private static string ReplaceResourcePlaceholders(string fileContent)
{
string outputString = fileContent;
Match resourceMatch = Regex.Match(fileContent, RESOURCEPATTERN);
while (resourceMatch.Success)
{
// Determine whether a valid match was found
if (resourceMatch.Groups["declaration"] != null)
{
// Extract resource arguments -> always two
// arguments expected: 1. resource location name, 2. resource name
string[] arguments =
resourceMatch.Groups["declaration"].Value.Split(',');
if (arguments.Length < 2)
{
throw new ArgumentException("Resource declaration");
}
string resourceLocationName = arguments[0].Trim();
string resourceName = arguments[1].Trim();
// Load resource string
string localizedValue = "";
try
{
localizedValue = Convert.ToString(HttpContext.GetGlobalResourceObject(resourceLocationName, resourceName));
localizedValue = localizedValue.Replace("\\'", "~");
localizedValue = localizedValue.Replace("'", "~");
localizedValue = localizedValue.Replace("~", "\\'");
}
catch
{
localizedValue = "ERROR WHILE LOCALIZATION!!!";
}
// Replace place holder
outputString = outputString.Replace(resourceMatch.Groups[0].Value, localizedValue);
}
// Find next regex match
resourceMatch = resourceMatch.NextMatch();
}
return outputString;
}
}
}
我认为错误是由以下几行引起的
context.Response.ContentType = "application/javascript";
context.Response.Output.Write(convertedFile);
context.Response.Flush();
错误是这样的远程主机关闭了连接。错误代码为 0x80072746。
在 System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean&异步)在 System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal) 在 System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush) () 在 System.Web.HttpWriter.Write(String s) 在 ProjectLocalization.JSFileResourceHandler.ProcessRequest(HttpContext context) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication .ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
任何人都可以提出解决方案......
最佳答案
您是否尝试过取消对 Flush() 的调用?这是唯一对我来说可疑的部分。
关于c# - HttpHandler 导致 "The remote host closed the connection"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2627740/
Windows“主机”文件编码是什么?是UTF-8吗?还是 ASCII + 系统代码页?应如何添加 IDN(带有变音符号等的国际域名)条目以及它们是否可以添加? 最佳答案 它应该是没有 BOM 的 A
我已经在谷歌上搜索了大约 90 分钟,但仍然没有答案。在哪里设置 default_url_options?我已经将其设置为 config.action_mailer.default_url_optio
我正在从它的样式文件中设计一个 Angular 组件,我发现我必须使用 :host ,:host() 或 :host-context selector ,它们各自有什么不同? 最佳答案 :主机 是用于
我正在创建简单的网络应用程序。而且我在服务器配置方面遇到了问题。当我尝试启动服务器时,收到错误消息 The element type "Host"must be terminated by the m
这个有效: 127.0.0.1 www.somesite.com 但不是这个: 127.0.0.1/somepath/www www.mysite.com 我做错了什么?是否可
我必须查看我的前同事留下的一些脚本,我很好奇为什么他在参数中使用 $a = $(Read-Host -Prompt "Write Something")节而不是 $a = (Read-Host -Pr
我在远程连接到 MySQL 服务器时遇到问题。我知道我的用户设置了来自任何主机的“%”通配符,因此我应该有权访问服务器。我可以使用我的凭据本地登录 MySQL 服务器,但不能远程登录。 我在 my.c
我正在尝试使用 angular 2 构建一个双主题应用程序。有问题的组件 css 已经使用 :host/deep/强制对子组件进行样式设置。 但是我正在尝试根据 html 主题切换此样式,并且我正在使
我需要将特定的传出连接(从我系统上的任何 网络客户端)重定向到特定的 IP。是的,可以通过在 /etc/hosts 文件中添加这一行来完成: 123.456.789.012 www.mydomain
尝试在开发中预览我的邮件程序,但出现上述错误。(使用 Michael Hartl 教程)。 我相信它因为我的主机而无法正常工作。但是,使用 cloud 9 我很难找到我的主机。在书中,它被定义为 Cl
nc 当我在虚拟机上执行它时工作正常。 Connection to 10.0.0.10 22 port [tcp/ssh] succeeded! 但是当我在 docker 容器内执行相同的命令时,它会
我正在学习PowerShell,想知道两者之间的区别 Read-Host -Prompt "Text" 和 Read-Host "Text" 是同一件事还是我没有看到的区别? 最佳答案 这是同一件事。
我无法对 Cassandra 节点进行查询。我能够连接到集群并进行连接。然而,在执行查询时,它失败了 Caused by: com.datastax.driver.core.exceptions.No
我有这样的场景,比如我需要运行一个位于 12.34.567 的 API curl 请求,登录后我必须再登录一台主机 98.76.543。 登录到第二个主机后,我必须运行curl -XPOST -H"C
当我尝试 git push -u origin master到我的存储库,我得到 400 Bad Request .我特意用 编辑了一些信息。 最近从 gitlab-ee 中解绑了 nginx,并验
假设我有某个程序的源代码,源代码使用 GNU autotools,为了简单起见,假设该程序绝不是任何类型的开发工具。如果我手边有交叉工具链,我可以使用 --host configure 为平台 foo
在我的 ASP.NET Core 2.0 项目中,我一直在使用 Microsoft.Extensions.Hosting 命名空间中的 IHostingEnvironment 和 IApplicati
我正在使用以 Python3.6 为基础的 docker 镜像。突然间它开始崩溃(启动后立即退出)。所以我冲进了容器,发现它崩溃是因为与容器化 postgres 数据库的连接突然失败。我设法得到的唯一
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我尝试使用 ssh命令连接到另一个远程主机。 ssh -p 21098 -i $OPENSHIFT_DATA_DIR/.ssh/host_key user@domain.com The authe
我是一名优秀的程序员,十分优秀!