Web Services & WCF Services (AIML)
Web Services & WCF Services (AIML)
WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace webService
{
/// <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 string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int Add(int a, int b)
{ return a + b; }
[WebMethod]
public int Sub (int a, int b)
{ return a + b; }
[WebMethod]
public int Multi (int a, int b)
{ return a + b; }
[WebMethod]
public int divi (int a, int b)
{ return a + b; }
}
}
https://localhost:44330/WebService1.asmx
WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication13
{
public partial class WebForm1 : System.Web.UI.Page
{
localhost.WebService1 service1 = new localhost.WebService1();
protected void Page_Load(object sender, EventArgs e)
{
WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Remoting.Messaging;
using System.Diagnostics;
namespace Webserver2
{
/// <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
{
SqlConnection con;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
public void Connect()
{
con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;
AttachDbFilename=C:\Users\timscdr\source\repos\Webserver2\Webserver2\App_Data\
emp.mdf;
Integrated Security=True");
con.Open();
}
[WebMethod]
public int InsterData(string name, string salary)
{
Connect();
SqlCommand cmd = new SqlCommand("insert into EMP_info (name, salary)
values('" + name + "', " + salary + ")", con);
return cmd.ExecuteNonQuery();
}
[WebMethod]
public DataSet select()
{
DataSet ds = new DataSet();
Connect();
SqlDataAdapter da = new SqlDataAdapter("Select * from EMP_info", con);
da.Fill(ds);
return ds;
}
[WebMethod]
public DataSet SearchRecord(string searchdata)
{
Connect();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from EMP_info
where name like '%" + searchdata+"%'", con);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
}
}
WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication14
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
3. Write a program to create a WCF Service which adds two numbers and also
create a client to call that service.
IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
int Add(int a, int b);
[OperationContract]
int Sub(int a, int b);
[OperationContract]
int Divi(int a, int b);
[OperationContract]
int Multi(int a, int b);
}
// 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.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "Service" in code, svc and config file together.
public class Service : IService
{
public int Add(int a, int b)
{
return a+b;
}
public int Sub(int a, int b)
{
return a - b;
}
public int Divi(int a, int b)
{
return a/b;
}
public int Multi(int a, int b)
{
return a * b;
}
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication16
{
public partial class WebForm1 : System.Web.UI.Page
{
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
protected void Page_Load(object sender, EventArgs e)
{
Divi OUTPUT
Multi OUTPUT
4. Create a WCF service which is used to display student information and
also create a client that displays the same information.
IService.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
void DoWork();
[OperationContract]
int IntsertData(string name, int proce);
[OperationContract]
DataSet SelectRecode();
[OperationContract]
DataSet SearchData(string name);
}
// 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.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the
class name "Service" in code, svc and config file together.
public class Service : IService
{
public void DoWork()
{
throw new NotImplementedException();
}
}
public DataSet SearchData(string name)
{
Connect();
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from emp where
Name like '%" + name + "%'", con);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
}
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;
using WebApplication17.ServiceReference1;
namespace WebApplication17
{
public partial class WebForm1 : System.Web.UI.Page
{
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
</div>
<input type="submit" value="Display" />
Showemp.cshtml
@model WebApplication3.Models.emp
@{
ViewBag.Title = "Showemp";
}
<h2>Showemp</h2>
@Model.Id
@Model.Name
empController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication3.Models;
namespace WebApplication3.Controllers
{
public class empController : Controller
{
// GET: emp
public ActionResult Index()
{
return View();
}
public ActionResult Showemp()
{
emp emp1 = new emp();
emp1.Id = int.Parse(Request.Form["empid"]);
emp1.Name = Request.Form["empname"];
return View(emp1);
}
}
}
emp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication3.Models
{
public class emp
{
public int Id { get; set; }
public string Name { get; set; }
}
}
OUTPUT