how to convert a webservice into wcf

Posted by Klbaiju under WCF on 3/3/2011 | Points: 10 | Views : 3218 | Status : [Member] | Replies : 21
Hi,
this is my webservice application
[webmethod]
Public DataSet bind()
{
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("People.Xml");
return ds;
}
next step add webreference
select service as test
load event of another webform
Load()
{
test.Service result=new test.Service();
Gridview1.DataSource=result.bind();
Gridview1.DataBind();

}

I want to do this example in wcf.
How it is possible

Rgards
K L BAIJU




Responses

Posted by: Vuyiswamb on: 3/3/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Normally you would have an interface

using System;

using System.Collections.Generic;
using System.Data;
using System.ServiceModel;

namespace DAL
{
[ServiceContract]
public interface IDAL
{
[OperationContract]
DataSet bind() ;
}
}


and you will have a class that will implement the interface like this

namespace DAL

{
public class DAL:IDAL
{
public DataSet bind()
{
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("People.Xml");
return ds;
}


and in your application, you can do the same thing when you access the service, the difference will be that , you will see something like blablaClient and you can consume your method




Thank you for posting at Dotnetfunda
[Administrator]

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi friend
this is my code

public class Service1 : IService1
{
public DataSet bind()
{

DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("People.xml"));
return ds;

}

}
error is showing
error is
object referencce not set to instance of an object

and another one is
i can't write
ds.ReadXml(Server.MapPath("People.xml"));
instead of
ds.ReadXml(HttpContext.Current.Server.MapPath("People.xml"));


Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 3/3/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Let me see your Interface and in which line do you get an Error?

Thank you for posting at Dotnetfunda
[Administrator]

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/3/2011 [Member] Starter | Points: 25

Up
0
Down
ds.ReadXml(HttpContext.Current.Server.MapPath("People.xml"));
here showing error
error is

object referencce not set to instance of an object

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/3/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

You need to provide the full path of the XML file? with the root folder as well... Since its not having the correct path its showing object reference error

Thanks
Karthik
www.f5Debug.net

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/4/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,karthik can you send the sample code of
full path of the XML file? with the root folder.


Regards

K L BAIJU

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/4/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

You need to give like this

Server.MapPath("../../Directory/SubDirA/SubDirB/Yourpage.aspx")


Thanks
Karthik
www.f5Debug.net

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/5/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthik
this is not an aspx page.It is a class file

public class Service1 : IService1
{

public DataSet bind()
{

DataSet ds = new DataSet();


ds.ReadXml(HttpContext.Current.Server.MapPath("XMLFile1.xml"));
return ds;


}

}

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/5/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Yeah instead of ASPX Page give the .xml url... this something like a fully qualified xml link

Thanks
Karthik
www.f5Debug.net

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/6/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthik I have changed the code like this .still showing Error

public class Service1 : IService1
{

public DataSet bind()
{

DataSet ds = new DataSet();

ds.ReadXml(HttpContext.Current.Server.MapPath("C:Documents and Settings/baijukl/My Documents/Visual Studio 2008/WcfService3/WcfService3/XMLFile1.xml"));
return ds;


}

}
}

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/6/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
See you code should be something like this... Place the xml in your resource folder of your project and try to use it as below

public class Service1 : IService1

{

public DataSet bind()
{

DataSet ds = new DataSet();

ds.ReadXml(HttpContext.Current.Server.MapPath("../Resourcefolder/XMLFile1.xml"));
return ds;


}

}
}


Thanks
Karthik
www.f5Debug.net

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/6/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Karthik
I finally tested this

ds.ReadXml(HttpContext.Current.Server.MapPath("../Xml/XMLFile1.xml"));
showing same Error

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/6/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
OK what error you are getting nw?

Thanks
Karthik
www.f5Debug.net

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/7/2011 [Member] Starter | Points: 25

Up
0
Down
error is
object referencce not set to instance of an object
do u have any idea

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/7/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
This is def that the input xml is not mapped correctly... you need to check the path correctly...

Thanks
Karthik
www.f5Debug.net

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 4/9/2011 [Member] Starter | Points: 25

Up
0
Down


ds.ReadXml(HttpContext.Current.Server.MapPath("C:Documents and Settings/baijukl/My Documents/Visual Studio 2008/WcfService3/WcfService3/XMLFile1.xml"));
this is the correct path
i don't know what to do

Regards
K L BAIJU

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 4/9/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
that is not a Full path , access the file via "My computer" and where the file is , look at the adressbar , it will have your path

Thank you for posting at Dotnetfunda
[Administrator]

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 4/10/2011 [Member] Starter | Points: 25

Up
0
Down
i have checked the same in an ordinary application just like this
DataSet ds = new DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("Dept.xml"));

GridView1.DataSource = ds;
GridView1.DataBind();
this is working fine. then why it is showing error in wcf

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 4/10/2011 [Member] Starter | Points: 25

Up
0
Down
Can you send the detailed error description?

Best Regards,

VG
www.TeacherJi.com

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 4/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi friend this is full code.
you just try this example

Regards
K L BAIJU

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

[ServiceContract]
public interface IService1
{


[OperationContract]
IEnumerable<Department> depts();

}


//implementation
public class Service1 : IService1
{



public IEnumerable<Department> depts()
{
XDocument xdoc = XDocument.Load(HttpContext.Current.Server.MapPath("Dept.xml"));
IEnumerable<Department> depts = from p in xdoc.Descendants("Department")
select new Department
{
Name = p.Element("Id").Value,
Dept = p.Element("Dept").Value,

};
return depts;

}
}
---Department.cs

public class Department
{
public string Name
{ get; set; }
public int Id
{ get; set; }
public string Dept
{ get; set; }
}


-------------
Dept.xml

<?xml version="1.0" encoding="utf-8" ?>
<Departments>
<Department>
<Id>100</Id>
<Name>Arun</Name>
<Dept>electrical</Dept>
</Department>
<Department>
<Id>108</Id>
<Name>Kiran</Name>
<Dept>computer</Dept>
</Department>
</Departments>

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 4/13/2011 [Member] Starter | Points: 25

Up
0
Down
Anyone have idea

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response