Saturday 3 December 2016

.asp wcf service


presentation -> wcf service ->database

add wcf_service as reference to presentation
add entity_frame as reference to wcf_service
copy connectionstring from app.config in entity_frame, paste it to web.config in both presentation and wcf_service


add service to wcf_service project


after rename service, make sure name also changed in markup view


//service_interface.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Wcf_service.domain;

namespace Wcf_service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface service_interface
    {

        [OperationContract]
        List<dto> get_instructor();

        [OperationContract]
        void add_instructor(dto instructor);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    //[DataContract]
    //public class CompositeType
    //{
    //    bool boolValue = true;
    //    string stringValue = "Hello ";

    //    [DataMember]
    //    public bool BoolValue
    //    {
    //        get { return boolValue; }
    //        set { boolValue = value; }
    //    }

    //    [DataMember]
    //    public string StringValue
    //    {
    //        get { return stringValue; }
    //        set { stringValue = value; }
    //    }
    //}
}

--------------------------------------------------------
//service_method.cs //implement service interface
using ClassLibrary1; //entity_frame library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Wcf_service.domain;

namespace Wcf_service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class service_method : service_interface
    {
        public void add_instructor(dto instructor)
        {           
            var i = new Instructor();
            i.FirstName = instructor.FirstName;
            i.LastName = instructor.LastName;

            var db = new CTTIEntities();
            db.Instructors.Add(i);
            db.SaveChanges();
        }

        public List<dto> get_instructor()
        {
            var db = new CTTIEntities();

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

            return instructors;
        }
    }
}

--------------------------------------------------------
//~/wcfservie/domain/dto.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Wcf_service.domain
{
    public class dto
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}


add service reference to presentation
right click on reference -> add service reference -> discover -> expand tree view


select service reference under presentation as data source
add objectdatasource to listview




//default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wcf_service.domain;

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

        }

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

            //consume service
            var proxy = new wcf_service_reference.service_interfaceClient();
            proxy.add_instructor(instructor);
            Page.DataBind();
        }
    }
}



No comments:

Post a Comment