步骤 1:测试 Echo 适配器的出站处理程序

步骤 1/2

完成时间: 15 分钟

在此步骤中,你将测试 Echo 适配器提供的三个出站操作。 你将使用 Visual Studio、添加适配器服务参考 Visual Studio Plug-In 和自定义代码执行此操作。

必备条件

若要完成此步骤,必须已完成 教程 1:开发回显适配器

创建 Visual Studio 项目

  1. 启动 Visual Studio。

  2. 在 Visual Studio 的“ 文件 ”菜单上,指向“ 新建”,然后单击“ 项目”。

  3. 在“新建项目” 对话框中执行以下操作:

    使用此选项 要执行此操作
    项目类型 单击 “Visual C#”。
    模板 单击“控制台应用程序”
    名称 键入 ConsumeEchoAdapter_Outbound
    位置 键入 C:\Tutorials
    解决方案名称 键入 ConsumeEchoAdapter_Outbound
  4. 单击 “确定”

  5. 在 Visual Studio 的“ 文件 ”菜单上,单击“ 全部保存”。

浏览、搜索和生成 WCF 客户端

  1. 在 Visual Studio 解决方案窗格中,右键单击 ConsumeEchoAdapter_Outbound 项目,然后选择 “添加适配器服务引用” 以启动“添加适配器服务引用”插件。

  2. “添加适配器服务引用” 屏幕中,选择一个绑定。 这是通过选择 echoAdapterBindingV2 来完成的

  3. 接下来,通过单击“ 配置...”来配置适配器和连接属性。 此时会显示 “配置适配器” 屏幕。

  4. “配置适配器” 屏幕中,选择“ URI 属性 ”选项卡以配置连接属性。 请注意,显示自定义回显适配器类别 - 连接格式。 在 “格式” 类别下,将 EchoInUpperCase 更改为 True

  5. “配置适配器” 屏幕中,选择“ 绑定属性” 选项卡以配置适配器属性。 请注意,将显示自定义回显适配器类别 “入站 ”和 “杂项 ”。 在 “杂项” 类别下,将 “计数” 更改为 “3”。

  6. 单击“ 确定 ”关闭 “配置适配器” 屏幕,并返回到 “添加适配器服务引用” 屏幕。

  7. 接下来,单击“ 连接 ”以连接到回显适配器 (它支持) 的假设业务线系统。 片刻后,连接状态应更改为 “已连接 ”,应填充“ 选择类别) ”下的“类别树” (。

  8. 在“类别树”中,单击“ 主类别”。 这将使用三个出站操作填充可用类别和操作的列表。 将没有类别。

    注意

    默认协定类型为“出站”。 类别结果将与此协定类型匹配。

  9. “可用类别和操作”中,选择所有三个操作。 当存在大量操作时,可以使用搜索来缩小选择范围;在本例中,只有三个。 单击“ 添加 ”,使所选操作成为生成的 WCF 接口的一部分。

  10. 单击“ 确定 ”以生成 WCF 接口。 这会将应用程序配置文件 (app.config) 和 WCF 客户端代理 (EchoAdapterBindingClient.cs) 添加到项目。

  11. 单击 Visual Studio 菜单上的“ 文件 ”,然后选择“ 全部保存”。

配置适配器身份验证

  1. 在 Visual Studio 解决方案窗格中,双击 app.config

  2. address 元素中 endpoint 查找 属性。 该属性应与下面类似:

    <endpoint address="echov2://lobhostname/lobapplication?enableAuthentication=False&echoInUpperCase=True"  
        binding="echoAdapterBindingV2" bindingConfiguration="EchoAdapterBinding"  
        contract="EchoOutboundContract" name="EchoAdapterBinding_EchoOutboundContract" />  
    

    enableAuthenticationFalse 更改为 True ,如下所示。 这将要求调用应用程序将凭据传递给适配器。

    <endpoint address="echov2://lobhostname/lobapplication?enableAuthentication=True&echoInUpperCase=True"  
        binding="echoAdapterBindingV2" bindingConfiguration="EchoAdapterBinding"  
        contract="EchoOutboundContract" name="EchoAdapterBinding_EchoOutboundContract" />  
    
  3. 通过单击 Visual Studio 菜单上的“ 文件 ”并选择“ 全部保存”来保存解决方案。

创建示例 XML 文件

  1. 启动记事本的实例。 使用“开始”菜单,单击“ 所有程序 | 附件 ”,然后选择 “记事本”。

  2. 将以下示例数据复制到记事本编辑器中。

    <?xml version="1.0" encoding="utf-16"?>  
    <ns0:greeting xmlns:ns0="echov2://microsoft.adapters.samples.echov2/PreDefinedTypes">  
      <ns0:address>  
        <ns0:street1>123 Microsoft Way</ns0:street1>  
        <ns0:street2>Building # 4599</ns0:street2>  
        <ns0:city>Redmond</ns0:city>  
        <ns0:state>WA</ns0:state>  
        <ns0:zip>98052</ns0:zip>  
      </ns0:address>  
      <ns0:greetingText>Welcome to Redmond!</ns0:greetingText>  
    </ns0:greeting>              
    
  3. 在“记事本”菜单上,单击“ 文件 ”,然后选择“ 另存为...”。 键入“CustomGreetingInstance.xml”作为文件名,选择 Unicode 作为编码,然后将其保存到项目目录或其他合适的位置。 请记下完整路径和文件名,供以后参考。

  4. 成功保存文件时关闭文本编辑器。

测试回显适配器

  1. 在解决方案资源管理器中,双击 Program.cs 文件。

  2. 在 Visual Studio 编辑器的 Main 方法中,添加以下代码行以创建生成的 WCF 客户端的实例。

    EchoOutboundContractClient client = new EchoOutboundContractClient();  
    
  3. 现在,添加用于为适配器建立凭据的代码。 在回显适配器中启用身份验证时,它将检查是否存在用户名,但不会检查该值。

    // pass client credentials  
    client.ClientCredentials.UserName.UserName = "username";  
    
  4. 继续添加代码以调用 EchoStrings 操作。

    // Invoke EchoStrings()  
    Console.WriteLine("Invoking EchoStrings() method against the adapter...");  
    string[] response = client.EchoStrings("Bonjour!");  
    foreach (string data in response)  
    {  
        Console.WriteLine(data);  
    }  
    
  5. 继续添加代码以调用 EchoGreetings 操作。

    // Invoke EchoGreetings()  
    Console.WriteLine("\nInvoking EchoGreetings() method against the adapter...");  
    microsoft.adapters.samples.echov2.Types.Greeting greeting = new microsoft.adapters.samples.echov2.Types.Greeting();  
    greeting.id = Guid.NewGuid();  
    greeting.sentDateTime = DateTime.Now;  
    greeting.greetingText = "Hello World!";  
    greeting.name = new microsoft.adapters.samples.echov2.Types.Name();  
    greeting.name.salutation = microsoft.adapters.samples.echov2.Types.Salutation.Miss;  
    greeting.name.firstName = "Jane";  
    greeting.name.middleName = "Z.";  
    greeting.name.lastName = "Smith";  
    microsoft.adapters.samples.echov2.Types.Greeting[] greetingArray = client.EchoGreetings(greeting);  
    foreach (microsoft.adapters.samples.echov2.Types.Greeting data in greetingArray)  
    {  
        Console.WriteLine(data.id + " " + data.sentDateTime + " " + data.greetingText + " " + data.name.firstName );  
    }  
    
  6. 继续添加代码以调用 EchoCustomGreetingsFromFile 操作。

    // Invoke EchoCustomGreetingFromFile()  
    Console.WriteLine("\nInvoking EchoCustomGreetingFromFile() method against the adapter ...");  
    // Copy the sample data from CustomGreeting-instance.xml file and place in appropriate folder  
    // as specified in the operation parameter  
    microsoft.adapters.samples.echov2.PreDefinedTypes.CustomGreeting   
    // change the Uri to point to the greeting instance xml file you created  
    customGreeting = client.EchoCustomGreetingFromFile(new Uri(@"c:\CustomGreetingInstance.xml"));  
    Console.WriteLine(customGreeting.greetingText + " " + customGreeting.address.city);  
    client.Close();  
    Console.ReadLine();  
    
  7. 在 EchoCustomGreetingsFromFile 测试代码中,确保自定义问候语使用在上一过程中创建的文件。 更改代码以反映文件的位置。

  8. 在 Visual Studio 的“ 文件 ”菜单上,单击“ 全部保存”。

  9. 运行应用程序。 应该会看到与下面类似的输出:

    正在针对适配器调用 EchoStrings () 方法...

    你好!

    你好!

    你好!

    你好!

    你好!

    正在针对适配器调用 EchoGreetings () 方法...

    179665bb-db21-42ac-810e-77ebfa99d460 2007/9/13 下午 3:18:07 Hello World! Jane

    179665bb-db21-42ac-810e-77ebfa99d460 2007/9/13 下午 3:18:07 Hello World! Jane

    179665bb-db21-42ac-810e-77ebfa99d460 2007/9/13 下午 3:18:07 Hello World! Jane

    179665bb-db21-42ac-810e-77ebfa99d460 2007/9/13 下午 3:18:07 Hello World! Jane

    179665bb-db21-42ac-810e-77ebfa99d460 2007/9/13 下午 3:18:07 Hello World! Jane

    针对适配器调用 EchoCustomGreetingFromFile () 方法...

    欢迎使用雷德蒙德! Redmond

  10. 按 Enter 键停止程序。

我刚才做了些什么?

在此步骤中,你为教程 1 中开发的 Echo 适配器公开的三个出站操作创建了一个测试应用程序。 为此,你创建了一个 Visual Studio 项目,生成了一个 WCF 服务,并提供了用于托管 WCF 服务的代码。 最后,运行测试应用程序。

后续步骤

若要测试入站操作,请继续执行 步骤 2:测试回显适配器的入站处理程序

另请参阅

教程 2:从 .NET 使用 Echo 适配器
步骤 2:测试 Echo 适配器的入站处理程序