添加包
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 效果如图: