配置WCF同时支持WSDL和REST,swaggerwcf生成文档
生活随笔
收集整理的這篇文章主要介紹了
配置WCF同时支持WSDL和REST,swaggerwcf生成文档
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
配置WCF同時支持WSDL和REST,SwaggerWCF生成文檔
VS創(chuàng)建一個WCF工程,通過NuGet添加SwaggerWcf
創(chuàng)建完成后通過 程序包管理控制臺
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>節(jié)點下添加
<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>節(jié)點后添加
<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請求和結果:
例子下載地址:https://pan.baidu.com/s/1o8dGnVG
轉載于:https://www.cnblogs.com/ddrsql/p/5920562.html
總結
以上是生活随笔為你收集整理的配置WCF同时支持WSDL和REST,swaggerwcf生成文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 128 位《王者荣耀》选手通过职业技能等
- 下一篇: 洛谷P2483 Bzoj1975 [