vs2010使用ajax,使用vs2010创建、发布、部署、调用 WebService

一 使用vs2010创建 WebService

1

打开VS2010,菜单 文件->新建->项目

2 选择 其它语言--Visual C# --Web--ASP.net 空web应用程序,给个名称myWebApplication

,点确定。

3 右键点击解决方案下自己的项目myWebApplication,添加->新建项--Visual C#--Web--选择

web服务,起个服务名称为myWebSevice1

4 在自动打开的myWebSevice1.asmx文件中,继续加入自己的WebService方法。

5

方法写完后,右击解决方案下自己的项目myWebApplication->发布,选择【文件系统】发布,指定发布后的文件夹地址,点击【发布】即可;

6 打开IIS 建立网站,在控制面板中--管理工具--Internet

信息服务,逐级打开到默认网站,右键点击默认网站->新建->虚拟目录,网站名称,myWebService,内容文件地址是上面发布的文件夹地址

7 继续为网站设置,虚拟目录下,执行权限为“纯脚本”,目录安全性,勾选“匿名访问”, 勾选 “集成Windows身份验证”

ASP.NET 下, ASP.NET 版本选择 4.0.30319

8 对于没有权限访问的,可以这样设置,方法如下:

开始--程序--Microsoft Visual Studio 2010 -- Visual Studio Tools --

Visual Studio 命令提示(2010)

进入文件夹

C:\Program Files\Microsoft Visual Studio 10.0\VC

执行命令:aspnet_regiss.exe -i 即可

简要代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Collections.ObjectModel;

using System.IO;

using System.Xml.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

namespace myWebApplication

{

///

///

myWebService1 的摘要说明

///

[WebService(Namespace = "http://myTempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

// 若要允许使用

ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

//

[System.Web.Script.Services.ScriptService]

public class

myWebService1 : System.Web.Services.WebService

{

[WebMethod]

public string HelloWorld()

{

return "Hello World";

}

[WebMethod]

public int Add(int a, int b)

{

return (a + b);

}

[WebMethod]

public string Sub(int a, int b)

{

return (a - b).ToString();

}

[WebMethod]

public string class_one(string str)

{

fuzaModel mm2 = serverFun.Deserialize(typeof(fuzaModel), str) as

fuzaModel;

mm2.a=mm2.a*3;

mm2.b=mm2.b+"web";

return serverFun.Serialize(mm2);

}

[WebMethod]

public string class_set(string str)

{

List newList = serverFun.Deserialize(typeof(List), str) as

List;

foreach (var info in newList)

{

info.a = info.a * 3;

info.b = info.b + " web";

}

return serverFun.Serialize>(newList);

}

}

[XmlInclude(typeof(fuzaModel))]

[Serializable]

public class

fuzaModel

{

private int m_UserId;

[XmlElement("userId")]

public int UserId

{

get { return m_UserId; }

set { m_UserId = value; }

}

public int a;

public string b;

// [WebMethod]

public fuzaModel aModel(fuzaModel m)

{

m.a = 2;

m.b = "b";

return m;

}

}

[Serializable]

public class

serverFun : System.Web.Services.WebService

{

[WebMethod]

public static string Serialize(T t)

{

using (StringWriter sw = new StringWriter())

{

XmlSerializer xz = new XmlSerializer(t.GetType());

xz.Serialize(sw, t);

return sw.ToString();

}

}

[WebMethod]

public static object Deserialize(Type type, string s)

{

using (StringReader sr = new StringReader(s))

{

XmlSerializer xz = new XmlSerializer(type);

return xz.Deserialize(sr);

}

}

// 在调用端序列化实体对象(实体对象须是可序列化的类): [WebMethod]

public static byte[] SerializeObject(object pObj)

{

if (pObj ==

null) return null;

System.IO.MemoryStream memoryStream = new

System.IO.MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(memoryStream, pObj);

memoryStream.Position = 0;

byte[] read = new byte[memoryStream.Length];

memoryStream.Read(read, 0, read.Length);

memoryStream.Close();

return read;

}

//在service的调用方法中,接受一个byte[] 参数即可,然后反序列化:

//然后将object强制转换成相应的实体类型,就可以直接使用此对象了。

[WebMethod]

public static object DeserializeObject(byte[] pBytes)

{

object newOjb = null;

if (pBytes == null)return newOjb;

System.IO.MemoryStream memoryStream = new

System.IO.MemoryStream(pBytes);

memoryStream.Position = 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值