.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控 ...
随机推荐
- ajaxfileupload批量上传文件+图片尺寸限制
1.首先展示ajaxfileupload代码,在这里修改为批量上传 //ajaxfileupload不展示全部代码,这是修改前与修改后代码对比,目的是上传多个文件 createUploadForm: ...
- CENTOS6.6下nmon的监控
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn Installing Nmon By default nmon is ...
- Python自动化--语言基础6--模块操作之re、MySQL、Excel
1.Python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("aaa ...
- 用线性单元(LinearUnit)实现工资预测的Python3代码
功能:通过样本进行训练,让线性单元自己找到(这就是所谓机器学习)工资计算的规律,然后用两组数据进行测试机器是否真的get到了其中的规律. 原文链接在文尾,文章中的代码为了演示起见,仅根据工作年限来预测 ...
- 【重磅】PRO基础版免费,是时候和ExtJS说再见了!
三石的新年礼物 9 年了,FineUI(开源版)终于迎来了她的继任者 - FineUIPro(基础版),并且完全免费! FineUIPro(基础版)作为三石奉献给社区的一个礼物,绝对让你心动: 拥 ...
- 剑指offer 第九天
35.数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出 ...
- 【前端】Vue和Vux开发WebApp日志二、优化gulp任务
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_2.html 项目github地址:https://github.com/shamoyuu/vue- ...
- ubuntu下动态链接库的编译和使用实例
以下实例的环境是amd64 + ubuntu10.10 + g++ 4.4.5测试成功,在其他配置的机器上可能有一点区别. 动态库的使用方式中有两种,第一种是类似于静态库的使用,另一种我称之为 ...
- mysql常用基础操作语法(五)--对数据的简单条件查询【命令行模式】
1.单条件查询:select 字段名 from tablename where 条件: 2.简单多条件查询,使用&&或者between and等: 3.is null和is not n ...
- Java Web项目(Extjs)报错一
1.Java Web(Extjs)项目报错一 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -no ...