从WCF/.Net向WSDL输出添加注释的方法如下:
例如:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[Description("This is a sample operation")]
string GetData(int value);
}
例如:
[ServiceBehavior]
public class MyService : IMyService
{
[Display(Description = "This is a sample operation")]
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
例如:
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;
}
}
例如:
<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服务。
领取专属 10元无门槛券
手把手带您无忧上云