完成时间: 15 分钟
在此步骤中,你将测试 Echo Adapter 提供的三个出站操作。 你将使用 Visual Studio、Add Adapter Service Reference (添加适配器服务引用) 的 Visual Studio Plug-In 和自定义代码来完成此任务。
先决条件
若要完成此步骤,必须已完成 教程 1:开发 Echo 适配器。
创建 Visual Studio 项目
启动 Visual Studio。
在 Visual Studio 的“ 文件 ”菜单上,指向“ 新建”,然后单击“ 项目”。
在“新建项目” 对话框中执行以下操作:
用这个 要执行的操作 项目类型 单击 “Visual C#”。 模板 单击 “控制台应用程序”。 名称 键入 ConsumeEchoAdapter_Outbound。 位置 键入 C:\Tutorials。 解决方案名称 键入 ConsumeEchoAdapter_Outbound。 单击 “确定” 。
在 Visual Studio 的 “文件 ”菜单上,单击“ 全部保存”。
浏览、搜索和生成 WCF 客户端
在 Visual Studio 解决方案窗格中,右键单击 ConsumeEchoAdapter_Outbound 项目,然后选择 “添加适配器服务引用 ”以启动“添加适配器服务引用”插件。
在 “添加适配器服务引用 ”屏幕中,选择绑定。 这是通过选择 echoAdapterBindingV2 来完成的。
接下来,单击“ 配置...”配置适配器和连接属性。 此时会显示 “配置适配器 ”屏幕。
在 “配置适配器 ”屏幕中,选择“ URI 属性 ”选项卡以配置连接属性。 请注意,将显示自定义回显适配器类别 - 连接 和 格式。 在 “格式 ”类别下,将 EchoInUpperCase 更改为 True。
在 “配置适配器 ”屏幕中,选择“ 绑定属性 ”选项卡以配置适配器属性。 请注意,将显示自定义回显适配器类别 入站 和 杂项 。 在 Misc 类别下,将 计数 更改为 3。
单击 “确定 ”关闭 “配置适配器 ”屏幕,然后返回到 “添加适配器服务引用 ”屏幕。
接下来,单击 “连接 ”以连接到 Echo 适配器(以及它支持的假设业务线系统)。 片刻后,连接状态应更改为 “已连接 ”,“ 选择类别”下的“类别树”应填充。
在类别树中,单击 “主类别”。 这将用三个出站操作填充可用类别和操作的列表。 不会有类别。
注释
默认协定类型为出站。 类别结果将与此协定类型匹配。
在 “可用类别和操作”中,选择所有三个操作。 当有大量操作时,可以使用搜索来缩小选择范围;在这种情况下,只有三个。 单击“ 添加 ”使所选作部分成为生成的 WCF 接口的一部分。
单击“ 确定 ”以生成 WCF 接口。 这将向项目添加应用程序配置文件(app.config)和 WCF 客户端代理(EchoAdapterBindingClient.cs)。
在 Visual Studio 菜单上单击“ 文件 ”,然后选择“ 全部保存”。
配置适配器身份验证
在 Visual Studio 解决方案窗格中,双击 app.config。
在
endpoint元素中查找address属性。 该属性应与下面类似:<endpoint address="echov2://lobhostname/lobapplication?enableAuthentication=False&echoInUpperCase=True" binding="echoAdapterBindingV2" bindingConfiguration="EchoAdapterBinding" contract="EchoOutboundContract" name="EchoAdapterBinding_EchoOutboundContract" />将 enableAuthentication 从 False 更改为 True ,如下所示。 这将要求调用应用程序将凭据传递给适配器。
<endpoint address="echov2://lobhostname/lobapplication?enableAuthentication=True&echoInUpperCase=True" binding="echoAdapterBindingV2" bindingConfiguration="EchoAdapterBinding" contract="EchoOutboundContract" name="EchoAdapterBinding_EchoOutboundContract" />单击 Visual Studio 菜单上 的“文件 ”并选择“ 全部保存”,保存解决方案。
创建示例 XML 文件
启动记事本。 使用“开始”菜单,单击“ 所有程序 | 附件 ”,然后选择 “记事本”。
将以下示例数据复制到记事本编辑器中。
<?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>在记事本菜单上,单击“ 文件 ”,然后选择“ 另存为...”。 键入文件名的“CustomGreetingInstance.xml”,然后选择 Unicode 进行编码,然后将其保存到项目目录或其他合适的位置。 记下完整路径和文件名供以后参考。
成功保存文件时关闭文本编辑器。
测试 Echo 适配器
在解决方案资源管理器中,双击 Program.cs 文件。
在 Visual Studio 编辑器的 Main 方法中,添加以下代码行来创建生成的 WCF 客户端的实例。
EchoOutboundContractClient client = new EchoOutboundContractClient();现在添加用于为适配器建立凭据的代码。 在 Echo Adapter 中启用身份验证时,它将检查是否存在用户名,但不会检查该值。
// pass client credentials client.ClientCredentials.UserName.UserName = "username";继续添加代码来调用 EchoStrings 操作。
// Invoke EchoStrings() Console.WriteLine("Invoking EchoStrings() method against the adapter..."); string[] response = client.EchoStrings("Bonjour!"); foreach (string data in response) { Console.WriteLine(data); }继续添加代码以调用 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 ); }继续添加代码以调用 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();在 EchoCustomGreetingsFromFile 测试代码中,确保自定义问候语使用在上一过程中创建的文件。 更改代码以反映文件的位置。
在 Visual Studio 的 “文件 ”菜单上,单击“ 全部保存”。
运行该应用程序。 应该会看到与下面类似的输出:
对适配器调用 EchoStrings() 方法...
你好!
你好!
你好!
你好!
你好!
对适配器调用 EchoGreetings() 方法...
179665bb-db21-42ac-810e-77ebfa99d460 9/13/2007 3:18:07 PM Hello World! 珍
179665bb-db21-42ac-810e-77ebfa99d460 9/13/2007 3:18:07 PM Hello World! 珍
179665bb-db21-42ac-810e-77ebfa99d460 9/13/2007 3:18:07 PM Hello World! 珍
179665bb-db21-42ac-810e-77ebfa99d460 9/13/2007 3:18:07 PM Hello World! 珍
179665bb-db21-42ac-810e-77ebfa99d460 9/13/2007 3:18:07 PM Hello World! 珍
对适配器调用 EchoCustomGreetingFromFile() 方法...
欢迎来到雷德蒙德! 雷德蒙德
按 Enter 键停止程序。
我刚刚做了什么?
在此步骤中,你为在教程一中开发的 Echo 适配器所公开的三个出站操作创建了一个测试应用程序。 为此,你创建了一个 Visual Studio 项目,生成了 WCF 服务,并提供了用于托管 WCF 服务的代码。 最后,运行测试应用程序。
后续步骤
若要测试入站操作,请转到 步骤 2:测试 Echo 适配器的入站处理程序。