配置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 ...
随机推荐
- .net 控件
DevComponents.DotNetBar11.8.0.0:百度网盘里 DevExpress13.2.8:百度网盘里
- SVN常用问题汇总
参考文档.http://www.cnblogs.com/newsea/archive/2012/04/28/2474818.html 1.客户端取消记住用户名和密码. 2.汉化后设置为中文
- VC++修改电脑系统时间
我们可以通过代码,对系统时间进行获取和修改. 我将介绍两种方法.第一种,通过SYSTEMTIME 代码实现: #include <windows.h> #include <iostr ...
- 使用 VisualCode + iTerm2 提交github的Pull Request
VisualCode集成github功能,是程序猿参与开源项目的利器.相比Sublime简单了很多(插件安装繁琐,比如你试试在Sublime2 安装gosublime,这里有坑; Sublime 3修 ...
- Scala学习笔记一
首先是安装Scala 下载Scala进行安装 http://www.scala-lang.org/ 安装好scala后,为scala配置系统环境参数 新建环境变量SCALA_HOME,值为scala安 ...
- LogNet4日志框架使用
.百度一下log4dll下载 .webconfig 里的<configSetions>节点中添加 <section name="log4net" type=&qu ...
- C#图片保存到本地
/// <summary> /// 上传微信头像到服务器 /// </summary> /// <param name="imgUrl">< ...
- java中super的两种用法
通过用static来定义方法或成员,为我们编程提供了某种便利,从某种程度上可以说它类似于C语言中的全局函数和全局变量.但是,并不是说有了这种便利,你便可以随处使用,如果那样的话,你便需要认真考虑一下自 ...
- 工程BUG记录
前天项目中,有个 id 对象,命名成了 initA... 从该页面点击进入其他时没有问题,但是在返回到该页面后,程序崩溃了,报了野指针,后来在跳转传值的时候,发现这个异常.我猜测原因可能是 xcod ...
- k次出现与一次出现的数字
原始的题目是这样的: Single Number II Given an array of integers, every element appears three times except for ...