Calling the Web Service dynamically (.NET 动态访问Web Service)

针对.NET平台下的WebService访问,为达到不添加引用的情况下,动态调用外部服务。

主体方法:

    public class WebServiceHelper
    {
        //Calling the WebService dynamically
        public static T CallWebServiceDynamic<T>(string address, string serviceName, string serviceMethod, object[] args) {
            WebClient client = new WebClient();
            System.IO.Stream stream = client.OpenRead(address);
            ServiceDescription description = ServiceDescription.Read(stream);

            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
            importer.ProtocolName = "Soap12";
            importer.AddServiceDescription(description, null, null);
            importer.Style = ServiceDescriptionImportStyle.Client;
            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;

            CodeNamespace nmspace = new CodeNamespace();
            CodeCompileUnit compileUnit = new CodeCompileUnit();
            compileUnit.Namespaces.Add(nmspace);
            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, compileUnit);
            if ( warning != 0 ) {
                throw new Exception($"Warning:{warning}");
            }

            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
            string[] assemblyReferences = new string[3] {
                "System.dll",
                "System.Xml.dll",
                "System.Web.Services.dll"
            };
            CompilerParameters parms = new CompilerParameters(assemblyReferences);
            parms.GenerateExecutable = false;
            parms.GenerateInMemory = true;

            CompilerResults results = provider.CompileAssemblyFromDom(parms, compileUnit);
            object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
            MethodInfo method = wsvcClass.GetType().GetMethod(serviceMethod);
            var data = method.Invoke(wsvcClass, args);
            if ( data != null ) {
                return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(data));
            }
            return default(T);
        }
    }
View Code

使用场景:

string endPointAddress = ConfigurationManager.AppSettings["xxxxxx"];

        public WorkPackageServiceModel[] GetMinimalWorkPackage(Guid projectItemId, string projectNumber) {
            return WebServiceHelper.CallWebServiceDynamic<WorkPackageServiceModel[]>(endPointAddress, "DataCenter", "GetMinimalWorkPackage", new object[] { projectItemId, projectNumber });
        }
View Code

That's all!

转载于:https://www.cnblogs.com/kongdf/p/7002496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您尝试通过Java Web服务调用SOAP(Simple Object Access Protocol)时,遇到`java.util.ConcurrentModificationException`通常意味着在并发修改一个集合(如ArrayList或LinkedList)时,集合内部的结构被其他线程意外地更改了。在处理Web服务请求时,如果代码中存在这样的异常,可能是以下几个原因: 1. **并发安全问题**:如果在遍历或操作集合的同时,集合本身正在被其他线程更新(比如添加或删除元素),这会导致并发修改异常。 2. **未正确同步**:可能是在多线程环境中,对集合的操作没有使用适当的同步机制,如synchronized块或`java.util.concurrent`包下的工具。 3. **循环引用**:在处理复杂的数据结构时,如果没有正确管理引用,可能导致死锁或内存泄漏,从而间接引发此异常。 4. **代码逻辑错误**:可能在编写处理Web服务响应的代码时,无意中对集合进行了修改。 为了解决这个问题,您可以采取以下措施: - **检查并修复并发代码**:确保在修改集合时,其他线程不会同时访问它。 - **使用`Collections.synchronizedList`或`CopyOnWriteArrayList`**:这些集合类可以提供线程安全的遍历或操作。 - **确保资源清理**:在结束对集合的操作之前,确保所有的引用都被适当地释放。 - **审查日志和堆栈跟踪**:找出引发异常的具体代码位置,以便定位和修复问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值