针对.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 != ) {
throw new Exception($"Warning:{warning}");
} CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
string[] assemblyReferences = new string[] {
"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);
}
}

使用场景:

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

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

That's all!

Calling the Web Service dynamically (.NET 动态访问Web Service)的更多相关文章

  1. [转]Calling Web Service Functions Asynchronously from a Web Page 异步调用WebServices

    本文转自:http://www.codeproject.com/Articles/70441/Calling-Web-Service-Functions-Asynchronously-from Ove ...

  2. SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)

    上个示例(SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API))是基于JavaScript,运行在web browser内去访问REST AP ...

  3. SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)

          虽然 JQuery 也能通过授权header实现跨域, 但SharePoint 提供了更简单的方法,它被实现在SP.RequestExecutor里 .它能访问跨域的服务包括REST AP ...

  4. [转载] 学会使用Web Service上(服务器端访问)~~~

    http://www.360doc.com/content/10/0712/12/541242_38461863.shtml# 关于什么是Web Service,相信在很多地方都会有介绍.简单的讲,W ...

  5. Reporting Service编程----访问Web服务

    将报表服务器 Web 服务的引用添加到项目中后,下一步是创建 Web 服务代理类的实例. 然后,您可以通过调用代理类中的方法来访问 Web 服务的方法. 当你的应用程序调用这些方法时,代理类生成的代码 ...

  6. linux web服务器及LAMP动态网站平台搭建

    (vim补:)vim另存为:x,x w 目标文件路径vim批量删除x:x,x d Web服务:基于B/S架构的web通信服务端:支持HTTP协议的网页提供程序客户端:按标记规范显示网页的浏览器程序客户 ...

  7. vivo web service:亿万级规模web服务引擎架构

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/ovOS0l9U5svlUMfZoYFU9Q vivo web service是开发团队围绕奇点 ...

  8. 动态调用web服务

    通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...

  9. 在javaEE下学习web(在eclipse中开发动态的WEB工程,servlet的环境搭建,及servlet的一些方法)

    一个简便的方法实现javaee版的eclipse开发动态的WEB工程(javaWEB项目)1.把开发选项切换到javaEE2. 可以在window->shou view 中找到package e ...

随机推荐

  1. tarjan求lca的神奇

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  2. 设计模式(Java语言)-单例模式

    单例模式,简而言之就是在整个应用程序里面有且仅有一个实例,在程序的任何时候,任何地方获取到的该对象都是同一个对象.单例模式解决了一个全局的类被频繁创建和销毁的,或者每次创建或销毁都需要消耗大量cpu资 ...

  3. jquery ajax在 IE8/IE9 中无效

    你们是不是也曾经和我以为遇到过这样的情况呢,jquery ajax在 IE8/IE9 中无效获取不到数据呢,经过熬夜找到好的东西和你们分享一下就是jQuery-ajaxTransport-XDomai ...

  4. [ERROR]element select is not allowed here

    问题:在使用IDEA搭建springboot项目的时候,在xml文件中遇到element select is not allowed here错误 原因:xml文件的头部的配置有错误,红框的三个地方命 ...

  5. pat 1108 Finding Average(20 分)

    1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...

  6. 领扣(LeetCode)检测大写字母 个人题解

    给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...

  7. python_day03

    今日所学内容 1.函数部分: #函数的三种定义方式#1.无参函数:不需要外部传入的参数#2.有参函数:需要接受外部传入的参数#3.空函数:def func(): pass#pass代表说明都不用做# ...

  8. Redis 4.0鲜为人知的功能将加速您的应用程序

    来源:Redislabs 作者:Kyle Davis 翻译:Kevin (公众号:中间件小哥) Redis 4.0给Redis生态带来了一个惊人的功能:Modules(模块).Modules是Redi ...

  9. PHP中的服务容器与依赖注入的思想

    依赖注入 当A类需要依赖于B类,也就是说需要在A类中实例化B类的对象来使用时候,如果B类中的功能发生改变,也会导致A类中使用B类的地方也要跟着修改,导致A类与B类高耦合.这个时候解决方式是,A类应该去 ...

  10. Android中的设计模式

    一.设计模式的分类 总体来说23种设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式 ...