积极答复者
多个项目如何共享静态对象?

问题
-
我的解决方案中有如下项目(Project):myView(wpf应用)、myModel(类库)、anotherView(wpf应用)。anotherView在myView之后启动调试。
引用关系如下:myView引用myModel,anotherView也引用myModel
其中myModel中含有静态类myStaticClass,可以通过myView项目初始化。
但是我发现,myStaticClass的状态不能在anotherView中共享(由myView更新的myStaticClass对anotherView不起作用,anotherView的myStaticClass是独立的)
那么,如何让anotherView共享由myView初始化过的myStaticClass呢?这已经跨了应用程序了吧?
答案
-
你好,
>>那么,如何让anotherView共享由myView初始化过的myStaticClass呢?这已经跨了应用程序了吧?
根据你的需求,多个项目共享同一个数据, 我建议你使用服务来做,几个项目调用同一个服务。 比如WCF, Asp.net 的 WebAPI.
#WCF
https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/whats-wcf
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 Dr. Vader 2018年5月24日 1:24
-
由于静态类只对当前进程有用,无法跨进程。您的需求是需要跨进程(多个WPF应用)共享数据, 有以下几种方法:
1 named pipe
How to: Use Named Pipes for Network Interprocess Communication
https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication
2 Memory-Mapped Files
https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files
3 .NET Remoting ,WCF
4 socket
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已标记为答案 Dr. Vader 2018年5月24日 1:24
全部回复
-
你好,
>>那么,如何让anotherView共享由myView初始化过的myStaticClass呢?这已经跨了应用程序了吧?
根据你的需求,多个项目共享同一个数据, 我建议你使用服务来做,几个项目调用同一个服务。 比如WCF, Asp.net 的 WebAPI.
#WCF
https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/whats-wcf
Best regards,
Zhanglong
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 Dr. Vader 2018年5月24日 1:24
-
由于静态类只对当前进程有用,无法跨进程。您的需求是需要跨进程(多个WPF应用)共享数据, 有以下几种方法:
1 named pipe
How to: Use Named Pipes for Network Interprocess Communication
https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication
2 Memory-Mapped Files
https://docs.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files
3 .NET Remoting ,WCF
4 socket
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
- 已标记为答案 Dr. Vader 2018年5月24日 1:24