我做一个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中的应用的更多相关文章

  1. 在WPF中使用依赖注入的方式创建视图

    在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...

  2. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

  3. MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信

    MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...

  4. MVVM设计模式和WPF中的实现(四)事件绑定

    MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  5. MVVM模式解析和在WPF中的实现(三)命令绑定

    MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  6. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  7. MVVM模式和在WPF中的实现(一)MVVM模式简介

    MVVM模式解析和在WPF中的实现(一) MVVM模式简介 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在 ...

  8. 【WPF】 Timer与 dispatcherTimer 在wpf中你应该用哪个?

    源:Roboby 1.timer或重复生成timer事件,dispatchertimer是集成到队列中的一个时钟.2.dispatchertimer更适合在wpf中访问UI线程上的元素 3.Dispa ...

  9. 在WPF中使用WinForm控件方法

    1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2.      在要使用WinForm控 ...

随机推荐

  1. python requests库学习笔记(下)

    1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions        #引入exc ...

  2. django-高级视图和url配置

    高级视图和url配置 一.URLconf技巧 1.流线型化函数导入 对于配置url,我们可以使用以下几种方式: (1)引入view中的函数 from firstSite.view import cur ...

  3. PHP实现水印效果(文字、图片)

    第一种 <?php /** * 功能:给一张图片加上水印效果 * $i 要加水印效果的图片 * $t 水印文字 * $size 文字大小 * $pos 水印的位置 * $color 文字的颜色 ...

  4. js获取对象长度和名称

    1.对象的长度不能用.length获取,用js原生的Object.keys可以获取到 var obj = {'name' : 'Tom' , 'sex' : 'male' , 'age' : '14' ...

  5. CodeForces-747A

    从sqrt(n)枚举到1,一旦满足一定是差最小的数. AC代码: #include<cstdio> #include<cmath> int main(){ int n; whi ...

  6. 实战DeviceIoControl 之六:访问物理端口

    Q 在NT/2000/XP中,如何读取CMOS数据? Q 在NT/2000/XP中,如何控制speaker发声? Q 在NT/2000/XP中,如何直接访问物理端口? A 看似小小问题,难倒多少好汉! ...

  7. mysql常用基础操作语法(六)--对数据排序和限制结果数量的条件查询【命令行模式】

    1.使用order by对查询的结果进行排序,asc升序,desc降序: 也可以在order by后指定多个字段名和排序方式进行多级排序: 2.使用limit限制查询结果的数量: 上图中的0,代表查询 ...

  8. STM32F4 串口实验中收不到超级终端发送的数据,调试工具却可以

    我用串口精灵发送数据没有问题,但是接收数据没反应. 串口接受的时候必须要用中断的,你发送只靠单一的标志位是可以判断的,但是接受的时候,你是一直停留在while里面,我们判断接受是否完成,通过检测是否收 ...

  9. 用vs2013+velt-0.1.4进行嵌入式开发 进行海思平台 UBOOT 开发

    1.1    什么是VELT VELT的全称是Visual EmbedLinuxTools,它是一个与visual gdb类似的visual studio插件,用以辅助完成Linux开发.利用这个插件 ...

  10. Java中集合List,Map和Set的区别

    Java中集合List,Map和Set的区别 1.List和Set的父接口是Collection,而Map不是 2.List中的元素是有序的,可以重复的 3.Map是Key-Value映射关系,且Ke ...