ASP.NET定位:无需重新启动应用程序更改的资源? [英] ASP.NET localization: Changing resources without restarting the application?
问题描述
我有终端用户可以改变本地化资源和变化应在应用程序中显示,而不需要重新启动应用程序的要求。
I have the requirement that the end-user can change localized resources and the changes should be visible in the application without the need to restart the application.
更新,以澄清情况:
我说的是在运行时更改了本地化资源。可以说,我有一个网页的德语翻译错字。然后,一些管理用户应该改变这种错字在运行的可能性。应该没有必要为了重新部署或重新启动,此更改反映在UI。
Update to clarify the scenario:
I am talking about changing the localized resources at runtime. Lets say I have a typo in the german translation of a page. Then some admin-user should have the possibility to change that typo at runtime. There should be no need for a redeployment or restart in order for this change to be reflected in the UI.
我使用ASP.NET MVC3。
I am using ASP.NET MVC3.
我有什么选择呢?
我一直在寻找到编写定制ResourceProvider 的是加载资源从数据库中。
这似乎不是太大的力气,不过到目前为止,我指出了两个缺点:
I have been looking into writing a custom ResourceProvider that loads resources from the database.
This seems not too much effort, however so far I pointed out two drawbacks:
- 这是不是与在MVC3用于方便的验证DataAnnotations工作(DataAnnotations用ErrorMessageResourceType参数,只编译资源工作的工作)
- 我们基本上要提供我们自己的围绕管理资源(如翻译等),模具这是一个遗憾,因为有很多的工具,这与RESX-文件。
什么其他选择?将在运行时部署的RESX-文件的操作是一种选择?
但我怀疑,应用程序将自动为重启,当它检测到这些变化:我怀疑ASP.NET意识到这个RESX,文件已更改,那么它回收应用程序池,并编译动态新RESX-文件。
它是否正确?有没有解决这个办法吗?
What are the other options? Would manipulation of the deployed resx-files at runtime be an option?
But I suspect that the application is automatically "restarted" when it detects those changes: I suspect ASP.NET realizes that the resx-files have changed, it then recycles the application-pool and compiles the new resx-files on the fly.
Is this correct? Is there any way around this?
我还没看着部署之前编译的资源投入到卫星组件。这是即使对于Web应用程序的推荐的方案?
但是,即使编译附属程序集我怀疑,ASP.NET重新启动应用程序,当这些组件在飞行中改变。这是正确的?
I have not yet looked into compiling the resources into satellite assemblies before deployment. Is this even a recommended scenario for web applications?
But even with compiled satellite assemblies I suspect that ASP.NET restarts the application, when those assemblies are changed on the fly. Is this correct?
我有兴趣的原始需求如何得到满足的经验吗?
我有兴趣了解我上面所提到的选项任何意见。
I would be interested in any experience in how the original requirement can be satisfied? And I would be interested in any comments about the options I have mentioned above.
推荐答案
DataAnnotations接受 ErrorMessageResourceType
告诉在哪里访问资源的ValidationAttrributes。你可以通过这个如下:
DataAnnotations accept a ErrorMessageResourceType
which tells the ValidationAttrributes where to access resources. You can pass this as follows:
[Required(
ErrorMessageResourceType = typeof(DynamicResources),
ErrorMessageResourceName = "ResourceKey")]
public string Username { get; set; }
通过创建此参数与每个键静态属性类型,你可以创建一个实现从数据库或其他实现负载的资源。然后,您可以用适合干性动态对象结合这一点,并移动到执行 TryGetMember
。潜在然后用T4模板来生成数据库在编译时静,这个结束了:
By creating a type for this parameter with static properties for each key you can create an implementation that loads resources from a database or other implementation. You could then combine this with a dynamic object for DRY and move the implementation into TryGetMember
. Potentially then use T4 templates to generate the statics from your database at compile time, ending up with this:
public class DynamicResources : DynamicObject
{
// move these into partial and generate using T4
public static string MyResource
{
get { return Singleton.MyResource; }
}
public static string MyOtherResource
{
get { return Singleton.MyOtherResource; }
}
// base implementation to retrieve resources
private static dynamic singleton;
private static dynamic Singleton
{
get { return singleton ?? (singleton = new DynamicResources()); }
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
// some logic here to look up resources
result = GetResourceKeyFromDatabase(binder.Name);
return true;
}
}
当然,这将是完美的,如果资源不是静态属性。
Of course it would be perfect if resources weren't static properties.
这篇关于ASP.NET定位:无需重新启动应用程序更改的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!