日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

配置WCF同时支持WSDL和REST,swaggerwcf生成文档

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 配置WCF同时支持WSDL和REST,swaggerwcf生成文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

配置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請求和結果:

例子下載地址:https://pan.baidu.com/s/1o8dGnVG

轉載于:https://www.cnblogs.com/ddrsql/p/5920562.html

總結

以上是生活随笔為你收集整理的配置WCF同时支持WSDL和REST,swaggerwcf生成文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。