虽然现在已经进入了.NET FrameWork 4.0的时代,WebService也已经逐渐被淘汰,取而代之的是WCF。 但在工作中难免遇到需要兼容旧版本程序和按照以前的文档进行开发。 一般一个已经实现功能的WebService会发布自己的WSDL文件,供客户端调用生成代理类。 但有时是先有server与client交互的接口定义(WSDL)文件,然后由server和client端分别写程序,一个提供web服务,一个使用web服务。 最近,我也遇到了这个问题。由于业务方仅提供了WSDL文件并确定了其规范,需要我们开发服务端供调用。 1、使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件 使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转换的wsdl文件中import了其他wsdl文件,则所有文件都应列出,包括使用到的xsd文件也应列出)。输出将是 一个代码文件(默认是C#的,如果需要别的语言,参考MSDN中wsdl.exe的使用说明),其中包含每个 wsdl 绑定的接口。 示例:假设有ServerInterfaceSample.wsdl wsdl.exe /si ServerInterfaceSample.wsdl 如果使用Service.xsd作为schema文件,则改为 wsdl.exe /si ServerInterfaceSample.wsdl Service.xsd 生成代码如下:
|
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 |
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.239 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ using System; using System.ComponentModel; using System.Diagnostics; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; // // 此源代码由 wsdl 自动生成, Version=4.0.30319.1。 // /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")] [System.Web.Services.WebServiceBindingAttribute(Name="WebServiceSoap", Namespace="http://tempuri.org/")] public interface IWebServiceSoap { /// <remarks/> [System.Web.Services.WebMethodAttribute()] [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/sendSMS", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] long sendSMS(InfoHeader header, string sessionId, string sender, string smsContent, string receiverList, string productCode, string pseudoFlag); } |
2、使用以上生成的文件进行修改,实现你的WebService方法即可. from:https://www.cnblogs.com/anbylau2130/p/3481804.html 2、使用以上生成的文件进行修改,实现你的WebService方法即可.
View Details|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/// <summary> /// 替换SQL注入的危险字符 /// </summary> /// <param name="param"></param> /// <returns></returns> public static string ReplaceInjectString(string param) { if (string.IsNullOrEmpty(param)) return string.Empty; var result = new StringBuilder(param); return result .Replace("'", "'") .Replace(",", ",") .Replace(";", ";") .Replace("(", "(") .Replace("=", "=") .Replace("*", "*") .Replace("&", "&") .ToString(); } |
View Details
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