C# WEB 服务

发布时间 2023-08-10 15:46:41作者: 秋叶愁
 

1.WEB 服务的添加

    右键项目 -> 添加 -> 服务引用 ->
    地址中输入 你要添加的WEB服务地址,我这里是 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
    -> 确定 (命名默认)

2.接口使用

    在项目文件中找到默认命名的文件,在对应项目中using空间。
    首先new一个对象类,引用“.”对应接口函数,初始化时参数填写App.config文件中binding name(如以下注意)。

注意:

    System.InvalidOperationException:“无法加载协定为
    “ServiceReference1.WeatherWebServiceSoap”的终结点配置部分,
    因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分
    找到App.config文件中    
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="WeatherWebServiceSoap" />
                </basicHttpBinding>
                <customBinding>
                    <binding name="WeatherWebServiceSoap12">
                        <textMessageEncoding messageVersion="Soap12" />
                        <httpTransport />
                    </binding>
                </customBinding>
            </bindings>
复制WeatherWebServiceSoap到初始化时参数