Windows CE 5.0 HTTPD <-> .NET应用程序
我想知道将 Windows CE 5.0 设备的 HTTPD Web 服务器耦合到同一 Windows CE 设备上运行的 .NET 应用程序的最实用方法是什么?
我的第一个想法是构建一个 ISAPI 扩展,将传入的 http 请求转发到 .NET 应用程序。不知道该怎么做!可能是共享内存、COM、TCP/IP 套接字?
另一种方法是在 .NET 应用程序本身内实现独立的 HTTP 服务器并避免使用 HTTPD。
有什么经验或想法吗?
谢谢 克里斯
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 CE Web 服务器中运行 .NET 代码的关键是将服务器 dll 加载到 .NET 进程中。几年前我做了一个概念验证来证明这一点。
该设计乍一看可能有点复杂,但有几个优点:
首先,我们需要阻止 Windows CE 自动启动 Web 服务器。将其添加到注册表中:
当我们这样做时,添加另一个键以将“/dotnet”映射到我们的自定义 ISAPI 处理程序:
从以下源代码创建一个名为 HttpdHostProc.exe 的 .NET exe:
一些注释:
如果您曾经进行过任何 ISAPI 编程,那么下面的三个函数 - GetExtensionVersion、TerminateExtension、HttpExtensionProc - 应该看起来很熟悉。如果不是,您真正需要知道的是传入请求是由 HttpExtensionProc 处理的。
继续讨论非托管 dll,HttpdHostUnmanagement.dll:
其中有许多不太有趣的函数,用于将调用转发到 .NET 或从 .NET 转发调用。
如上所述,RunHttpd 函数只是接受传入连接,并通过另一个 ioctl 将它们传递到 Web 服务器进行处理。
要进行全部测试,请在设备上启动 HttpdHostProc.exe,然后在浏览器中打开
http:///dotnet
。 CE 设备应使用一段包含消息“Hello .NET!”的 HTML 进行响应。此代码在带有 .NET Compact Framework 3.5 的 Windows Embedded Compact 7.0 上运行,但也可能适用于其他版本。
我针对 Pocket PC 2003 SDK 构建了非托管 dll,因为这就是我碰巧安装的。也许任何 Windows CE SDK 都可以,但您可能必须调整编译器设置,例如我必须使用 /GS-(禁用缓冲区安全检查)来构建 PPC2003。
在 .NET 中实现 RunHttpd 函数也是很诱人的,但请注意,这存在一些潜在的问题:
如果您不介意使用 /unsafe 进行编译,则性能可能会得到提高通过传递固定缓冲区稍微到 WriteClient,而不是将所有响应数据复制到 HttpExtensionProc 中的非托管缓冲区。
EXTENSION_CONTROL_BLOCK 结构包含许多有用的字段和函数,显然需要将它们包含在完整的实现中。
编辑
只是为了澄清如何处理请求:
The key to running .NET code in the CE web server is to load the server dll into a .NET process. I did a proof of concept a few years back to demonstrate this.
The design may look a bit convoluted at first sight, but has several advantages:
First off, we'll need to prevent Windows CE from starting the web server automatically. Add this to the registry:
While we're at it, add another key to map '/dotnet' to our custom ISAPI handler:
Now create a .NET exe called HttpdHostProc.exe from the following source code:
A few comments:
The three functions further down - GetExtensionVersion, TerminateExtension, HttpExtensionProc - should look familiar if you've ever done any ISAPI programming. If not, all you really need to know is that incoming requests are handled by HttpExtensionProc.
Moving on to the unmanaged dll, HttpdHostUnmanaged.dll:
There are a number of not terribly interesting functions in there that forward calls to and from .NET.
As mentioned above, the RunHttpd function just accepts incoming connections and passes them to the web server for processing by means of another ioctl.
To test it all, launch HttpdHostProc.exe on the device, then open
http://<ipaddr>/dotnet
in a browser. The CE device should respond with a bit of HTML containing the message "Hello .NET!".This code runs on Windows Embedded Compact 7.0 with .NET Compact Framework 3.5, but would probably work on other versions as well.
I built the unmanaged dll against the Pocket PC 2003 SDK, since that's what I happened to have installed. Probably any Windows CE SDK would do, but you might have to adjust compiler settings, for instance I had to build with /GS- (buffer security checks disabled) for PPC2003.
It is tempting to implement the RunHttpd function in .NET as well, but be warned there are a couple of potential issues with that:
If you don't mind compiling with /unsafe, performance could probably be improved slightly by passing fixed buffers to WriteClient, rather than copying all the response data to an unmanaged buffer in HttpExtensionProc.
The EXTENSION_CONTROL_BLOCK structure contains a number of useful fields and functions that obviously would need to be included in a full implementation.
EDIT
Just to clarify how requests are handled:
在我看来,基于过去尝试使用内置 HTTPD 服务器的经验,内置服务器绝对无法尝试做任何有用的事情。调试任何东西都是一件令人头疼的事情,并且与任何设备硬件/系统的互操作都是痛苦的。
由于 CF 中没有 EE 托管支持,因此 Web 服务器无法加载托管程序集(来自 ISAPI 或其他任何内容)。这意味着您的托管代码必须位于单独的进程中,并且要进行通信,您必须使用 IPC - 类似于 P2PMessageQueue,MemoryMappedFile 、套接字等。
编写自己的 HTTPD 服务器也是一种选择,但这不是一项简单的任务。听起来很简单,但一旦深入其中,就会涉及到很多内容。我知道这是因为我们几年前在一个项目上做出了这个决定,并且我们最终创建的服务器支持 ASP.NET 的一个子集,并且我们实际上将其变成了 商业产品因为a)它真的很有用,b)因为它需要大量的工作来实际编写。然而,它确实提供了托管托管代码并能够在 Studio 中调试而不是像托管开发人员所期望的 printf 那样的好处。
In my opinion, based on trying to use the built-in HTTPD server in the past, is that the built-in server absolutely sucks for trying to do anything useful. It's a major headache to debug anything and interop with any device hardware/system is painful.
Since there is no EE Hosting support in the CF, the web server cannot load managed assemblies (from ISAPI or anything else). That means your managed code has to be in a separate process and to communicate you have to use IPC - something like a P2PMessageQueue, MemoryMappedFile, socket, etc.
Writing your own HTTPD server is also an option, but it's not a trivial task. It sounds simple, but there's a lot involved once you dive into it. I know that because we made that decision several years ago on a project, and the server we ended up creating supported a subset of ASP.NET and we actually turned it into a commercial product because a) it was really useful and b) becasue it took a lot of work to actually write. It does, however, give that benefit of hosting managed code and being able to debug in Studio instead of printf as a managed developer expects.