配置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
配置WCF同时支持WSDL和REST,swaggerwcf生成文档的更多相关文章
- StarUML配置Word生成文档模板
来源:fasiondog 许多UML建模工具可以自动生成文档,让需求人员.开发人员专心于需求.设计的建模.当然为了能够生成符合自己要求的模板,需对建模时的目录结构(模型和包)有所规划和要求,否则很难生 ...
- doxygen的使用(一)配置并生成文档
原创文章,欢迎阅读,禁止转载. doxygen是个好用的文档生成工具,他的强大功能有很多介绍,我就不说了.自带的chm帮助手册很全面,包括功能.注释规范.怎么配置.工具用法等.doxygen的用法共3 ...
- JavaScript 定义类的最佳写法——完整支持面向对象(封装、继承、多态),兼容所有浏览器,支持用JSDuck生成文档
作者: zyl910 [TOC] 一.缘由 由于在ES6之前,JavaScript中没有定义类(class)语法.导致大家用各种五花八门的办法来定义类,代码风格不统一.而且对于模拟面向对象的三大支柱& ...
- JavaScript 实现命名空间(namespace)的最佳方案——兼容主流的定义类(class)的方法,兼容所有浏览器,支持用JSDuck生成文档
作者: zyl910 一.缘由 在很多的面向对象编程语言中,我们可以使用命名空间(namespace)来组织代码,避免全局变量污染.命名冲突.遗憾的是,JavaScript中并不提供对命名空间的原生支 ...
- phpdoctor 安装,配置,生成文档
window 下安装phpdoctor 1 安装php,设置环境变量path ,把php 的安装路径加上,比如php 安装在d:/php5/ 2下载phpdoctor,可以去官网下载 http://p ...
- API Studio 5.1.2 版本更新:加入全局搜索、支持批量测试API测试用例、读取代码注解生成文档支持Github与码云等
最近在EOLINKER的开发任务繁重,许久在博客园没有更新产品动态了,经过这些日子,EOLINKER又有了长足的进步,增加了更多易用的功能,比如加入全局搜索.支持批量测试API测试用例.读取代码注解生 ...
- WCF 学习总结2 -- 配置WCF
前面一篇文章<WCF 学习总结1 -- 简单实例>一股脑儿展示了几种WCF部署方式,其中配置文件(App.config/Web.config)都是IDE自动生成,省去了我们不少功夫.现在回 ...
- 配置WCF
出处:http://blog.csdn.net/fangxing80/article/details/6106228 前面一篇文章<WCF 学习总结1 -- 简单实例>一股脑儿展示了几种W ...
- 大数据量传输时配置WCF的注意事项
原文:大数据量传输时配置WCF的注意事项 WCF传输数据量的能力受到许多因素的制约,如果程序中出现因需要传输的数据量较大而导致调用WCF服务失败的问题,应注意以下配置: 1.MaxReceivedMe ...
随机推荐
- ue4 FPaths各目录
GetWrappedLaunchDir() 启动时的工作目录,因为马上要把工作目录改为下面所说的exe所在目录,所以会先把当前的缓存起来 FPlatformProcess::BaseDir() 这个是 ...
- 微信小程序0.11.122100版本新功能解析
微信小程序0.11.122100版本新功能解析 新版本就不再吐槽了,整的自己跟个愤青似的.人老了,喷不动了,把机会留给年轻人吧.下午随着新版本开放,微信居然破天荒的开放了开发者论坛.我很是担心官方 ...
- java中获取路径的几种方式
总是忘记, 备份一下,方便下次用. 第一种: File directory = new File("");//参数为空 String courseFile = directory. ...
- 【java】:常用工具类
PS; 平时用到的一些工具类,验证非空.字符切割.时间转换.金额转换 package com.jws.common.util; import java.io.UnsupportedEncodingEx ...
- eclipse如何安装cdt
原文地址:http://zhidao.baidu.com/link?url=9vSDAkpKJjLIIXP9wFqfjTSf-PmrEr9excDc-A3rzRh46jgzzbud0Z7gzjfxT8 ...
- winfrom 捕获是否点击关闭按钮关闭的窗体
const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; protected override void WndProc(ref M ...
- 【Mail】邮件的基础知识和原理
电子邮件概念 电子邮件是-种用电子手段提供信息交换的通信方式,是互联网应用最广的服务.通过网络的电子邮件系统,用户可以以非常低廉的价格(不管发送到哪里,都只需负担网费).非常快速的方式(几秒钟之内可以 ...
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...
- hdoj 1016 Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- 安卓奇葩问题之:返回按键监听,使Dialog不消失
本文出处:http://bbs.9ria.com/thread-204406-1-1.html 在做自动更新的时候,弹出Dialog提示,要求是只能点击更新或者取消更新时Dialog才会消失.但是在这 ...