Wednesday, 30 November 2016

.asp xml service



presentation layer -> service -> server data


add .ado (model1.edmx) to entityframe data library


add webservice1.asmx to service .asp project
service project creates service
//~/service/webservice.asmx

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

namespace service
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public dto[] get_instructors()
        {
            var db = new CTTIEntities();

            var instructors = db.Instructors.
                Select(x => new dto
                {
                    Id = x.Id,
                    FirstName = x.FirstName,
                    LastName = x.LastName
                }).ToArray();

            return instructors;
        }

        [WebMethod]
        public void add(dto new_instructor)
        {
            var db = new CTTIEntities();

            var i = new Instructor();
            i.FirstName = new_instructor.FirstName;
            i.LastName = new_instructor.LastName;

            db.Instructors.Add(i);
            db.SaveChanges();
        }
    }
}

--------------------------------------------------------

copy connection string from ~/entityframe/app.config

 <connectionStrings>
  <add name="CTTIEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=CTTI;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

into

~/presentation/web.config
and ~/service/web.config



right click service project, select property
select webservice1.asmx as start page


right click on service project, set as start up project



run solution, see get_instructors method from webservice1.asmx


click invoke


get_instructors method returns instructor info
test is successful


//dto.cs (data transfer object) to service project

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

namespace service
{
    public class dto
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

//copy property from instructor.cs in entityframe (data) project


add service to presentation project
presentation consume service


click advanced


click add web reference


click web service in this solution


select webservice1


if new webmethod is added in ~/service/webservice1.asmx, right click -> update




~/presentation/default.aspx



objectdata source, select ~/presentation/xml_service/webservice1.asmx
data source from web reference get_instructor() method


select get_instructor method
service consumed



set presentation as start project
set data gridview source to objectdatasource


//~/presentation/default.aspx.cs

using object_form1.xml_service; //use service reference
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace object_form1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var ins = new dto();
            ins.FirstName = TextBox1.Text;
            ins.LastName = TextBox2.Text;

            var proxy = new xml_service.WebService1(); //select service
            proxy.add(ins); //method from service
            Page.DataBind(); //update table/ data pull back from server
        }
    }
}




No comments:

Post a Comment