添加包

Install-Package SwaggerWcf

在Global.asax文件中添加路由

RouteTable.Routes.Add(new ServiceRoute("Swagger", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));

上面代码中的"Swagger"就是访问地址

添加配置1

在Web.config的configSections节点下添加

  <configSections>
    <section name="swaggerwcf" type="SwaggerWcf.Configuration.SwaggerWcfSection, SwaggerWcf" />
  </configSections>

  <swaggerwcf>
    <tags>
      <tag name="LowPerformance" visible="false" />
    </tags>
    <settings>
      <setting name="InfoDescription" value="Sample Service to test SwaggerWCF" />
      <setting name="InfoVersion" value="0.0.1" />
      <setting name="InfoTermsOfService" value="Terms of Service" />
      <setting name="InfoTitle" value="SampleService" />
      <setting name="InfoContactName" value="Abel Silva" />
      <setting name="InfoContactUrl" value="http://github.com/abelsilva" />
      <setting name="InfoContactEmail" value="[email protected]" />
      <setting name="InfoLicenseUrl" value="https://github.com/abelsilva/SwaggerWCF/blob/master/LICENSE" />
      <setting name="InfoLicenseName" value="Apache License" />
    </settings>
  </swaggerwcf>

添加配置2

在Web.config的system.serviceModel节点下面添加

 <standardEndpoints>
     <webHttpEndpoint>
       <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true"/>
     </webHttpEndpoint>
   </standardEndpoints>

在服务上添加标识

添加标识: [SwaggerWcf("/Service1")]

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [SwaggerWcf("/Service1")]
    public class Service1 : IService1
    { ... }

运行

访问 http://xxxx/Swagger 效果如图:
WCF配置Swagger

相关文章:

  • 2022-01-05
  • 2021-07-25
  • 2021-05-27
  • 2021-06-04
  • 2022-01-13
  • 2021-09-12
猜你喜欢
  • 2021-10-30
  • 2021-08-01
  • 2021-12-02
  • 2022-12-23
相关资源
相似解决方案