• cad.net 跨进程通讯Remoting


    cad跨进程通讯Remoting

    正在研究cad的跨进程通讯,
    难点在于cad是服务端的话,让客户端直接调用暴露的接口,而不会暴露实现,
    因此写了一个Remoting的案例,如下代码就可以进行了.

    至于WCF工程,由于它的App.config处理有些地方不是很理解,之后再仔细研究.

    动图演示

    工程结构

    1. 服务端工程 --> cad的类库,透过cad调用 --> 引用端口工程
    2. 客户端工程 --> 控制台工程 --> 引用端口工程
    3. 端口工程 --> 类库

    1.服务端工程

    #if !HC2020
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
    #else
    using GrxCAD.ApplicationServices;
    using GrxCAD.DatabaseServices;
    using GrxCAD.EditorInput;
    using Acap = GrxCAD.ApplicationServices.Application;
    #endif
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System;
    using JoinBox.IAdapter;
    
    namespace JoinBox.Service
    {
        public class Remoting
        {
            /// <summary>
            /// 注册服务管道 Remoting,实现占用端口
            /// </summary>
            [JoinBoxInitialize]
            public void RegisterServerChannel()
            {
                var channel = new TcpServerChannel(AdapterHelper.Port);
                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(BaseService),            //服务端实现(通讯地址接口)的函数
                    AdapterHelper.TcpPortAdapter,   //通讯地址接口
                    WellKnownObjectMode.Singleton);
            }
        }
    
        // 服务端实现接口工程的方法,在cad命令栏打印
        public class BaseService : MarshalByRefObject, IJoinBoxAdapter
        {
            public void PostAcedString(string str)
            {
                //阻塞线程并切换上下文,另见 https://www.cnblogs.com/JJBox/p/14179921.html
                AutoGo.SyncManager.SyncContext.Post(() => {
                    Document doc = Acap.DocumentManager.MdiActiveDocument;
                    Database db = doc.Database;
                    Editor ed = doc.Editor;
                    ed.WriteMessage(str);
                });
            }
        }
    }
    

    2.客户端工程 JoinBox.Client

    using System;
    using System.Threading;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using JoinBox.IAdapter;
    
    namespace JoinBox.Client
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                ChannelServices.RegisterChannel(new TcpClientChannel(), false);
                var adapter = (IJoinBoxAdapter)Activator.GetObject(typeof(IJoinBoxAdapter), AdapterHelper.TcpPortFullName);
                while (true)
                {
                    var str = DateTime.Now.ToString() + "
    ";
                    Console.WriteLine(str);
                    adapter.PostAcedString(str);//调用接口工程的方法.
                    Thread.Sleep(1000);
                }
            }
        }
    }
    

    3.接口工程 JoinBox.IAdapter

    此工程同时暴露给服务端和客户端利用

    namespace JoinBox.IAdapter
    {
        //公共接口和方法
        public interface IJoinBoxAdapter
        {
            public void PostAcedString(string str);
        }
    
        //公共的属性
        public class AdapterHelper
        {
            /// <summary>
            /// 端口
            /// </summary>
            public static int Port               = 15341;
            /// <summary>
            /// 通讯地址(接口)--服务端用
            /// </summary>
            public static string TcpPortAdapter  = "jingjing/" + nameof(IJoinBoxAdapter);
            /// <summary>
            /// 通讯地址(完整)--客户端用
            /// </summary>
            public static string TcpPortFullName = $"tcp://localhost:{Port}/" + TcpPortAdapter;
        }
    }
    

    (完)

  • 相关阅读:
    前端CSS-font属性,超链接的美化,css精灵,background综合属性
    iOS App上架流程(2016详细版)
    iOS中使用正则
    iOS开发--JS调用原生OC篇
    iOS开发--OC调用JS篇
    CocoaPods 的简单快速安装方法
    iOS开发小技巧 -- tableView-section圆角边框解决方案
    Mac合并分区
    iOS开发小技巧
    iOS开发中遇到的错误整理
  • 原文地址:https://www.cnblogs.com/JJBox/p/14992966.html
Copyright © 2020-2023  润新知