.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控 ...
随机推荐
- 在Vue2.0中集成UEditor 富文本编辑器
在vue的'项目中遇到了需要使用富文本编辑器的需求,在github上看了很多vue封装的editor插件,很多对图片上传和视频上传的支持并不是很好,最终还是决定使用UEditor. 这类的文章网上有很 ...
- 1.3 fractions模块
数学世界中,浮点数还可以用分数形式展示,不可约简的分数形式往往更简洁直观. 问题来了,Python中如何输出不可约简的分数形式呢? 答案:用Fraction类来实现.这个类属于标准库的fracti ...
- 轻松掌握VS Code开发.Net Core及创建Xunit单元测试
前言 本篇文章主要还是介绍使用 VS Code 进行.Net Core开发和常用 CLI命令的使用,至于为啥要用VS Code ,因为它是真的是好看又好用 :) ,哈哈,主要还是为了跨平台开发做准备. ...
- UVA - 11270 轮廓线DP
其实这题还能用状压DP解决,可是时间达到2000ms只能过掉POJ2411.状压DP解法详见状压DP解POJ2411 贴上POJ2411AC代码 : 2000ms 时间复杂度h*w*(2^w)*(2^ ...
- Hadoop1.x原理
将这种单机的工作进行分拆,变成协同工作的集群,这就是分布式计算框架设计.使得计算机硬件类似于应用程序中资源池的资源,使用者无需关心资源的分配情况,从而最大化了硬件资源的使用价值.分布式计算也是如此,具 ...
- linux yum源配置及vim运用
redhat7默认没有yum模板,需要自己创建[root@localhost ~]# mount /dev/cdrom /root/iso/(挂载镜像)mount: /dev/sr0 写保护,将以只读 ...
- redis的密码设置(windows与linux相同)
接着我们昨天的说,昨天redis的启动已经了解,今天来说说redis的密码设置.(不管怎么说redis也是数据库,也需要密码) 修改密码可以2种行径.第一种,直接修改配置文件,打开redis.conf ...
- VxWorks:添加自己组件到Tornado
项目要求将cpci的驱动做成Tornado组件,尝试了一下! Folder FOLDER_CPCI { //上层组件设置 NAME cpci componen ...
- dedecms 在首页调取文章内容
方法一:arcticle 标签 加上channeleid {dede:arclist' addfields='body' channelid='1'}[field:body/]{/dede:arcli ...
- hql查询实例
1.设计思路 (1)在页面中设计一个下拉框,数据取自数据库: (2)查询是用hql查询. 2.设计实例 (1)Java模型层 public class Tree { private String id ...