.net remoting在wpf中的应用
我做一个remotting的通讯测试,让控制台程序和wpf窗体通讯。具体实现的功能如下:
1、wpf获取信息在控制台上显示
2、控制台启动wpf,以及在屏幕前端显示
首先,我们来看项目结构:
共三个项目,它们分工明确,test是控制台和wpf的公共类库,它定义了双方通讯的接口,以及接口的实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace test
{
public interface IOfficeService
{
void Insert(string stream);
}
[Serializable]
public class OfficeServiceImplement : MarshalByRefObject, IOfficeService
{
public void Insert(string stream)
{
Console.WriteLine(stream);
}
} public interface IWPFService
{
IntPtr GetHandle();
}
[Serializable]
public class WPFServiceImplement : MarshalByRefObject, IWPFService
{
public static Func<IntPtr> GetWPFHandle { set; get; }
public IntPtr GetHandle()
{
if (GetWPFHandle != null)
{
return GetWPFHandle();
}
return IntPtr.Zero;
}
}
}
wfaKnowledgeWarehouse 是个控制台项目,用来接收从wpf传回来的消息,并打印到屏幕上:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Serialization.Formatters;
using System.Net.Sockets;
using System.Net;
using test;
using System.Diagnostics;
using System.Threading; namespace wfaKnowledgeWarehouse
{
class Program
{
public const string CHANNEL_NAME = "ConsoleService";
public const string OBJECT_URI = "ConsoleService.rem"; /// <summary>
/// 二进制信道处理
/// </summary>
public static SoapServerFormatterSinkProvider Provider = new SoapServerFormatterSinkProvider()
{
TypeFilterLevel = TypeFilterLevel.Full
}; static void Main(string[] args)
{ //定义一组服务
var channel = new HttpServerChannel(CHANNEL_NAME, , Provider);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(OfficeServiceImplement), OBJECT_URI, WellKnownObjectMode.Singleton); var consoleInfo = Console.ReadKey(); if (consoleInfo.Key == ConsoleKey.A)
{
//如果按下A,获取wpf窗体句柄 var ServiceUrl = "http://" + IPAddress.Loopback.ToString() + ":{0}/WPFService.rem";
var WpfService = Activator.GetObject(typeof(IWPFService), string.Format(ServiceUrl, )) as IWPFService; try
{
//启动客户端
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"D:\mywork\WordAddInTest2010\WpfTest\WpfTest\bin\Debug\WpfTest.exe";
info.Arguments = "";
info.WindowStyle = ProcessWindowStyle.Minimized;
Process pro = Process.Start(info); Thread.Sleep(); var window= WpfService.GetHandle(); Console.WriteLine(window.ToInt32()); if (Win32APIs.IsIconic(window) != IntPtr.Zero)
{
Win32APIs.ShowWindow(window, Win32APIs.WindowState.SW_SHOWNOACTIVATE);
} Win32APIs.SetForegroundWindow(window); }
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Console.Read();
}
}
}
WpfTest 是wpf项目:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using test;
using System.Windows.Interop;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using System.Windows.Threading; namespace WpfTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public const string CHANNEL_NAME = "WPFService";
public const string OBJECT_URI = "WPFService.rem"; /// <summary>
/// 二进制信道处理
/// </summary>
public static SoapServerFormatterSinkProvider Provider = new SoapServerFormatterSinkProvider()
{
TypeFilterLevel = TypeFilterLevel.Full
}; public MainWindow()
{
InitializeComponent();
} public static IntPtr WindowPtr { set; get; } private void Button_Click(object sender, RoutedEventArgs e)
{
var ServiceUrl = "http://" + IPAddress.Loopback.ToString() + ":{0}/ConsoleService.rem";
var WordService = Activator.GetObject(typeof(IOfficeService), string.Format(ServiceUrl, )) as IOfficeService; try
{
WordService.Insert("wbq");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
} public IntPtr GetHandle()
{
return WindowPtr;
} private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowPtr = new WindowInteropHelper(this).Handle;
var channel = new HttpServerChannel(CHANNEL_NAME, , Provider);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(WPFServiceImplement), OBJECT_URI, WellKnownObjectMode.Singleton);
WPFServiceImplement.GetWPFHandle = GetHandle;
}
}
}
wpf项目定义了提供了WPFService服务,同时它又使用控制台提供的ConsoleService服务。
小结:想想,我们程序员为老板,为公司提供了一定的技术服务,同时得到一些报酬;老板和公司得到一些技术服务的同时,给程序员付一定的报酬。要和这个社会打交道,大抵如此吧。
.net remoting在wpf中的应用的更多相关文章
- 在WPF中使用依赖注入的方式创建视图
在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信
MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...
- MVVM设计模式和WPF中的实现(四)事件绑定
MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM模式解析和在WPF中的实现(三)命令绑定
MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM模式和在WPF中的实现(二)数据绑定
MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM模式和在WPF中的实现(一)MVVM模式简介
MVVM模式解析和在WPF中的实现(一) MVVM模式简介 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在 ...
- 【WPF】 Timer与 dispatcherTimer 在wpf中你应该用哪个?
源:Roboby 1.timer或重复生成timer事件,dispatchertimer是集成到队列中的一个时钟.2.dispatchertimer更适合在wpf中访问UI线程上的元素 3.Dispa ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
随机推荐
- Angular2 ^ 资源链接
Angular2 资源链接 Material Desgin 2 githubhttps://github.com/Promact/md2 DEMOhttp://code.promactinfo.co ...
- IE8兼容问题总结---trim()方法
1.IE8不支持,jquery的trim()去空格的方法 错误表现 : 会报错,对象不支持此属性或方法; 解决办法 : 使用正则匹配空格 例如 : /^\s+|\s+$/greplace(/^\s+| ...
- 洛谷P3796 - 【模板】AC自动机(加强版)
原题链接 Description 模板题啦~ Code //[模板]AC自动机(加强版) #include <cstdio> #include <cstring> int co ...
- 在SpringBoot中配置定时任务
前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...
- 看eShopOnContainers学一个EventBus
最近在看微软eShopOnContainers 项目,看到EventBus觉得不错,和大家分享一下 看完此文你将获得什么? eShop中是如何设计事件总线的 实现一个InMemory事件总线eShop ...
- 源码编译安装Apache-附一键部署脚本
1.进入apache官网https://www.apache.org/,点击Download 2.如图选择 3.选择httpd 4.下载两个包,2.2为CentOS6使用,2.4为CentOS7使用 ...
- redis 突然大量逐出导致读写请求block
现象 redis作为缓存场景使用,内存耗尽时,突然出现大量的逐出,在这个逐出的过程中阻塞正常的读写请求,导致 redis 短时间不可用: 背景 redis 中的LRU是如何实现的? 当mem_used ...
- 【前端】Vue和Vux开发WebApp日志四、增加命令行参数
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_4.html 项目github地址:https://github.com/shamoyuu/vue- ...
- android自定义View的绘制原理
每天我们都会使用很多的应用程序,尽管他们有不同的约定,但大多数应用的设计是非常相似的.这就是为什么许多客户要求使用一些其他应用程序没有的设计,使得应用程序显得独特和不同. 如果功能布局要求非常定制化, ...
- 笔记︱金融风险控制基础常识——巴塞尔协议+信用评分卡Fico信用分
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 本笔记源于CDA-DSC课程,由常国珍老师主讲 ...