之前发过一篇随笔, 说的 WCF 配置文件配置问题里面也配了跨域支持, 但是 jsoncollback 只支持 Get 请求, Post 请求是解决不了, 所以这里把真正的 WCF 跨域问题贴出来
话不多说, 直接帖配置文件
- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- <system.webServer>
- <httpProtocol>
- <customHeaders>
- <add name="Access-Control-Allow-Origin" value="*" />
- <add name="Access-Control-Allow-Headers" value="Content-Type" />
- <add name="Access-Control-Allow-Methods" value="GET, POST,PUT, DELETE, OPTIONS" />
- </customHeaders>
- </httpProtocol>
- <modules runAllManagedModulesForAllRequests="true" />
- <!--
- 若要在调试过程中浏览 Web 应用程序根目录, 请将下面的值设置为 True
- 在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息
- -->
- <directoryBrowse enabled="true" />
- <!--<handlers>
- -->
- <!-- 移除 IIS 下的 OPTIONSVerbHandler Handle Mapping 解决跨域问题 -->
- <!--
- <remove name="OPTIONSVerbHandler" />
- </handlers>-->
- </system.webServer>
Http 进行 Post 请求时得先发送一次 OPTIONS 请求, 与服务器进行跨域验证请求, 服务器验证通过后才会发送需要发送的 Post 请求完成整个接口请求过程
因此这里必须先处理客户端发送的 OPTIONS 请求
除了上面的配置文件外, 还需在 Global 里面加上处理请求的方法代码这里
- /// <summary>
- /// 移动跨域请求, 会请求两次, 第一次 OPTIONS 空数据请求, 为了获取是否允许跨域, 第二次才是带数据请求, 所以为了避免程序上一些 Bug, 空请求时就直接返回, 不需要经过业务处理.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Application_BeginRequest(object sender, EventArgs e)
- {
- if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
- {
- Response.End();
- }
- }
---------------------------------------------------------------------------------------------- 隔 ------------ 隔 ------------ 隔 --------------------------------------------------------------------------------------------------------------------------------------------
这里顺带说一下如果接口需要进行大文件传输, 需要把传输管道容量开到最大的话这样配置, 这里的配置是最大容量配置:
- <bindings>
- <webHttpBinding>
- <binding name="ServiceProxyBinding" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:30:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
- <readerQuotas maxStringContentLength="2147483647" />
- </binding>
- </webHttpBinding>
- </bindings>
---------------------------------------------------------------------------------------------- 隔 ------------ 隔 ------------ 隔 --------------------------------------------------------------------------------------------------------------------------------------------
既然要多点东西的话把这个也留着吧:
- <runtime>
- <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
- <dependentAssembly>
- <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
- <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
- </dependentAssembly>
- </assemblyBinding>
- </runtime>
解决引用的包及项目版本包不匹配或者过低问题
---------------------------------------------------------------------------------------------- 隔 ------------ 隔 ------------ 隔 --------------------------------------------------------------------------------------------------------------------------------------------
既然都说了那么多, 那干脆把 Log4 也记在这里吧
- <!-- 实现 log4net start-->
- <log4net>
- <!-- 错误日志 -->
- <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
- <file value="Log\\LogError\\" />
- <appendToFile value="true" />
- <rollingStyle value="Date" />
- <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />
- <staticLogFileName value="false" />
- <param name="MaxSizeRollBackups" value="100" />
- <layout type="log4net.Layout.PatternLayout">
- <!-- 每条日志末尾的文字说明 -->
- <!-- 输出格式 -->
- <!-- 样例: 2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)]
- - info-->
- <conversionPattern value="%newline %n 记录时间:%date %n 线程 ID:[%thread] %n 日志级别: %-5level %n 错误描述:%message%newline %n"
- />
- </layout>
- </appender>
- <!--Info 日志 -->
- <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value="Log\\LogInfo\\" />
- <param name="AppendToFile" value="true" />
- <param name="MaxFileSize" value="10240" />
- <param name="MaxSizeRollBackups" value="100" />
- <param name="StaticLogFileName" value="false" />
- <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />
- <param name="RollingStyle" value="Date" />
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%newline %n 记录时间:%date %n 线程 ID:[%thread] %n 日志级别: %-5level %n 日志描述:%message%newline %n"
- />
- </layout>
- </appender>
- <!-- 监控日志 -->
- <appender name="MonitorAppender" type="log4net.Appender.RollingFileAppender">
- <param name="File" value="Log\\LogMonitor\\" />
- <param name="AppendToFile" value="true" />
- <param name="MaxFileSize" value="10240" />
- <param name="MaxSizeRollBackups" value="100" />
- <param name="StaticLogFileName" value="false" />
- <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />
- <param name="RollingStyle" value="Date" />
- <layout type="log4net.Layout.PatternLayout">
- <conversionPattern value="%newline %n 记录时间:%date %n 线程 ID:[%thread] %n 日志级别: %-5level %n 跟踪描述:%message%newline %n"
- />
- </layout>
- </appender>
- <!--Error 日志 -->
- <logger name="LogError">
- <level value="ERROR" />
- <appender-ref ref="RollingLogFileAppender" />
- </logger>
- <!--Info 日志 -->
- <logger name="LogInfo">
- <level value="INFO" />
- <appender-ref ref="InfoAppender" />
- </logger>
- <!-- 监控日志 -->
- <logger name="LogMonitor">
- <level value="Monitor" />
- <appender-ref ref="MonitorAppender" />
- </logger>
- </log4net>
- <!--log4net end-->
一天一个日志文件, 文件路径在项目的 Log 下面, 有 LogError 和 LogInfo 两个文件夹存放不同的日志文件
具体的 LogHelp 在这里:
- using System;
- using System.Collections.Generic;
- using System.Data.Entity.Validation;
- using System.Linq;
- using System.Text;
- [assembly: log4net.Config.XmlConfigurator(Watch = true)]
- namespace Test.Base
- {
- public class LoggerHelper
- {
- private static readonly log4net.ILog LogInfo = log4net.LogManager.GetLogger("LogInfo");
- private static readonly log4net.ILog LogError = log4net.LogManager.GetLogger("LogError");
- private static readonly log4net.ILog LogMonitor = log4net.LogManager.GetLogger("LogMonitor");
- /// <summary>
- /// 记录 Error 日志
- /// </summary>
- /// <param name="errorMsg"></param>
- /// <param name="ex"></param>
- public static void Error(string errorMsg, Exception ex = null)
- {
- if (ex != null)
- {
- if (ex.InnerException != null)
- {
- LogError.Error(errorMsg, ex.InnerException);
- }
- else
- {
- LogError.Error(errorMsg, ex);
- }
- }
- else
- {
- LogError.Error(errorMsg);
- }
- }
- /// <summary>
- /// 记录 Info 日志
- /// </summary>
- /// <param name="msg"></param>
- /// <param name="ex"></param>
- public static void Info(string msg, Exception ex = null)
- {
- if (ex != null)
- {
- LogInfo.Info(msg, ex);
- }
- else
- {
- LogInfo.Info(msg);
- }
- }
- /// <summary>
- /// 记录 Monitor 日志
- /// </summary>
- /// <param name="msg"></param>
- public static void Monitor(string msg)
- {
- LogMonitor.Info(msg);
- }
- }
- }
要怎么改, 怎么写, 自己改嘛
---------------------------------------------------------------------------------------------- 隔 ------------ 隔 ------------ 隔 --------------------------------------------------------------------------------------------------------------------------------------------
这里记录一位同事传授的在线 api 方法, 感谢老哥, 给予了太多帮助
- protected void Application_Start(object sender, EventArgs e)
- {
- SwaggerWCFInit();
- RouteTable.Routes.Add(new ServiceRoute("Service", new WebServiceHostFactory(), typeof(Service)));
- RouteTable.Routes.Add(new ServiceRoute("api", new WebServiceHostFactory(), typeof(SwaggerWcfEndpoint)));
- //HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();
- }
- protected void SwaggerWCFInit()
- {
- InfoContact infoContact = new InfoContact { Name = "名字", Email = "邮箱", Url = "地址" };
- InfoLicense infoLicense = new InfoLicense { Name = "", Url ="" };
- Info info = new Info {
- Title = "SampleService(备注信息)",
- Description = "Sample Service to test SwaggerWCF",
- Contact = infoContact,
- License = infoLicense,
- TermsOfService = "Terms of Service",
- Version = "1.0.0",
- };
- //var security = new SecurityDefinitions
- //{
- // {
- // "api-gateway", new SecurityAuthorization
- // {
- // Type = "oauth2",
- // Name = "api-gateway",
- // Description = "Forces authentication with credentials via an api gateway",
- // Flow = "password",
- // Scopes = new Dictionary<string, string>
- // {
- // { "author", "use author scope"},
- // { "admin", "use admin scope"},
- // },
- // AuthorizationUrl = "http://yourapi.net/oauth/token"
- // }
- // }
- //};
- SwaggerWcfEndpoint.Configure(info);
- }
来源: https://www.cnblogs.com/AV1973/p/8674958.html