Using the Microsoft.Practices.EnterpriseLibrary.Data configuration database in WebService

Source: Internet
Author: User
Tags connectionstrings

1. New WebApplication1 Project

1.1 New-web-asp.net Empty Web application--webapplication1

1.2 Adding a WebForm1

2. New WebService Project

2.1 New-web-asp.net Empty Web Application-webapplicationservice

2.2 Select webapplicationservice-Right-click-add-webservice (ASMX)--Webservicegss.asmx

2.3 Adding references in Project Webapplicationservice

Microsoft.Practices.EnterpriseLibrary.Data.dll (version 4.1)

Microsoft.Practices.EnterpriseLibrary.Common.dll (version 4.1)

Note that if you report an error, Microsoft.Practices.EnterpriseLibrary.Common "type cannot be instantiated,

Possible solution: Microsoft.Practices.ObjectBuilder.dll add to References Oh ~ ~ ~

Write a method in the service,

File Webservicegss.asmx Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;namespacewebapplicationservice{[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 classWebServiceGSS:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld () {return "Hello World"; } [WebMethod (Description="Returns A list of ReportServerUrl one is for source another are for destination")]         PublicDataSet Getreportserverurl (GUID sourceclientid, guid destinationclientid) {returnGssdml.        Getreportserverurl (Sourceclientid, Destinationclientid); } [WebMethod (Description="Returns A list of Roles")]         PublicDataSet Getreportrole () {returnGssdml.        Getrole (); }    }}

File GSSDML.cs code (and webservicegss.asmx separate, clear)

usingMicrosoft.Practices.EnterpriseLibrary.Data;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.Common;usingSystem.Data.SqlClient;usingSystem.Linq;usingsystem.web;namespacewebapplicationservice{ Public classGSSDML {Private Const stringDB_GSS ="DB_GSS"; Private Const stringDb_fas ="Db_fas";  Public StaticDataSet Getreportserverurl (GUID sourceclientid, guid destinationclientid) {SqlDataAdapter adapter =NewSqlDataAdapter (); DataSet DS=NewDataSet (); Try            {                using(DbConnection con =databasefactory.createdatabase (DB_GSS). CreateConnection ()) {Adapter=NewSqlDataAdapter (); Adapter. SelectCommand=NewSqlCommand (); Adapter. SelectCommand.Connection= Con asSqlConnection; 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            {            }            returnds; }         Public StaticDataSet Getrole () {SqlDataAdapter adapter=NewSqlDataAdapter (); DataSet DS=NewDataSet (); Try            {                using(DbConnection con =databasefactory.createdatabase (DB_FAS). CreateConnection ()) {Adapter=NewSqlDataAdapter (); Adapter. SelectCommand=NewSqlCommand (); Adapter. SelectCommand.Connection= Con asSqlConnection; Adapter.SelectCommand.CommandText="SELECT * FROM [dbo]. [Role]"; Adapter.SelectCommand.CommandType=CommandType.Text; Con.                    Open (); Adapter.                Fill (DS); }            }            Catch            {            }            returnds; }    }}

Config file in the service

<?xml version="1.0"?><!--For more information on what to configure your ASP. 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"/> "4.5.2"/> </system.web></configuration>

3. Add WebService Reference

4. A settings.settings and web References will appear when the add is complete

5. Write the test code in WebForm1.aspx.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebapplication1{ Public Partial classWebForm1:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e)        {Web (); }         Public voidweb () {serviceweb.globalswitchingservice serviceweb=NewServiceweb.globalswitchingservice (); //Serviceweb.url = "http://localhost: 25003/webservicegss.asmx "; //Configuring URLs in setting//Serviceweb.url = Properties.Settings.Default.GlobalSwitchingService; //configuring URLs in config//Serviceweb.url = system.configuration.configurationmanager.appsettings["Globalswitchingservice"];            strings =Serviceweb.helloworld (); Guid ID=NewGuid (); DataSet DS=Serviceweb.getreportserverurl (ID, id); DataSet DS2=Serviceweb.getreportrole (); }    }}

6. If you want to customize the port number of a service, you can set it on the Web page of the property

Using the Microsoft.Practices.EnterpriseLibrary.Data configuration database in WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.