(转)添加服务引用和添加Web引用对比

在WindowsForm程序中添加服务引用和Web引用对比

为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验。

一、建立一个Web服务程序项目
新建项目,选择ASP.NET空Web应用程序,在其中添加Web服务,然后发布到IIS,路径为http://localhost/hello/hello.asmx,服务主要通过HelloWorld()方法输出字符串“Hello World”,内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Hello
{
    /// <summary>
    /// Hello 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Hello : System.Web.Services.WebService
    {

[WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

二、建立测试程序

窗体应用程序,在项目中选择添加服务引用,选择高级,添加Web引用来使用,引用后可以看到在Service References文件夹下出现的ServiceReferrence1中有:configuration.svcinfo,configuration91.svcinfo,hello.disco,hello.wsdl,Reference.cs,Reference.svcmap,共六个文件,或者直接添加服务引用。要使用的泪和方法在下图:

添加服务引用和添加Web引用对比 - king - king

在Web项目和Web网站解决方案引用时可以选择添加Web引用还是服务引用。

1.添加Web引用,选择命名空间为localhost,在App_WebReferences文件夹下出现引用信息,localhost文件夹下有如下文件:Hello.disco,Hello.discomap,Hello.wsdl。

2.添加服务引用,命名空间为local1,local1文件夹中有以下文件:configuration.svcinfo,configuration91.svcinfo,hello.disco,Hello.wsdl,Reference.svcmap。

效果如下

添加服务引用和添加Web引用对比 - king - king

 
 
 
 
 
原文地址:https://www.cnblogs.com/wanshutao/p/3916068.html