c# 实现网页(Web Page)和本地程序(Local App)之间的动态调用
场景说明:例如使用网站时,需要通过第三方程序获取本机的串口获取信息,服务端无法直接获取,需要通过本地程序
解决方案:通过本地exe程序通过监听http请求的特定端口,然后实现在本地exe调用第三方程序
第三方程序例如:DLL插件,用于调用电脑本地硬件(打印机、扫描仪、读卡器等)或者通过DLL插件和第三方项目程序进行交互。
Exe可执行程序,主要用于集成第三方程序。
主要实现逻辑(参考:https://github.com/wrxiang/WebRunLocal)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Net.Http; using System.ServiceModel; using System.Web.Http; using System.Web.Http.SelfHost; namespace RestController { class Program { static void Main( string [] args) { var config = new HttpSelfHostConfiguration( "http://localhost:12357" ); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( "DefaultApi" , "api/{controller}/{action}" ); using (HttpSelfHostServer server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine( "Press Enter to quit." ); Console.ReadLine(); } } } public class TestController : ApiController { [HttpPost] public HttpResponseMessage PostMethodFactory() { return new HttpResponseMessage(); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)