PDA

View Full Version : سوال: چند سوال درباره وب سرويس ها !



merlin_vista
دوشنبه 28 مرداد 1387, 17:33 عصر
سلام . آقا من دارم تازه با وب سرويس ها كار ميكنم ولي چند تا سوال داشتم گفتم خوبه اينجا بپرسم . :چشمک:

1 - من يك وب متد ساختم به اين صورت :‌

public DataTable DbData()
{
SqlDataAdapter dat = new SqlDataAdapter("select * from tbl_Melk", con);
dat.SelectCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
dat.Fill(dt);
return dt;

}و در برنامه اينجوري صدا ميزنم :

merlin.Service serv_merl = new merlin.Service();
this.GridView1.DataSource = serv_merl.DbData();
this.GridView1.DataBind();ولي اين خطا را ميده :

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.<br> at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy)<br> at System.Data.DataTable.System.Xml.Serialization.IXm lSerializable.WriteXml(XmlWriter writer)<br> at System.Xml.Serialization.XmlSerializationWriter.Wr iteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)<br> at Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_DbDataResponse(Object[] p)<br> at Microsoft.Xml.Serialization.GeneratedAssembly.Arra yOfObjectSerializer5.Serialize(Object objectToSerialize, XmlSerializationWriter writer)<br> at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)<br> --- End of inner exception stack trace ---<br> at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)<br> at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)<br> at System.Web.Services.Protocols.SoapServerProtocol.W riteReturns(Object[] returnValues, Stream outputStream)<br> at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[] returnValues)<br> at System.Web.Services.Protocols.WebServiceHandler.In voke()<br> --- End of inner exception stack trace --- body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.
at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy)
at System.Data.DataTable.System.Xml.Serialization.IXm lSerializable.WriteXml(XmlWriter writer)
at System.Xml.Serialization.XmlSerializationWriter.Wr iteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write3_DbDataResponse(Object[] p)
at Microsoft.Xml.Serialization.GeneratedAssembly.Arra yOfObjectSerializer5.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
at System.Web.Services.Protocols.SoapServerProtocol.W riteReturns(Object[] returnValues, Stream outputStream)
at System.Web.Services.Protocols.WebServiceHandler.Wr iteReturns(Object[] returnValues)
at System.Web.Services.Protocols.WebServiceHandler.In voke()
--- End of inner exception stack trace ---

2 - براي اعتبار سنجي در وب سرويس ها بايد چگونه عمل كنيم . (اگه يه مقاله در ابم مورد بدين ممنون ميشم . ترجيحآً با سي شارپ و فارسي ) اگه يه مقاله در زمينه امنيت در وب سرويس باشه ممنون ميشم .

ممنون :لبخندساده:

mohammad272005
دوشنبه 28 مرداد 1387, 21:21 عصر
Cannot serialize the DataTable. DataTable name is not set.بیچاره خودش به این قشتگی داره میگه. حالا تستی یه اسم به datatableت بده ببین چی میشه.

jaza_sa
سه شنبه 29 مرداد 1387, 01:35 صبح
کد شما نیاز به serialize شدن داره
کد زیر رو برای انجام این کار به برنامتون اضافه کنید

[Serializable]
public class WebService : System.Web.Services.WebService {
public WebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}ها و بالا سر نام کلاس :چشمک:

بطور نمونه به این متد ها دقت کنید :

[WebMethod]
public DataTable GetAllProduct2()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Server=.\SQLEXPRESS; Database=AdventureWorks; Integrated Security=True;";
con.Open();
string doCommand = "SELECT ProductID, Name, ProductNumber, Color, ModifiedDate FROM Production.Product WHERE Color='Silver'";
SqlCommand cmd = new SqlCommand(doCommand, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
return dt;
}
[WebMethod]
public DataSet GetAllProduct1()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Server=.\SQLEXPRESS; Database=AdventureWorks; Integrated Security=True;";
con.Open();
string doCommand = "SELECT ProductID, Name, ProductNumber, Color, ModifiedDate FROM Production.Product WHERE Color='Silver'";
SqlCommand cmd = new SqlCommand(doCommand, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
return ds;
}

merlin_vista
سه شنبه 29 مرداد 1387, 11:44 صبح
2 - براي اعتبار سنجي در وب سرويس ها بايد چگونه عمل كنيم . (اگه يه مقاله در ابم مورد بدين ممنون ميشم . ترجيحآً با سي شارپ و فارسي ) اگه يه مقاله در زمينه امنيت در وب سرويس باشه ممنون ميشم .


كسي براي اين نظري چيزي نداره !!

merlin_vista
سه شنبه 29 مرداد 1387, 11:50 صبح
[Serializable]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["cstr"].ToString());
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public DataTable GetAllProduct2()
{
DataTable dt = new DataTable();
con.Open();
string doCommand = "select * from tbl_Melk";
SqlCommand cmd = new SqlCommand(doCommand, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
return dt;
}

}



protected void Page_Load(object sender, EventArgs e)
{
merlin.Service me = new merlin.Service();
this.GridView1.DataSource = me.GetAllProduct2();
this.GridView1.DataBind();
}


ولي دوباره همان خطا را ميده .

jaza_sa
سه شنبه 29 مرداد 1387, 15:25 عصر
به این مثال دقت کنید ، ممکنه مقداری از مشکلتون حل بشه :چشمک:

mehdi.mousavi
سه شنبه 29 مرداد 1387, 16:01 عصر
سلام.
جواب رو همون اول آقای mohammad272005 دادن، اونجاییکه دارید DataTable رو new میکنید، باید یه اسم به DataTable بدین، و الا قابل Serialize شدن نیست:


DataTable dt = new DataTable("mine");

در مورد Validation هم وقتی شما، Reference به یک Web Service رو به برنامه اضافه میکنید، عملیات مورد نظر بطور خودکار انجام میشه، و دیگه نیازی نیست که دستی کاری انجام بدین.

پاورقی:
جناب jaza_sa عنوان کردن که باید Serializable Attribute رو به کلاس Web Service اتون اعمال کنید. این حرف درست نیست. کلاس Web Service قرار نیست Serialize بشه، اون DataTable هستش که میخواد Serialize بشه و به Client برگرده که مایکروسافت قبلا این Attribute رو به کلاس DataTable، نسبت داده.

mhaeri
شنبه 20 مهر 1387, 11:45 صبح
يك سئوال
آيا تعداد پارامترهاي كه به وب سرويس پاس داده مي شوند و بالعكس آيا محدوديت دارد؟
مثلا اگر 100 تا پارامتر را بخواهيم Send كنيم يا Get مشكلي هست؟