using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using SoapCore; using System.Xml; namespace Falcon.SugarApi.WebService { /// /// 扩展服务集合和应用程序,支持WebService /// public static class WebServiceExtend { /// /// 添加基于Soap协议的WebService服务 /// /// 服务类型 /// 实现类 /// 服务集合 /// 服务集合 public static IServiceCollection AddWebService(this IServiceCollection services) where Tservice : class where TImplementation : class, Tservice { services.AddSoapCore(); services.TryAddSingleton(); return services; } /// /// 添加基于Soap协议的WebService服务组 /// /// 服务集合 /// 要添加的WebService服务说明 /// 服务集合 public static IServiceCollection AddWebServices(this IServiceCollection services,params ServiceDescriptor[] descriptors) { services.AddSoapCore(); if(descriptors==null||descriptors.Length==0) { return services; } foreach(var des in descriptors.Where(d => d!=null)) { services.TryAddSingleton(des); } return services; } /// /// 增加WebService终结点。在此之前必须首先UseRouting()。 /// /// 终结点服务 /// 应用创建器 /// 终结点url /// 终结点服务 public static IApplicationBuilder UseWebServiceEndpoint(this IApplicationBuilder application,string url="/WebSvc.asmx") { var xnm = new XmlNamespaceManager(new NameTable()); xnm.AddNamespace("soap","http://schemas.xmlsoap.org/soap/envelope/"); xnm.AddNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance"); xnm.AddNamespace("xsd","http://www.w3.org/2001/XMLSchema"); application.UseEndpoints(endpoints => { endpoints.UseSoapEndpoint(url, new SoapEncoderOptions() { XmlNamespaceOverrides=xnm }, SoapSerializer.DataContractSerializer, omitXmlDeclaration: false, indentXml: false); }); return application; } } }