当前位置: 首页 > news >正文

换种不同的方式调用WCF服务[提供源代码下载]

我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码和配置;通过ChannelFactory<TChannel>创建服务代理对象。在这篇文章中,我们采用一种独特的方式进行服务的调用。从本质上讲,我们只要能够创建于服务端相匹配的终结点,就能够实现正常的服务调用。在WCF客户端元数据架构体系中,利用MetadataExchangeClient可以获取服务的元数据,而利用MetadataImporter将获取的元数据导入成ServiceEndpoint对象。在本例中,我们将利用这两个组件定义了一个独特的服务调用的简单的例子,相信可以帮助读者进一步加深对WCF元数据框架体系的理解。 (Source从这里下载)

我们依然采用我们熟悉的计算服务的例子,下面是该服务相应的服务契约、服务类型的定义和寄宿该服务采用的配置。

1: using System.ServiceModel;
2: namespace Artech.ServiceInvocationViaMetadata.Contracts
3: {
4: [ServiceContract(Namespace = "http://www.artech.com/")]
5: public interface ICalculator
6: {
7: [OperationContract]
8: double Add(double x, double y);
9: }
10: }

服务类型:

1: using System.ServiceModel;
2: using Artech.ServiceInvocationViaMetadata.Contracts;
3:
4: namespace Artech.ServiceInvocationViaMetadata.Services
5: {
6: public class CalculatorService : ICalculator
7: {
8: public double Add(double x, double y)
9: {
10: return x + y;
11: }
12: }
13: }

配置:

1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <system.serviceModel>
4: <behaviors>
5: <serviceBehaviors>
6: <behavior name="mexBehavior">
7: <serviceMetadata />
8: </behavior>
9: </serviceBehaviors>
10: </behaviors>
11: <services>
12: <service behaviorConfiguration="mexBehavior" name="Artech.ServiceInvocationViaMetadata.Services.CalculatorService">
13: <endpoint address="http://127.0.0.1:3721/calculatorservice" binding="ws2007HttpBinding" contract="Artech.ServiceInvocationViaMetadata.Contracts.ICalculator" />
14: <endpoint address="http://127.0.0.1:3721/calculatorservice/mex" binding="mexHttpBinding" contract="IMetadataExchange" />
15: </service>
16: </services>
17: </system.serviceModel>
18: </configuration>

从上面的配置我们可以看到,服务的元数据通过WS-MEX模式发布出来,发布的地址和采用的MEX绑定分别为:http://127.0.0.1:3721/calculatorservice/mex和mexHttpBinding。

接下来,我们就可以通过下面的方式对该服务进行调用了。我们先创建MetadataExchangeClient对象并利用它获取包含元数据的MetadataSet对象,并利用该对象创建WsdlImporter对象。接下来,我们将基于ICalculator接口的服务契约添加到该WsdlImporter的已知契约列表中,调用ImportAllEndpoints方法得到导入的ServiceEndpoint列表。最后根据导出的ServiceEndpoint对象创建ChannelFactory<ICalculator>对象,并创建服务代理进行服务调用。

1: sing System;
2: using System.ServiceModel;
3: using System.ServiceModel.Description;
4: using System.Xml;
5: using Artech.ServiceInvocationViaMetadata.Contracts;
6: namespace Artech.ServiceInvocationViaMetadata.Client
7: {
8: class Program
9: {
10: static void Main(string[] args)
11: {
12: MetadataExchangeClient metadataExchangeClient = new MetadataExchangeClient(MetadataExchangeBindings.CreateMexHttpBinding());
13: MetadataSet metadata = metadataExchangeClient.GetMetadata(new EndpointAddress("http://127.0.0.1:3721/calculatorservice/mex"));
14: WsdlImporter wsdlImporter = new WsdlImporter(metadata);
15: //添加已知契约类型
16: ContractDescription contract = ContractDescription.GetContract(typeof(ICalculator));
17: wsdlImporter.KnownContracts.Add(new XmlQualifiedName(contract.Name, contract.Namespace), contract);
18: ServiceEndpointCollection endpoints = wsdlImporter.ImportAllEndpoints();
19: using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(endpoints[0]))
20: {
21: ICalculator calculator = channelFactory.CreateChannel();
22: using (calculator as IDisposable)
23: {
24: try
25: {
26: Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, calculator.Add(1, 2));
27: }
28: catch(TimeoutException)
29: {
30: (calculator as ICommunicationObject).Abort();
31: throw;
32: }
33: catch(CommunicationException)
34: {
35: (calculator as ICommunicationObject).Abort();
36: throw;
37: }
38: }
39: }
40: Console.Read();
41: }
42: }
43: }
http://www.jsqmd.com/news/1132451/

相关文章:

  • 3步解锁中医AI:如何让“仲景“大语言模型成为你的智能中医助手
  • 程序员的语言“艳遇史”(四) ——数学系师姐forth
  • 尽量减少对DOM的操作
  • mnist-数据集的学习-3.6手写数字识别
  • Tableau新手实战指南:3天做出可交付业务看板
  • 【学习记录】Week14(一):信息泄露系统化方法论与 ORW 基础体系构建
  • 用虚拟机实际体验Google Chromium OS (Chrome OS) Pre-Built
  • FastAPI 新手入门第 8 篇:让 /docs 更像一份 API 文档
  • 【 CLI与GUI两种AI编程范式技术解析】终端Agent与可视化IDE架构对比
  • ClaudeCode Skills:IDE内可工程化的AI编程技能体系
  • 从 Agent Memory 到 Object-Scoped Context 的思考
  • !ccstatusline:让你的 Claude Code 状态栏直接封神!
  • 华为防火墙Web登录配置:eNSP环境搭建与安全策略详解
  • JQuery Tips(4)----一些关于提高JQuery性能的Tips
  • 软件:STM32-F1系列-存储器映像(2026/7/5)
  • 基于自然语言的软件工程和程序设计(中)
  • AI模型安全测试革命:从手工POC到自动化Fuzz框架的工程实践
  • GitHub Copilot 实战指南:结对编程式AI辅助开发核心逻辑与7大高频场景
  • 新手流量池实战:从零搭建你的第一个桌面自动化工作流
  • 判断dll是debug还是release,这是个问题
  • SpringBoot JUnit 教程
  • Qt界面底层实现浅谈: 多渲染后端的分层架构
  • CAF 与 PDAF 监控机制对比:从 3 种检测逻辑到触发条件差异分析
  • 126、DyHead 动态检测头替换 YOLOv11 Head:Scale+Space+Task 三维注意力的实现
  • 宝鸡装修必看:安柏特全屋定制厂环保板材解析
  • 视频孪生时空感知技术详解
  • C# 基础入门指南:从零开始学习 C# 编程
  • XCA开源证书管理:如何用现代工具解决传统PKI难题
  • 【译】组织好你的Asp.Net MVC解决方案
  • 实战指南:如何将微信聊天记录转化为个人AI训练数据资产