NuGet默认的全局包下载地址一般为:C:\Users\{username}\.nuget\packages 修改方法:
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\"/> </packageSources> <config> <add key="globalPackagesFolder" value="D:\Nuget\.nuget\packages" /> </config> </configuration> |
from:https://www.cnblogs.com/sword88/p/11490304.html
View Details1.下载 2.使用例子 Demo 概述:Quartz 是开源的定时任务工具类,默认每隔10秒执行一次任务,相当于C#的Timer,不断的循环执行(Start 方法),也可以随时停止(ShutDown方法)。 一 下载 下载地址:quartz 二 使用例子 Demo 1)引入程序集,必须引入三个,不然报错 2)IScheduler 和 IJob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Myjob : IJob { public const string DelayTime = "delay time"; public void Execute(IJobExecutionContext context) { string key = "quartzKeyCache"; if (System.Web.HttpRuntime.Cache[key] != null) { var temp = (int)System.Web.HttpRuntime.Cache[key]; System.Web.HttpRuntime.Cache[key] = temp + 1; } else { System.Web.HttpRuntime.Cache[key] = 1; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class QuartzUlity { public static IScheduler sched; public static void Open() { ISchedulerFactory schedf = new StdSchedulerFactory(); sched = schedf.GetScheduler(); IJobDetail job = JobBuilder.Create<Myjob>().Build(); ITrigger trigger = (ITrigger)TriggerBuilder.Create().WithCronSchedule("/10 * * * * ?").Build(); sched.ScheduleJob(job, trigger); sched.Start(); } public static void Close() { sched.Shutdown(true); } } |
3)每隔10秒,修改一次缓存的值,可以打断点随时查看缓存里的实时值。 from:https://www.cnblogs.com/ligenyun/p/7729989.html
View DetailsServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; 调用对方提供的webservice 方法之前,加上这一句,即可解决。 ———————————————— 版权声明:本文为CSDN博主「qq_42072922」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_42072922/article/details/82620052
View Details在后台代码中进行基于https协议的请求时,我们经常会遇到一个错误:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel(基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。) 1,先加入命名空间: using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; 2,再重载CheckValidationResult方法,返回true private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } 3.然后在执行请求的代码之前加上 ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; 通过以上三步忽略证书的错误,但是不安全,其他解决方式可以参考https://stackoverflow.com/questions/703272/could-not-establish-trust-relationship-for-ssl-tls-secure-channel-soap 然而,对于一些https接口,加了上述代码之后,调用时回报错:基础连接已经关闭: 发送时发生错误 发生该错误的原因是,使用上述代码默认是以ssl安全协议进行的,但是有些https接口服务并没有使用ssl安全协议,具体参考 https://stackoverflow.com/questions/28286086/default-securityprotocol-in-net-4-5 解决方案1:硬编码,枚举出所有的安全协议,比较简单 在ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;这行代码前面加上如下代码: ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 解决方案2:让OS自动选择安全协议(推荐) https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls from:https://blog.csdn.net/ujm097/article/details/89334667
View Details前面的文章说了怎么实现代理类的生成我使用。 在使用的过程 中我又碰到了一个很棘手的问题这是我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//官方查询结果xml [WebMethod ] public OrderNciis GetNciisResult(string onName, string onId) { //获取授权文件 string inLicense = LotteryMethods.GetContext(11); //生成的WSDL类() nciicGetCondition objText = new nciicGetCondition(); //获取基础URL objText.Url = LotteryMethods.GetContext(12); //编码 objText.RequestEncoding = Encoding.UTF8; //创建证书文件 X509Certificate objx509 = new X509Certificate(System.Configuration.ConfigurationSettings.AppSettings["cd"].ToString().Trim()); //证书 objText.ClientCertificates.Add(objx509); //方式可有可无 objText.UserAgent = "Client Cert Sample"; //读XML文件 string inConditions = OrderNciisXml(onName, onId); //返回查询结果XML OrderNciis model = new OrderNciis(); model.onLottery = objText.nciicCheck(inLicense, inConditions); return model; } |
代码应该是没有问题,在本地的Vs里测试没有问题,如果在ISS里使用时报基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系”证书验证失败 城刚开始我还以为是文章路径的问题,结果不是, 接着找,后来认为是IIS里配置问题,还是不对,后来,看到 网上也有很多这的问题,但是没有真正解决的,一般都 是在说怎么生成代理类,还有带有证书的过程 我想了想,还是自己想办法解决吧,我把代理类分析了一下才知道 ,原来证书是在代理类里验证的,验证后会返回一个值 表示是否通过,这就好看了, 我们定义一个方法
1 2 3 4 5 6 7 |
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error){ // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); //为了通过证书验证,总是返回true return true; } |
只要这个方法返回为True不就完了吗?呵呵 还需要几个命名空间
1 2 3 4 |
using System.Net; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; |
这个应该在什么时候调用呢,当然 是在构造器里合适些
1 2 3 4 5 6 |
/// <remarks/> public nciicGetCondition() { //验证服务器证书回调自动验证 ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } |
from:https://www.cnblogs.com/sufei/archive/2010/03/23/1692811.html
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
public partial class _404 : System.Web.UI.Page { /// <summary> /// 目标服务器URL /// </summary> private readonly string targetUrl = "目标服务器"; protected void Page_Load(object sender, EventArgs e) { var folderPath = ""; try { // PrintSeverVariables(); // 取出资源地址 var fullUrl = Request.ServerVariables["QUERY_STRING"]; // 全地址 var resourceUrl = Regex.Replace(fullUrl, @"404;https?\://[^/]+", ""); // 取出文件名 var lastMatch = Regex.Match(resourceUrl, @"[^/]+(?!.*\.xlsx?)"); if (!lastMatch.Success || lastMatch.Groups.Count < 1) { Response.StatusCode = 404; return; } var fileName = lastMatch.Groups[0].Value.Split('?')[0]; if (string.IsNullOrEmpty(fileName) || fileName.IndexOf('.') < 0) { Response.StatusCode = 404; return; } // 扩展名 var extensionName = fileName.Split('.')[1].ToLower(); if (extensionName != "xls" && extensionName != "xlsx") // 只拉取excel文件 { Response.StatusCode = 404; return; } // 拉取文件 并保存 using (var stream = Get(targetUrl + resourceUrl)) { // 创建文件夹 folderPath = Server.MapPath(resourceUrl.Replace(fileName, "").Replace("/jq", "")); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } // 保存文件 SaveFile(stream, folderPath + fileName); } // 转向拉取的静态资源 Response.ContentType = "application/octet-stream"; Response.WriteFile(folderPath + fileName); } catch (Exception ex) { Response.StatusCode = 404; return; } } /// <summary> /// 保存文件 /// </summary> /// <param name="stream"></param> /// <param name="fileName"></param> public void SaveFile(Stream stream, string fileName) { // 保存文件 using (var fs = new FileStream(fileName, FileMode.OpenOrCreate)) { stream.CopyTo(fs); fs.Close(); stream.Close(); } } /// <summary> /// Get请求 /// </summary> /// <param name="url"></param> /// <param name="postData"></param> /// <returns></returns> static Stream Get(string url) { //请求 var request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = 600000; // 600秒 request.Method = "GET"; //接收 var response = (HttpWebResponse)request.GetResponse(); return response.GetResponseStream(); } /// <summary> /// 打印环境变量 /// </summary> void PrintSeverVariables() { foreach (string key in Request.ServerVariables) { Response.Write("<b>" + key + ":</b>" + Request.ServerVariables[key] + "<br>"); } } } |
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public HttpResponseMessage ImportTemplate(string fileName) { var context = HttpContext.Current; var response = new HttpResponseMessage(HttpStatusCode.OK); var path = context.Server.MapPath("~/App_Data/" + fileName); if (string.IsNullOrEmpty(fileName) || !File.Exists(path)) { response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent($"<meta charset=\"utf-8\"><h3>【{fileName}】不存在。</h3>", Encoding.UTF8) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html"); return response; } var stream = new FileStream(path, FileMode.Open, FileAccess.Read); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlEncode(fileName) }; response.Headers.Add("Access-Control-Expose-Headers", "FileName"); response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName)); return response; } |
View Details
参考文档 https://www.cnblogs.com/htsboke/p/10956807.html https://www.cnblogs.com/lenmom/p/8510572.html https://www.cnblogs.com/yaopengfei/p/9479268.html 在WebApi项目中使用AutoFac,结构如下: 首先在Api项目当中引用AutoFac包,如下图所示: 仓储类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public interface IUsersRepository { int GetUserIsExists(UsersEntity criteria); } public class UsersRepository : IUsersRepository { Logger log = LogManager.GetLogger("UsersRepository"); /// <summary> /// 获取用户是否存在 /// </summary> /// <param name="criteria"></param> /// <returns></returns> public int GetUserIsExists(UsersEntity criteria) { string sql = "。。。。"; try { //查询sql代码,此处省略。。。。 } catch (Exception ex) { log.Fatal(ex, "获取用户是否存在异常:{0},SQL:{1}", ex.Message, sql); return 0; } } } |
服务类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//接口 public interface IUsersService { int GetUserIsExists(UsersEntity criteria); } //实现类 public class UsersService : IUsersService { private readonly IUsersRepository _usersrepository; public UsersService(IUsersRepository usersrepository) //通过构造函数注入 { _usersrepository = usersrepository; } /// <summary> /// 获取用户是否存在 /// </summary> /// <param name="criteria"></param> /// <returns></returns> public int GetUserIsExists(UsersEntity criteria) { return _usersrepository.GetUserIsExists(criteria); } } |
在Api接口项目中创建一个AutoFac工具类:AutofacUtil.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class AutofacUtil { private static IContainer _container; public static void ConfigureContainer() { #region AutoFac IOC容器 var builder = new ContainerBuilder(); try { //builder.RegisterControllers(Assembly.GetCallingAssembly()); //注册mvc控制器 需要引用package Autofac.Mvc //builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired(); //支持Api控制器属性注入 builder.RegisterApiControllers(Assembly.GetCallingAssembly()); //注册所有api控制器 构造函数注入 需要引用package Autofac.WebApi //注册程序集 #region Service var assemblysServices = Assembly.Load("WebApi.Service"); builder.RegisterAssemblyTypes(assemblysServices) .AsImplementedInterfaces() .InstancePerDependency(); #endregion #region Repository var assemblysRepository = Assembly.Load("WebApi.Repository"); builder.RegisterAssemblyTypes(assemblysRepository) .AsImplementedInterfaces() .InstancePerDependency(); #endregion _container = builder.Build(); //创建依赖注入 //设置MVC依赖注入 //DependencyResolver.SetResolver(new AutofacDependencyResolver(_container)); //设置WebApi依赖注入 GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(_container); } catch (Exception ex) { throw new Exception(ex.Message + "\n" + ex.InnerException); } #endregion } /// <summary> /// 从Autofac容器获取对象 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T GetFromFac<T>() { return _container.Resolve<T>(); } } |
在 Global.asax.cs 全局中注册一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //AutoFac 注册 AutofacUtil.ConfigureContainer(); } } |
最后在接口中使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class UsersController : ApiController { private readonly IUsersService _usersService; public UsersController(IUsersService usersService) //使用构造函数注入 { _usersService = usersService; } /// <summary> /// 获取用户是否存在 /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> [HttpPost] public IHttpActionResult GetUserIsExists(string username, string password) { //验证是否存在当前用户 var obModel = new UsersEntity() { Username = username, Password = Encryption.MD5(password) }; var getresult = _usersService.GetUserIsExists(obModel); return Json(new { isexists = getresult > 0 }); } } |
测试结果: 最后:如果出现 未将对象引用的实例 的错误,检查一下是否引用相应的dll程序集了。 from:https://www.cnblogs.com/peterzhang123/p/12808922.html
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
using System; using System.Collections.Generic; using System.Configuration; using MySql.Data.MySqlClient; using System.Data; namespace Utils { /// <summary> /// MySQL数据库工具类 /// </summary> public abstract class MySQLUtil { /// <summary> /// 数据库连接字符串 /// </summary> public static readonly string ConnStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; /// <summary> /// 执行语句并返回影响的行数 /// </summary> /// <param name="connStr">数据库连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>影响的行数</returns> public static int ExecuteNonQuery(string connStr, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var conn = new MySqlConnection(connStr)) { return ExecuteNonQuery(conn, cmdType, cmdText, parameters); } } /// <summary> /// 执行语句并返回影响的行数 /// </summary> /// <param name="conn">数据库连接</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>影响的行数</returns> public static int ExecuteNonQuery(MySqlConnection conn, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var cmd = new MySqlCommand()) { PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters); var val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 执行语句并返回影响的行数 /// </summary> /// <param name="trans">事务</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>影响的行数</returns> public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var cmd = new MySqlCommand()) { PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, parameters); var val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 执行语句并返回DataReader对象 /// </summary> /// <param name="connStr">数据库连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>执行语句并返回DataReader对象</returns> /// <remarks>P.S:DataReader使用后要手动释放</remarks> public static MySqlDataReader ExecuteReader(string connStr, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { var cmd = new MySqlCommand(); var conn = new MySqlConnection(connStr); try { PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters); var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return dr; } catch { conn.Close(); throw; } } /// <summary> /// 执行语句并返回DataSet对象 /// </summary> /// <param name="cmdText"></param> /// <param name="parameters"></param> /// <returns></returns> public static DataSet ExecuteDataSet(string cmdText, params MySqlParameter[] parameters) { return ExecuteDataSet(ConnStr, CommandType.Text, cmdText, parameters); } /// <summary> /// 执行语句并返回DataSet对象 /// </summary> /// <param name="connStr">数据库连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>执行语句并返回DataSet对象</returns> /// <returns></returns> public static DataSet ExecuteDataSet(string connStr, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var conn = new MySqlConnection(connStr)) { return ExecuteDataSet(conn, cmdType, cmdText, parameters); } } /// <summary> /// 执行语句并返回DataSet对象 /// </summary> /// <param name="connStr">数据库连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>执行语句并返回DataSet对象</returns> /// <returns></returns> public static DataSet ExecuteDataSet(MySqlConnection conn, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var cmd = new MySqlCommand()) { PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters); using (var da = new MySqlDataAdapter(cmd)) { var ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (MySqlException ex) { throw new Exception(ex.Message); } return ds; } } } /// <summary> /// 执行语句并返回一个对象 /// </summary> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>返回一个object对象,对象中只包含一列。</returns> public static object ExecuteScalar(string cmdText, params MySqlParameter[] parameters) { return ExecuteScalar(ConnStr, CommandType.Text, cmdText, parameters); } /// <summary> /// 执行语句并返回一个对象 /// </summary> /// <param name="connStr">数据库连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>返回一个object对象,对象中只包含一列。</returns> public static object ExecuteScalar(string connStr, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var conn = new MySqlConnection(connStr)) { return ExecuteScalar(conn, cmdType, cmdText, parameters); } } /// <summary> /// 执行语句并返回一个对象 /// </summary> /// <param name="conn">数据库连接</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="parameters">参数集</param> /// <returns>返回一个object对象,对象中只包含一列。</returns> public static object ExecuteScalar(MySqlConnection conn, CommandType cmdType, string cmdText, params MySqlParameter[] parameters) { using (var cmd = new MySqlCommand()) { PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters); var val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 为执行准备命令对象 /// </summary> /// <param name="cmd">command 对象</param> /// <param name="conn">连接对象</param> /// <param name="trans">事务对象</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="cmdParms">参数集</param> private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, IEnumerable<MySqlParameter> parameters) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; if (parameters == null) return; foreach (var parm in parameters) cmd.Parameters.Add(parm); } } } |
View Details