主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

使用原生 .NET API 访问可远程访问的 .NET 程序集:幻方

为什么要使用本机 .NET API?

创建远程组件后,您可以使用本机 .NET API 设置服务器应用程序和客户端。有关选择适合您的访问需求的正确 API 的详细信息,请参阅比较 MWArray 和用于远程程序集的本机 .NET API

您可能使用本机 .NET API 而不是 MWArray API 的一些原因是:

  • 您希望使用标准 .NET 类型传递参量和返回值,并且您或您的用户不会广泛使用特定于 MATLAB® 数据类型。

  • 您想从未安装 MATLAB 版本的客户端计算机访问您的组件。

有关使用 MWArray API 访问组件的信息,请参阅使用 MWArray API 访问可远程访问的 .NET 程序集

编码并构建托管服务器应用程序和配置文件

服务器应用程序将托管您内置的远程组件创建可远程访问的 .NET 程序集

在单独的进程中运行的客户端应用程序将访问服务器应用程序托管的远程组件。使用 Microsoft® Visual Studio® 工程文件 MagicSquareServer\MagicSquareServer.csproj 构建服务器:

  1. 将生成的组件程序集的引用更改为 MagicSquareCompNative.dll

  2. 选择合适的构建平台。

  3. 选择 DebugRelease 模式。

  4. 构建 MagicSquareServer 工程。

  5. 提供 MagicSquareServer 的配置文件。

MagicSquareServer 代码

服务器的 C# 代码位于文件 MagicSquareServer\MagicSquareServer.cs 中。MagicSquareServer.cs 服务器代码如下所示:

using System;
   using System.Runtime.Remoting;

   namespace MagicSquareServer
     {
       class MagicSquareServer
         {
           static void Main(string[] args)
             {
               RemotingConfiguration.Configure
                 (@"..\..\..\..\MagicSquareServer.exe.config");

               Console.WriteLine("Magic Square Server started...");

               Console.ReadLine();
             }
         }
     }
此代码执行以下操作:

  • 读取相关的配置文件以确定它将托管的组件的名称、要使用的远程协议和消息格式,以及远程组件的租用时间。

  • 表示服务器处于活动状态,并等待输入回车符后再终止。

MagicSquareServer 配置文件

MagicSquareServer 的配置文件位于文件 MagicSquareServer\MagicSquareServer.exe.config 中。以 XML 编写的整个配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="SingleCall"
                   type="MagicSquareCompNative.MagicSquareClass,  
                         MagicSquareCompNative" 
                   objectUri="MagicSquareClass.remote" />
      </service>
      <lifetime leaseTime= "5M" renewOnCallTime="2M"
                leaseManagerPollTime="10S" />
      <channels>
        <channel ref="tcp" port="1234">    
          <serverProviders>            
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>
        </channel>                
      </channels>     
    </application>
    <debug loadTypes="true"/>
  </system.runtime.remoting>
   </configuration>

本规范规定:

  • 访问远程组件的模式 - 在本例中为单一调用模式

  • 远程组件的名称、组件集以及用于访问远程组件的对象 URI(统一资源标识符)

  • 远程组件的租用时间

  • 远程协议 (TCP/IP) 和端口号

  • 消息格式化程序 (binary) 和通信通道的权限(full 信任)

  • 服务器调试选项

编码并构建客户端应用程序和配置文件

运行在单独进程中的客户端应用程序访问运行在内置服务器应用程序中的远程组件编码并构建托管服务器应用程序和配置文件。使用 Microsoft Visual Studio 工程文件 MagicSquareClient\MagicSquareClient.csproj 构建远程客户端。要使用 Microsoft Visual Studio 创建远程客户端:

  1. 将生成的组件程序集的引用更改为 MagicSquareCompNative.dll

  2. 将生成的接口程序集的引用更改为 IMagicSquareCompNative.dll

  3. 选择合适的构建平台。

  4. 选择 DebugRelease 模式。

  5. 构建 MagicSquareClient 工程。

  6. 提供 MagicSquareServer 的配置文件。

MagicSquareClient 代码

客户端的 C# 代码位于文件 MagicSquareClient\MagicSquareClient.cs 中。

 MagicSquareClient.cs

此代码执行以下操作:

  • 客户端读取相关的配置文件来获取远程组件的名称和位置。

  • 客户端使用静态 Activator.GetObject 方法实例化可远程访问对象

  • 从此时起,远程客户端调用可远程组件上的方法就像调用本地组件方法一样。

MagicSquareClient 配置文件

幻方客户端的配置文件位于文件 MagicSquareClient\MagicSquareClient.exe.config 中。以 XML 编写的配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="MagicSquareServer" 
            value="tcp://localhost:1234/MagicSquareClass.remote"/>    
  </appSettings>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel name="MagicSquareChannel" ref="tcp" port="0">        
          <clientProviders>            
            <formatter ref="binary" />
          </clientProviders>
          <serverProviders>            
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>            
        </channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

本规范规定:

  • 远程组件服务器的名称和远程组件 URI(统一资源标识符)

  • 远程协议 (TCP/IP) 和端口号

  • 消息格式化程序 (binary) 和通信通道的权限(full 信任)

启动服务器应用程序

通过执行以下操作启动服务器:

  1. 打开 DOS 或 UNIX® 命令行窗口并导航到 MagicSquareServer\bin\x86\v4.0\Debug

  2. 运行 MagicSquareServer.exe。您将看到消息:

    Magic Square Server started...

启动客户端应用程序

通过执行以下操作启动客户端:

  1. 打开 DOS 或 UNIX 命令行窗口并导航到 MagicSquareClient\bin\x86\v4.0\Debug

  2. 运行 MagicSquareClient.exeMATLAB Runtime 初始化后,您应该看到以下输出:

    Magic square of order 4
    
    Element(0,0)= 16
    Element(0,1)= 2
    Element(0,2)= 3
    Element(0,3)= 13
    Element(1,0)= 5
    Element(1,1)= 11
    Element(1,2)= 10
    Element(1,3)= 8
    Element(2,0)= 9
    Element(2,1)= 7
    Element(2,2)= 6
    Element(2,3)= 12
    Element(3,0)= 4
    Element(3,1)= 14
    Element(3,2)= 15
    Element(3,3)= 1