1、 新建WebApplication1项目
1.1 新建—Web—ASP.NET Empty Web Application--WebApplication1
1.2 添加一个WebForm1
2、 新建webService项目
2.1 新建—Web—ASP.NET Empty Web Application—WebApplicationService
2.2 选择WebApplicationService—右键—Add—WebService (ASMX)-- WebServiceGSS.asmx
2.3 在项目WebApplicationService中添加引用
Microsoft.Practices.EnterpriseLibrary.Data.dll(版本4.1)
Microsoft.Practices.EnterpriseLibrary.Common.dll(版本4.1)
注意,如果报错,Microsoft.Practices.EnterpriseLibrary.Common”類型不能實例化之類的,
可能解決方法:Microsoft.Practices.ObjectBuilder.dll加到References中就哦啦~~~
写服务中的方法,
文件WebServiceGSS.asmx代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; namespace WebApplicationService { [WebService(Namespace = "http://Mercer.GlobalSwitchingService/2008/09", Name = "GlobalSwitchingService")] [System.Web.Services.WebServiceBindingAttribute(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1, EmitConformanceClaims = true, Name = "GlobalSwitchingService")] [ToolboxItem(false)] public class WebServiceGSS : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod(Description = "Returns a list of ReportServerURL one is for source another is for destination")] public DataSet GetReportServerURL(Guid sourceClientID, Guid destinationClientID) { return GSSDML.GetReportServerURL(sourceClientID, destinationClientID); } [WebMethod(Description = "Returns a list of Roles")] public DataSet GetReportRole() { return GSSDML.GetRole(); } } }
文件GSSDML.cs代码(和WebServiceGSS.asmx分开,条理清楚些)
using Microsoft.Practices.EnterpriseLibrary.Data; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.SqlClient; using System.Linq; using System.Web; namespace WebApplicationService { public class GSSDML { private const string DB_GSS = "DB_GSS"; private const string DB_FAS = "DB_FAS"; public static DataSet GetReportServerURL(Guid sourceClientID, Guid destinationClientID) { SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); try { using (DbConnection con = DatabaseFactory.CreateDatabase(DB_GSS).CreateConnection()) { adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand(); adapter.SelectCommand.Connection = con as SqlConnection; adapter.SelectCommand.CommandText = "getStudentTable"; adapter.SelectCommand.CommandType = CommandType.StoredProcedure; //adapter.SelectCommand.Parameters.Add(new SqlParameter("@SourceClientID", sourceClientID)); //adapter.SelectCommand.Parameters.Add(new SqlParameter("@DestinationClientID", destinationClientID)); con.Open(); adapter.Fill(ds); } } catch { } return ds; } public static DataSet GetRole() { SqlDataAdapter adapter = new SqlDataAdapter(); DataSet ds = new DataSet(); try { using (DbConnection con = DatabaseFactory.CreateDatabase(DB_FAS).CreateConnection()) { adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand(); adapter.SelectCommand.Connection = con as SqlConnection; adapter.SelectCommand.CommandText = " SELECT * from [dbo].[Role]"; adapter.SelectCommand.CommandType = CommandType.Text; con.Open(); adapter.Fill(ds); } } catch { } return ds; } } }
服务中的Config文件
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="DB_GSS" connectionString="Database=Test; Server=.; User ID=sa;Password=123;Connection Timeout=90;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/> <add name="DB_FAS" connectionString="Database=fas; Server=.; User ID=sa;Password=123;Connection Timeout=90;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.5.2" /> <httpRuntime targetFramework="4.5.2" /> </system.web> </configuration>
3、添加WebService引用
4、添加完成后会出现一个Settings.settings和Web References
5、在WebForm1.aspx.cs中写测试代码
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { web(); } public void web() { ServiceWeb.GlobalSwitchingService serviceWeb = new ServiceWeb.GlobalSwitchingService(); //serviceWeb.Url = "http://localhost:25003/WebServiceGSS.asmx"; //在setting中配置url //serviceWeb.Url = Properties.Settings.Default.GlobalSwitchingService; //在config中配置url //serviceWeb.Url = System.Configuration.ConfigurationManager.AppSettings["GlobalSwitchingService"]; string s = serviceWeb.HelloWorld(); Guid id = new Guid(); DataSet ds = serviceWeb.GetReportServerURL(id, id); DataSet ds2 = serviceWeb.GetReportRole(); } } }
6、如果想自定义服务的端口号,可以在属性的web页设置
时间: 2024-10-18 01:06:02