我可以运行ASPX并在不发出HTTP请求的情况下grep结果吗?

我怎样才能在没有URL和没有HTTP的情况下对一个简单的ASP.NET文件进行函数调用,并捕获它生成的字节流?

更多背景资料,

我需要一种模板可以在里面放一点逻辑,来渲染一些像文本文件一样的INI。 我放弃了从Java移植的库,并提出了使用ASP.NET作为模板引擎的解决方案。 (我不是用它来构建一个网站,甚至不是HTML。)

我编写了一个ASP.NET页面(没有WebForm,没有MVC),它接受XML POST,并根据一组简单但不太简单的规则生成一个长文本文件。

我从DB对象生成XML,提交到ASP页面,grep结果,它工作得很好。 但是,问题是我们想要用作WCF使用的库。 因此,我没有使用相对路径,我必须在配置中的某处存储ASP的URL,这是我不想要的。

它将托管在IIS服务器上,但不会从任何前端ASP调用(至少不是直接),并且永远不会从最终用户调用。

PS。 我原本是在为C#寻找一个简单的模板引擎,但它们太旧了,不再需要维护,文档很差,缺少集成的编辑器/调试器,太简单了,而且它们可能会说不同的语言。

PPS。 我也考虑过T4,但它在VS 2008中没有编辑器和调试器。

如果为ASPNET运行时构建主机,则可以在没有IIS的情况下运行ASPX页面而不使用HTTP消息。

例:

public class MyAspNetHost : System.MarshalByRefObject { public void ProcessRequest(string page) { var request = new System.Web.Hosting.SimpleWorkerRequest (page, // the page being requested null, // query - none in this case System.Console.Out // output - any TextWriter will do ); // this will emit the page output to Console.Out System.Web.HttpRuntime.ProcessRequest(request); } public AppDomain GetAppDomain() { return System.Threading.Thread.GetDomain(); } } public class Example { public void Run(IEnumerable pages) { // ASPNET looks for assemblies - including the assembbly // that contains any custom ASPNET host - in the bin\ // subdirectory of the physical directory that backs the // ASPNET Host. Because we are going to use the current // working directory as the physical backing directory for // the ASPNET host, we need to ensure there's a bin // subdirectory present. bool cleanBin = false; if (!Directory.Exists("bin")) { cleanBin = true; Directory.CreateDirectory("bin"); } // Now, ensure that the assembly containing the custom host is // present in that bin directory. The assembly containing the // custom host is actually *this* assembly. var a = System.Reflection.Assembly.GetExecutingAssembly(); string destfile= Path.Combine("bin", Path.GetFileName(a.Location)); File.Copy(a.Location, destfile, true); host = (MyAspNetHost) System.Web.Hosting.ApplicationHost.CreateApplicationHost ( typeof(MyAspNetHost), "/foo", // virtual dir - can be anything System.IO.Directory.GetCurrentDirectory() // physical dir ); // process each page foreach (string page in pages) host.ProcessRequest(page); } } 

如果要清理该bin目录,则必须首先卸载AppDomain。 你可以这样做,像这样:

  private ManualResetEvent aspNetHostIsUnloaded; private void HostedDomainHasBeenUnloaded(object source, System.EventArgs e) { // cannot clean bin dir here. The AppDomain is not yet gone. aspNetHostIsUnloaded.Set(); } private Run(IEnumerable pages) { try { ....code from above .... } finally { if (host!= null) { aspNetHostIsUnloaded = new ManualResetEvent(false); host.GetAppDomain().DomainUnload += this.HostedDomainHasBeenUnloaded; AppDomain.Unload(host.GetAppDomain()); // wait for it to unload aspNetHostIsUnloaded.WaitOne(); // optionally remove the bin directory if (cleanBin) { Directory.Delete("bin", true); } aspNetHostIsUnloaded.Close(); } } } 

这对于测试ASPX页面是有意义的。 但对于你的场景,我不太确定这是正确的。 有更多直接的方法来生成文本文件。 但是,它可能适合你。 如果您真的喜欢模板引擎的想法,那么托管ASPNET可能就是您的最佳选择。

在您的情况下,您可能希望修改自定义主机,以便每个页面的输出转到StringWriter而不是Console.Out,然后您可以在该输出上执行Grep(或更可能使用Regex进行搜索)。 您可能还想修改它以接受所有输入数据作为查询字符串。 您需要格式化页面请求才能执行此操作。

编辑 : 关于这种托管ASPNET运行时的技术,在MSDN杂志上有一篇很好的文章 。 从2004年12月起。


EDIT2 :有一种更简单的方法来管理bin目录。 只需创建一个名为bin的符号链接,指向“。”。 然后,您可以在调用AppDomain.Unload()之后删除符号链接,而无需等待。 看起来像这样:

  public void Run(string[] pages) { bool cleanBin = false; MyAspNetHost host = null; try { // This creates a symlink. // ASPNET always looks for a bin\ directory for the privateBinPath of the AppDomain. // This will create the bin dir, pointing to the current dir. if (!Directory.Exists("bin")) { cleanBin = true; CreateSymbolicLink("bin", ".", 1); } host = (MyAspNetHost) System.Web.Hosting.ApplicationHost.CreateApplicationHost ( typeof(MyAspNetHost), "/foo", // virtual dir - can be anything System.IO.Directory.GetCurrentDirectory() // physical dir ); foreach (string page in pages) host.ProcessRequest(page); } finally { // tell the host to unload if (host!= null) { AppDomain.Unload(host.GetAppDomain()); if (cleanBin) { // remove symlink - can do without waiting for AppDomain unload Directory.Delete("bin"); } } } } 

这消除了对ManualResetEvent,复制文件,同步等的需要。它假定自定义ASPNet主机的程序集以及您运行的ASPX页面所需的所有程序集都包含在当前工作目录中。

这听起来像是一个非常类似的问题,它在服务器上生成HTML电子邮件。 这里有一些答案(对于MVC):

ASP.NET MVC:如何使用控制器发送HTML电子邮件?

您可以通过加载和将控件(ASCX)加载到文件来以类似的方式继续非MVC。