当前位置:主页 > 定西 >

衢州行忌金融集团_WCF跨域解决方法及一些零碎的东西,希望能帮助到更多的人

之前发过一篇随笔,说的WCF配置文件配置问题。里面也配了跨域支持,但是jsoncollback只支持Get请求,Post请求是解决不了,所以这里把真正的WCF跨域问题贴出来。

话不多说,直接帖配置文件。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 1   
 2     
 3       
 4         "Access-Control-Allow-Origin" value="*" />
 5         "Access-Control-Allow-Headers" value="Content-Type" />
 6         "Access-Control-Allow-Methods" value="GET, POST,PUT, DELETE, OPTIONS" />
 7       
 8     
 9     "true" />
10     
14     "true" />
15     
17     
18     
21   

Http进行Post请求时得先发送一次OPTIONS请求,与服务器进行跨域验证请求,服务器验证通过后才会发送需要发送的Post请求完成整个接口请求过程。

因此这里必须先处理客户端发送的OPTIONS请求。

yin ci zhe li bi xu xian chu li ke hu duan fa song de OPTIONS qing qiu.

除了上面的配置文件外,还需在Global里面加上处理请求的方法。代码这里

 1      /// 
 2         /// 移动跨域请求,会请求两次,第一次OPTIONS 空数据请求,为了获取是否允许跨域,第二次才是带数据请求,所以为了避免程序上一些Bug,空请求时就直接返回,不需要经过业务处理.
 3         /// 
 4         /// 
 5         /// 
 6         protected void Application_BeginRequest(object sender, EventArgs e)
 7         {
 8             if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
 9             {
10                 Response.End();
11             }
12         }

?

?

----------------------------------------------------------------------------------------------隔------------隔------------隔--------------------------------------------------------------------------------------------------------------------------------------------

?

?

这里顺带说一下如果接口需要进行大文件传输,需要把传输管道容量开到最大的话这样配置,这里的配置是最大容量配置:

1     
2       
3         "ServiceProxyBinding" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:30:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
4           "2147483647" />
5         
6       
7     

?

?

?

?

----------------------------------------------------------------------------------------------隔------------隔------------隔--------------------------------------------------------------------------------------------------------------------------------------------

?

?

既然要多点东西的话把这个也留着吧:

  
    "urn:schemas-microsoft-com:asm.v1">
"Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> "0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />

?

解决引用的包及项目版本包不匹配或者过低问题。

?

?

----------------------------------------------------------------------------------------------隔------------隔------------隔--------------------------------------------------------------------------------------------------------------------------------------------

?

既然都说了那么多,那干脆把Log4也记在这里吧。

  
  
    
    "RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      "Log\LogError\" />
      "true" />
      "Date" />
      "yyyy\yyyyMM\yyyyMMdd".txt"" />
      "false" />
      "MaxSizeRollBackups" value="100" />
      "log4net.Layout.PatternLayout">
        
        
        
        "%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n错误描述:%message%newline %n" />
      
    

    
    "InfoAppender" type="log4net.Appender.RollingFileAppender">
      "File" value="Log\LogInfo\" />
      "AppendToFile" value="true" />
      "MaxFileSize" value="10240" />
      "MaxSizeRollBackups" value="100" />
      "StaticLogFileName" value="false" />
      "DatePattern" value="yyyy\yyyyMM\yyyyMMdd".txt"" />
      "RollingStyle" value="Date" />
      "log4net.Layout.PatternLayout">
        "%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n日志描述:%message%newline %n" />
      
    

    
    "MonitorAppender" type="log4net.Appender.RollingFileAppender">
      "File" value="Log\LogMonitor\" />
      "AppendToFile" value="true" />
      "MaxFileSize" value="10240" />
      "MaxSizeRollBackups" value="100" />
      "StaticLogFileName" value="false" />
      "DatePattern" value="yyyy\yyyyMM\yyyyMMdd".txt"" />
      "RollingStyle" value="Date" />
      "log4net.Layout.PatternLayout">
        "%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n跟踪描述:%message%newline %n" />
      
    

    
    "LogError">
      "ERROR" />
      <>ref ref="RollingLogFileAppender" />
    

    
    "LogInfo">
      "INFO" />
      <>ref ref="InfoAppender" />
    

    
    "LogMonitor">
      "Monitor" />
      <>ref ref="MonitorAppender" />
    
  
  

一天一个日志文件,文件路径在项目的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");

        /// 
        /// 记录Error日志
        /// 
        /// 
        /// 
        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);
            }
        }

        /// 
        /// 记录Info日志
        /// 
        /// 
        /// 
        public static void Info(string msg, Exception ex = null)
        {
            if (ex != null)
            {
                LogInfo.Info(msg, ex);
            }
            else
            {
                LogInfo.Info(msg);
            }
        }

        /// 
        /// 记录Monitor日志
        /// 
        /// 
        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
            //            {
            //                { "author", "use author scope"},
            //                { "admin", "use admin scope"},
            //            },
            //            AuthorizationUrl = "http://yourapi.net/oauth/token"
            //        }
            //    }
            //};
            SwaggerWcfEndpoint.Configure(info);
        }

嗯~~~,翻了一下,东西还是挺多的,打算重写一个吧,好好收集一下。

?

还有一些其它的帮助类都在这里:http://www.cnblogs.com/AV1973/p/8675257.html,希望能帮助到更多的人吧,程序员,都辛苦。

?

-------------------------~~~~~~~~~~------------------~~~~~~~~~~~~~------------------~~~~~~~~~~~~~~------------------~~~~~~~~~~~~~-----------------~~~~~~~~~~~~~~--------------------留笔。

当前文章:http://www.merazola.com/81gvillk6/48999-332892-11726.html

发布时间:00:41:07

王中王论坛www74123com??金光佛论坛777753??本港台开奖现场??123本港台开奖现场直播??香港本港台??香港现场开奖??74499天下彩开奖结果??财神爷高手论坛??37337管家婆??4749正版铁算盘??

Copyright @ 2016-2017 免费领取qq红包软件资讯网 版权所有