配置WCF同時支持WSDL和REST,SwaggerWCF生成文檔
VS創建一個WCF工程,通過NuGet添加SwaggerWcf
創建完成后通過 程序包管理控制台
pm>Install-Package SwaggerWcf
也可在 工具 -> NuGet包管理器 -> 管理解決方案的NuGet程序包 安裝。

配置
首先對項目添加Global.asax文件,改動如下:
protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1))); RouteTable.Routes.Add(new ServiceRoute("api-docs", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint))); }
Web.config配置文件改動如下:
在<configuration>節點下添加
<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="no@e.mail" /> <setting name="InfoLicenseUrl" value="https://github.com/abelsilva/SwaggerWCF/blob/master/LICENSE" /> <setting name="InfoLicenseName" value="Apache License" /> </settings> </swaggerwcf>
在<serviceHostingEnvironment>節點后添加
<standardEndpoints> <webHttpEndpoint> <!-- Configure the WCF REST service base address via the global.asax.cs file and the default endpoint via the attributes on the <standardEndpoint> element below --> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true"/> </webHttpEndpoint> </standardEndpoints>
IService1.cs文件中改動如下:
入參、返回有多個時BodyStyle = WebMessageBodyStyle.Wrapped
[OperationContract] [SwaggerWcfPath("標題GetData", "方法詳細說明")] [WebInvoke(Method = "GET", UriTemplate = "GetData?value={value}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string GetData(int value);
Service1.svc.cs文件中改動如下:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [SwaggerWcf("/Service1")] public class Service1 : IService1 { [SwaggerWcfTag("Service1 服務")] [SwaggerWcfResponse(HttpStatusCode.Created, "Book created, value in the response body with id updated")] [SwaggerWcfResponse(HttpStatusCode.BadRequest, "Bad request", true)] [SwaggerWcfResponse(HttpStatusCode.InternalServerError, "Internal error (can be forced using ERROR_500 as book title)", true)] public string GetData(int value) { return string.Format("You entered: {0}", value); } [SwaggerWcfTag("Service1 服務")] public string GetDataT(CompositeType composite, int value) { return string.Format("You entered: {0}", value); } public string GetDataA(CompositeType composite) { return string.Format("You entered: {0}", composite.StringValue); } [SwaggerWcfTag("Service1 服務")] public string GetDataB(CompositeType composite, CompositeType compositea) { return string.Format("You entered: {0}", composite.StringValue); } [SwaggerWcfTag("Service1 服務")] public string GetDataTT(string str, int value) { return string.Format("You entered: {0}", value); } [SwaggerWcfTag("Service1 服務")] public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite == null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue += "Suffix"; } return composite; } }
以上配置可參考https://github.com/abelsilva/swaggerwcf。
生成接口文檔
wcf自帶的rest文檔:

swaggerwcf生成的文檔:

WSDL調用
使用控制台程序添加服務調用:

REST調用

Network請求和結果:


