I need to make SMS program using C# , I took the sample code from the SMS company and paste it to my project But its not sending when click SEND button and got this error :
Type 'mamlaka_lab.BL.SendClass' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
This is the complete code :
1- SendClass :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace mamlaka_lab.BL
{
class SendClass
{
private string _recipients = string.Empty;
private string _body = string.Empty;
private string _sender = string.Empty;
[DataMember()]
public string recipients
{
set
{
_recipients = value;
}
get
{
return _recipients;
}
}
[DataMember()]
public string body
{
set
{
_body = value;
}
get
{
return _body;
}
}
[DataMember()]
public string sender
{
set
{
_sender = value;
}
get
{
return _sender;
}
}
}
}
2- JsonSerialize Code:
private string JSONSerialize(mamlaka_lab.BL.SendClass objStudent)
{
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof(mamlaka_lab.BL.SendClass));
jsonSer.WriteObject(stream, objStudent);
stream.Position = 0;
StreamReader sr = new StreamReader(stream);
return sr.ReadToEnd();
}
3- SMSSEND_Click Code:
private void SMSSEND_Click(object sender, EventArgs e)
{
string strResponse;
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
ServicePointManager.Expect100Continue = true;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.taqnyat.sa/v1/messages");
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("Authorization", "Bearer " TxtBearer.Text);
mamlaka_lab.BL.SendClass objStudent = new mamlaka_lab.BL.SendClass();
// objStudent.body = smsBody;
// objStudent.recipients = txtMobile.Text;
// objStudent.sender = sender.ToString();
objStudent.body = TxtBody.Text;
objStudent.recipients = TxtRecipients.Text;
objStudent.sender = TxtSender.Text;
Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(JSONSerialize(objStudent));
req.ContentLength = byteArray.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
TxtResult.Text = strResponse;
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse1 = (HttpWebResponse)response;
try
{
using (Stream data = response.GetResponseStream())
{
using (var reader = new StreamReader(data))
{
strResponse = reader.ReadToEnd();
}
}
TxtBalance.Text = strResponse;
}
catch (Exception exGetResp)
{
throw exGetResp;
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
when I run the application sample code given from the SMS provider its sending the message but when I copy the code into my project its not sending and show this error ,
I checked the whole code line by line but its not sending from my project ,
I need your help please how to solve this error
when I run the application I write bearer code , sender , receiver and SMS text then click send button
In this link sample code from SMS provider
CodePudding user response:
I think you could try adding the [DataContract] to your SendClass. https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-6.0#examples
CodePudding user response:
Thank you so much my friends adding the [DataContract] to SendClass solved the issue and SMS was sent.
This is the final SendClass :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace mamlaka_lab.BL
{
[DataContract]
class SendClass
{
private string _recipients = string.Empty;
private string _body = string.Empty;
private string _sender = string.Empty;
[DataMember()]
public string recipients
{
set { _recipients = value; }
get { return _recipients; }
}
[DataMember()]
public string body
{
set { _body = value; }
get { return _body; }
}
[DataMember()]
public string sender
{
set { _sender = value; }
get { return _sender; }
}
}
}
