Consume Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter a Key");
string _AuthorId = Console.ReadLine();
ServiceReference1.ServiceClient sc = new ServiceReference1.ServiceClient();
ServiceReference1.Author a = sc.GetAuthorInfo(_AuthorId);
if (a != null)
{
Console.WriteLine(a.ArticleId);
Console.WriteLine(a.FirstName);
Console.WriteLine(a.LastName);
}
else
{
Console.WriteLine("Key is not valid");
}
Console.ReadLine();
}
catch (FaultException<string> e1)
{
Console.WriteLine(e1.Message);
Console.WriteLine(e1.Reason);
Console.WriteLine(e1.Detail);
}
}
}
}
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;
using System.Data;
using System.Data.SqlClient;
// 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
{
private const string _AuthorId = "Gulshan";
SqlConnection con;
SqlDataAdapter dap;
DataSet ds;
public bool CreateConnection(AuthorRequest Req)
{
con = new SqlConnection();
con.ConnectionString = "Data Source=xxxx;Initial Catalog=Master;User Id=xxxxx;Password=xxxxxx;";
dap = new SqlDataAdapter("SELECT EMP_ID FROM Employee_Test1 where EMP_ID='"+Req._AuthorId+"'", con);
ds = new DataSet();
dap.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
string x = "Data Exists";
return true;
}
else
{
return false;
}
}
public AuthorResponse GetAuthorInfo(AuthorRequest Req)
{
bool b=CreateConnection(Req);
if (b == true)
{
//if (Req._AuthorId != _AuthorId)
//{
// string _Error = "Invalid Author Id";
// throw new FaultException<string>(_Error);
//}
AuthorResponse au = new AuthorResponse();
au.Obj = new Author();
au.Obj.FirstName = "Gulshan Kumar";
au.Obj.LastName = "Arora";
au.Obj.ArticleId = "11001";
return au;
}
else
{
AuthorResponse au = new AuthorResponse();
au.Obj = null;
return au;
}
}
}
Interface:
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]
[FaultContract(typeof(string))]
AuthorResponse GetAuthorInfo(AuthorRequest Req);
}
[MessageContract]
public class AuthorRequest
{
[MessageHeader(Name = "AuthorId")]
public string _AuthorId;
}
[MessageContract]
public class AuthorResponse
{
[MessageBodyMember]
public Author Obj;
}
[DataContract]
public class Author
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
[DataMember]
public string ArticleId;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter a Key");
string _AuthorId = Console.ReadLine();
ServiceReference1.ServiceClient sc = new ServiceReference1.ServiceClient();
ServiceReference1.Author a = sc.GetAuthorInfo(_AuthorId);
if (a != null)
{
Console.WriteLine(a.ArticleId);
Console.WriteLine(a.FirstName);
Console.WriteLine(a.LastName);
}
else
{
Console.WriteLine("Key is not valid");
}
Console.ReadLine();
}
catch (FaultException<string> e1)
{
Console.WriteLine(e1.Message);
Console.WriteLine(e1.Reason);
Console.WriteLine(e1.Detail);
}
}
}
}
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;
using System.Data;
using System.Data.SqlClient;
// 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
{
private const string _AuthorId = "Gulshan";
SqlConnection con;
SqlDataAdapter dap;
DataSet ds;
public bool CreateConnection(AuthorRequest Req)
{
con = new SqlConnection();
con.ConnectionString = "Data Source=xxxx;Initial Catalog=Master;User Id=xxxxx;Password=xxxxxx;";
dap = new SqlDataAdapter("SELECT EMP_ID FROM Employee_Test1 where EMP_ID='"+Req._AuthorId+"'", con);
ds = new DataSet();
dap.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
string x = "Data Exists";
return true;
}
else
{
return false;
}
}
public AuthorResponse GetAuthorInfo(AuthorRequest Req)
{
bool b=CreateConnection(Req);
if (b == true)
{
//if (Req._AuthorId != _AuthorId)
//{
// string _Error = "Invalid Author Id";
// throw new FaultException<string>(_Error);
//}
AuthorResponse au = new AuthorResponse();
au.Obj = new Author();
au.Obj.FirstName = "Gulshan Kumar";
au.Obj.LastName = "Arora";
au.Obj.ArticleId = "11001";
return au;
}
else
{
AuthorResponse au = new AuthorResponse();
au.Obj = null;
return au;
}
}
}
Interface:
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]
[FaultContract(typeof(string))]
AuthorResponse GetAuthorInfo(AuthorRequest Req);
}
[MessageContract]
public class AuthorRequest
{
[MessageHeader(Name = "AuthorId")]
public string _AuthorId;
}
[MessageContract]
public class AuthorResponse
{
[MessageBodyMember]
public Author Obj;
}
[DataContract]
public class Author
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
[DataMember]
public string ArticleId;
}
No comments:
Post a Comment
Your comment is pending for approval