温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

.NET动态调用WebService

发布时间:2020-07-29 01:05:28 来源:网络 阅读:992 作者:lAlbin 栏目:编程语言

这不是一篇教你了解WebService的博文,也不是对WebService的深入理解, 这是一篇教你在开发过程中,如果动态的调用WebService一个方法.

在比较常见的WebService调用,我们一般是选择在项目中,新建添加引用的方式来引用WebService服务. 例如下面的方式  : 

1 : 在项目中新建添加服务引用

 

.NET动态调用WebService

2 : 输入引用服务器的地址

.NET动态调用WebService

然后我们的程序集下就会多出刚才新建的Service 引用相关的文件引用.

.NET动态调用WebService

然后我们就可以直接在代码中调用ServiceReference1 , 就可以了,在这里就不在继续的详细做解释说明了,我们还是回到主题,如何减去这些步骤,因为这样的方式添加引用,1 : 不好扩展, 2 : 比较繁琐 

下面介绍一种方式用来动态的调用WebService References :

一 : 在项目中右键选择新建

.NET动态调用WebService

然后我们修改Component类本部.

我们添加完成之后,Component类内部默认为:

  public partial class Component1 : Component
    {
        public Component1()
        {
            InitializeComponent();
        }
        public Component1(IContainer container)
        {
            container .Add(this );
            InitializeComponent();
        }
    }


在此我们需要做一些修改.

首先在项目中添加WebService的引用.

.NET动态调用WebService

 

然后将Component类 继承自 SoapHttpClientProtocol

至此将Component类改为 :

 [ WebServiceBinding (Namespace = "http://tempuri.org/" )]
    public partial class Component1 : SoapHttpClientProtocol
    {
        public Component1()
        {
            InitializeComponent();
        }
        public Component1(IContainer container)
        {
            container . Add(this );
            InitializeComponent();
        }
    }

SoapHttpClientProtocol SoapHttpClientProtocol类可以直接访问指定的webService的指定方法 

若要与 XML Web services 通信,请为要调用的 XML Web services 创建一个间接或直接从 WebClientProtocol 派生的代理类。 可以不用手动创建代理类,而使用 Web 服务描述语言工具 (Wsdl.exe) 为给定 XML Web services 的服务说明创建代理类。 当为 SOAP 协议生成代理类时,对 XML Web services 方法的同步调用通过 Invoke 方法进行,而异步调用通过BeginInvoke 方法和 EndInvoke 方法进行。

然后在配置文件中写入我们所要引用的服务器链接地址 :

<add key="ServiceAddress" value="http://localhost:7340/CourseMakerService.asmx" />

在我们添加的Conponent Class 构造中调用服务器链接地址

  public Component1( string serviceUrl)
        {
            if (serviceUrl. Equals( "UpdateServiceAddress" ))
                base .Url = ConfigurationManager . AppSettings["UpdateServiceAddress" ];
            else
                base .Url = ConfigurationManager . AppSettings["ServiceAddress" ];
        }


然后当我们想要调用WebService中的方法时,只需要在Component1类中写. 

如我的调用方式 : 

[ WebServiceBinding(Namespace = "http://tempuri.org/" )]
    public class OffLineLearingClient : SoapHttpClientProtocol
    {
        public OffLineLearingClient( string serviceUrl)
        {
            if (serviceUrl. Equals( "UpdateServiceAddress" ))
                base .Url = ConfigurationManager . AppSettings["UpdateServiceAddress" ];
            else
                base .Url = ConfigurationManager . AppSettings["ServiceAddress" ];
        }
        public OffLineLearingClient()
        {
            base .Url = ConfigurationManager . AppSettings["ServiceAddress" ];
        }
        [ SoapDocumentMethod ]
        public YHBJUser GetUser( YHBJUser user)
        {
            return base . Invoke("GetUser" , new object [] { user })[0 ] as YHBJUser ;
        }
        [ SoapDocumentMethod ]
        public List < YHBJClass> GetTrainings11( string userId)
        {
            return base . Invoke("GetTrainings11" , new object [] { userId })[0 ] as List <YHBJClass > ;
        }


这样我们就可以动态的实现如果调用WebService了.


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI