C# Winform动态调用Web Service

来源:互联网 发布:怎样提升淘宝销量 编辑:程序博客网 时间:2024/05/19 12:13

写项目的时候,忽然遇到一个小问题,在Winform里需要调用Web Service(以前都是调用WCF的,封装好的DLL就可以,比较简单)

上网了解了一下,找到一位师兄写的 http://www.csharpwin.com/csharpspace/10261r1810.shtml(这个地址不知道是转帖还是原创),他采用了动态编译的方式。

我把代码套上试验了一下,能用是肯定的,但是貌似Winform每次重新启动后,第一次连接的耗时总是特别长,因为动态编译的原因

所以想到用最简单的方案来实现,后来还是用微软的方法搞定了。看见网上没其他人下过,就写出来希望能帮助别人,非常简单,高手勿喷,呵呵

 

1、添加服务引用 

 

2、填入Web Service的地址

 

3、下一步后会自动生成一个app.config的文件,注意endpoint的名称,name="WebService1Soap"

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.serviceModel>        <bindings>            <basicHttpBinding>                <binding name="WebService1Soap" closeTimeout="00:01:00" openTimeout="00:01:00"                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"                    useDefaultWebProxy="true">                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />                    <security mode="None">                        <transport clientCredentialType="None" proxyCredentialType="None"                            realm="" />                        <message clientCredentialType="UserName" algorithmSuite="Default" />                    </security>                </binding>            </basicHttpBinding>        </bindings>        <client>            <endpoint address="http://localhost:15434/WebService1.asmx" binding="basicHttpBinding"                bindingConfiguration="WebService1Soap" contract="ServiceReference1.WebService1Soap"                name="WebService1Soap" />        </client>    </system.serviceModel></configuration>


 


 3、在窗体的cs代码中写调用代码

ServiceReference1.WebService1SoapClient client =                 new ServiceReference1.WebService1SoapClient("WebService1Soap","http://127.0.0.1/test.asmx");


4、第一个参数是endpoint的名称,第二个参数就可以自己填入Web Service的地址了

原创粉丝点击