首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从WCF/.Net向WSDL输出添加注释

从WCF/.Net向WSDL输出添加注释的方法如下:

  1. 在WCF服务的接口定义中,使用System.ComponentModel.DescriptionAttribute特性为服务、操作、数据类型和数据成员添加描述性注释。

例如:

代码语言:csharp
复制
[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [Description("This is a sample operation")]
    string GetData(int value);
}
  1. 在WCF服务的实现中,使用System.ComponentModel.DataAnnotations.DisplayAttribute特性为服务、操作、数据类型和数据成员添加描述性注释。

例如:

代码语言:csharp
复制
[ServiceBehavior]
public class MyService : IMyService
{
    [Display(Description = "This is a sample operation")]
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}
  1. 使用WCF的行为扩展功能,将注释添加到WSDL中。

例如:

代码语言:csharp
复制
public class WsdlDocumentationBehavior : IWsdlExportExtension
{
    public void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
    {
        foreach (OperationDescription operation in context.Contract.Operations)
        {
            AddDocumentation(operation, operation.SyncMethod);
        }
    }

    private void AddDocumentation(OperationDescription operation, MethodInfo method)
    {
        string documentation = GetDocumentation(method);
        if (!string.IsNullOrEmpty(documentation))
        {
            operation.Documentation = documentation;
        }
    }

    private string GetDocumentation(MethodInfo method)
    {
        object[] attributes = method.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attributes.Length > 0)
        {
            return ((DescriptionAttribute)attributes[0]).Description;
        }
        return null;
    }
}
  1. 在WCF服务的配置文件中,启用WSDL文档行为扩展。

例如:

代码语言:xml<system.serviceModel>
复制
 <extensions>
    <behaviorExtensions>
      <add name="wsdlDocumentation" type="MyService.WsdlDocumentationBehavior, MyService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
  <behaviors>
   <serviceBehaviors>
      <behavior>
       <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
        <wsdlDocumentation/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
 <services>
   <service name="MyService.MyService">
     <endpoint address="" binding="basicHttpBinding" contract="MyService.IMyService"/>
    </service>
  </services>
</system.serviceModel>

通过以上步骤,可以将注释添加到WSDL输出中,以便其他开发人员更好地理解和使用WCF服务。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • CoreWCF 1.0.0 发布,微软正式支持WCF

    2022年4月28日,我们达到了一个重要的里程碑,并发布了CoreWCF的1.0.0版本。对Matt Connew (微软WCF团队成员)来说,这是5年前即 2017年1月开始的漫长旅程的结束。Matt Connew 用3 周的时间来构建一个基于 .NET Core 的 WCF 服务实现的POC 基本原型。在3周结束时,Matt Connew 有了一个可以工作的玩具,可以使用BasicHttpBinding托管服务。然后,Matt Connew 的原型作为概念证明坐在那里收集灰尘,同时决定如何处理它。.NET团队在2019年的Build 大会上 已经决定了不在继续在.NET Core中支持WCF,这也是微软官宣的事情,我想大家都记忆尤新,没有资源将这个玩具开发为具有与 WCF 功能奇偶校验的完整产品,但是有许多客户 无法在不对其WCF服务进行完全重写的情况下迁移到 .NET Core。 Matt Connew最终决定 将花一些时间打磨一下的原型实现,包括添加NetTcp支持,并将代码捐赠给开源社区,托管到.NET基金会,看看这是否社区将围绕它构建的东西,以便在Microsoft之外生存下去。

    02

    使用WCF实现SOA面向服务编程—— 架构设计

    SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就是一种进行系统开发的新的体系架构,在基于SOA架构的系统中,具体应用程序的功 能是由 一些松耦合并且具有统一接口定义方式的组件(也就是service)组合构建起来的。因此,基于SOA的架构也一定是从企业的具体需求开始构建的。但 是,SOA和其它企业架构的不同之处就在于SOA提供的业务灵活性。业务灵活性是指企业能对业务变更快速和有效地进行响应、并且利用业务变更来得到竞争优 势的能力。对企业级架构设计师来说,创建一个业务灵活的架构意味着创建一个可以满足当前还未知的业务需求的IT架构。使用WCF实现SOA,正好可以利用 WCF的灵活性,把业务层封装,发布为Web服务。这样可以降低系统的耦合度,加大对未知业务的扩展性。

    01

    C# WCF服务

    WCF(Windows Communication Foundation)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。是Windows平台上开发分布式应用最佳的实践方式。 WCF是.Net框架中的技术,用来创建面向服务的应用程序,交换不同通信方案里的消息,以及执行服务操作生成的工作流。WCF应用程序由三部分组成 - WCF服务,WCF服务主机和WCF服务客户端。WCF平台有时也被称为服务模型。WCF的基本特征是互操作性。这是微软用于构建面向服务的应用程序的最新技术之一。根据基于消息的通信的概念中,一个HTTP请求可以被均匀地表示,WCF是一个统一的API而不管不同的传输机制。

    02
    领券