web服务器(Web Services)的小介绍(4)--(C#)

                                  web服务器(webservices)
        webservices即Web Service, Web Service技术,是应用于第三方软件的,咱们能够经过配置它,给第三方配置一个对外的能够调用的外部的一个 接口。在咱们开发中是常常须要用到的重要的知识内容,因此在这里和你们探究一下它的主要用法:web

    一、web服务的页面都是以 .asmx结尾的.首先是在framework3.5版本如下,由于在4.0版本中微软是集成到wcf中服务器

       

wKioL1L7gPfR0_E-AAJlguN4U8Q091.jpg

    

  二、.asmx是web服务的前台页面文件,会使用一个叫作.asmx.cs的文件来存放页面类文件,例如:getpig.asmx 会有一个对应的getpig.asmx.cs文件。ide

wKiom1L7hGWywJxpAACN9hXGxeE505.jpg

    三、getpig.asmx.cs 中的内容说明:
         3.一、全部web服务页面类必定要集成                                   System.Web.Services.WebService public class GetPig : System.Web.Services.WebService 
         3.二、要想使某个方法作为web服务的方式暴露出去必定在此方法上添加: [WebMethod] 特性,这样才能在其余站点中访问到,不然是做为此web服务中的内部方法。
网站

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Web服务
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://itcast.cn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要容许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释如下行。
// [System.Web.Script.Services.ScriptService]
public class GetPig : System.Web.Services.WebService
{
[WebMethod] // 注意:在web服务器类中若是要使某个方法可以被外部调用则必定要加上 [WebMethod]特性
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public Pig GetPigInfo(int age)
{
List<Pig> list = new List<Pig>() {
new Pig(){ Name="猪猪",Age=2},
new Pig(){ Name="小猪",Age=1},
new Pig(){ Name="八戒",Age=500}
};
return list.FirstOrDefault(c => c.Age == age);
}
}
}

   四、在一个web站点的项目中要想使用web服务必须按照如下步骤来执行:
         4.一、在web站点的【引用】上右键点击 【添加服务引用】,这时VS 自动打开服务面板,输入你发布的web服务的地址(例如:http://127.0.0.1/GetPig.asmx)
spa

wKiom1L7gh7CKeL3AABrIy-23Jg761.jpg

  

  后自动生成web服务对于的代理类,同时会向web站点的web.config中注册 <system.serviceModel>
   <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="GetPigSoap" />
            </basicHttpBinding>
        </bindings>
        <client>
        <endpoint  address="http://192.168.10.2:8085/GetPig.asmx"   //web服务的发布地址
                 binding="basicHttpBinding"                       //web服务的绑定方式
                 bindingConfiguration="GetPigSoap"                //web服务的配置类型名称
                 contract="WbS.GetPigSoap"                        //web服务的契约
                name="GetPigSoap" />                              //web服务的名称
        </client>
    </system.serviceModel>

        4.二、在web站点中调用web服务的写法:代理

           先导入命名空间(查看方式在:网站项目的 Service References 下的代理对象中查看,例如演示项目中的命名空间为:WebSite.Webs) 好比要调用 http://192.168.10.2:8085/GetPig.asmx 中的方法 GetPigInfo 的写法:
  orm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebSite
{
//1.0 导入web服务器代理类的命名空间
using WebSite.WbS;
using WebSite.WebsDog;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetDogInfoSoapClient client = new GetDogInfoSoapClient();
Dog dog = client.GetDog();
}
protected void Button1_Click(object sender, EventArgs e)
{
GetPigSoapClient client = new GetPigSoapClient();
Pig pig = client.GetPigInfo(int.Parse(TextBox1.Text));
Response.Write(pig.Name + "  ,Age=" + pig.Age);
}
}
}

 

 

j_0050.gif

相关文章
相关标签/搜索