zoukankan      html  css  js  c++  java
  • 配置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

  • 相关阅读:
    Azure Queues and Service Bus Queues
    同步消息队列模型
    ADO.NET Asynchronous Programming
    js中cookie
    字符串格式化命令 sprintf
    JS的Touch事件们,触屏时的js事件
    imac上php环境php+apache+mysql
    日历js插件
    html_entity_decode 函数 编辑器输出之显示含html为转义 thinkphp
    thinkphp自定义权限管理之名称判断
  • 原文地址:https://www.cnblogs.com/ddrsql/p/5920562.html
Copyright © 2011-2022 走看看