.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控 ...
随机推荐
- Oracle创建表时Storage参数具体含义
本文通过图表和实例的阐述在Oracle数据库创建新表时Storage的参数具体含义. 可用于:表空间.回滚段.表.索引.分区.快照.快照日志 参数名称 缺省值 最小值 最大值 说明 INITIAL 5 ...
- hadoopmaster主机上传文件出错: put: File /a.txt._COPYING_ could only be replicated to 0 nodes instead of minReplication (=1). There are 3 datanode(s) running and 3 node(s) are excluded in this operation.
刚开始装好hadoop的时候,namenode机上传文件没有错误,今天打开时突然不能上传文件,报错 put: File /a.txt._COPYING_ could only be replicate ...
- 关于c++栈溢出的问题
我自己定义了一个数据类型node,嵌套在另一个数据类型当中时候,用到了delete函数, 在我node的声明当中声明了几个指针 在我的析构函数中却调用了delet函数 结果程序结果是能跑出来 提示我栈 ...
- 基于WebSocketSharp 的IM 简单实现
websocket-sharp 是一个websocket的C#实现,支持.net 3.5及以上来开发服务端或者客户端.本文主要介绍用websocket-sharp来做服务端.JavaScript做客户 ...
- 高通ASOC中的codec驱动
ASOC的出现是为了让codec独立于CPU,减少和CPU之间的耦合,这样同一个codec驱动就无需修改就可以匹配任何一款平台. 在Machine中已经知道,snd_soc_dai_link结构就指明 ...
- PAT 1002. A+B for Polynomials
思路:就是两个多项式做加法–指数相同的相加即可,输出的时候按照指数递减输出,并且系数为0的项不输出. AC代码 #include <stdio.h> #include <vector ...
- 关于adb is down 的两个解决方案
在Android开发过程中经常遇到这样的一个问题,The connection to adb is down, and a severe error has occured. 解决方案一: 1.为了以 ...
- RTlinux3.2安装
( 1 ).前言 2003 年以后, fmslabs 的 RTLinux Free 版本为 3.2Pre ,和以前的 RTLinux 3.1 比较,不再需要必须从 2.4.4 的内核上安装. RTLi ...
- BIOS简介
BIOS简介: BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".其实,它是一组固化到计 ...
- FusionCharts封装-Value
Data.java: /** * @Title:Data.java * @Package:com.fusionchart.model * @Description:FusionCharts 封装dat ...