Ciao a tutti
Ho creato un servizio WCF che restituisce un oggetto se non si verificano errori e un altro oggetto in caso di errore.
Solo che ho un problema sul come i dati sono serializzati da WCF.
Questo è il codice:
IXXXSoft
namespace XXXSoft
{
[ServiceContract]
public interface IXXXSoft
{
[OperationContract]
[ServiceKnownType(typeof(ConfigurationResponse))]
[ServiceKnownType(typeof(Errors))]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "Configuration")]
object Configuration(ConfigurationRequest xml);
}
}
XXXSoft.svc
namespace XXXSoft
{
public class XXXSoftService : IXXXSoft
{
public object Configuration(ConfigurationRequest xml)
{
tipoResult result = new tipoResult();
Errors errors = new Errors();
ConfigurationResponse response = new ConfigurationResponse();
if (result.Result)
{
result = Users.Autorizzato(new tipoUser() {
Username = xml.Username,
Password = xml.Password
});
errors.Error = errors.Error.Union(result.Messaggi).ToList();
if (result.Result)
{
//Return Configuration
}
}
if (errors.Error.Count > 0)
{
return errors;
}
return response;
}
}
}
Errors.cs
namespace XXXSoft
{
[DataContract]
public class Errors
{
public Errors()
{
Error = new List<string>();
}
[DataMember]
[XmlElementAttribute("Error", IsNullable = false)]
public List<string> Error
{
get;
set;
}
}
}
WebClient
HttpWebRequest req = null;
HttpWebResponse res = null;
try
{
const string url = "http://localhost:54920/XXXSoft.svc/Configuration";
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/xml; charset=utf-8";
req.Timeout = 30000;
req.Headers.Add("SOAPAction", url);
var xmlDoc = new XmlDocument { XmlResolver = null };
xmlDoc.Load(Server.MapPath("PostData.xml"));
string sXml = xmlDoc.InnerXml;
req.ContentLength = sXml.Length;
var sw = new StreamWriter(req.GetRequestStream());
sw.Write(sXml);
sw.Close();
res = (HttpWebResponse)req.GetResponse();
Stream responseStream = res.GetResponseStream();
var streamReader = new StreamReader(responseStream);
//Read the response into an xml document
var soapResonseXmlDocument = new XmlDocument();
soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
//return only the xml representing the response details (inner request)
string response = soapResonseXmlDocument.InnerXml;
//Response.Write(soapResonseXMLDocument.InnerXml);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
In caso di errore vorrei avere questo xml:
<Errors>
<Error>Errore1</Error>
<Error>Errore2</Error>
<Error>Errore3</Error>
</Errors>
Ma quello che ricevo come risposta nel webclient è questo:
<Errors xmlns=\"http://schemas.datacontract.org/2004/07/EricSoft\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
<Error xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
<a:string>Error1</a:string>
</Error>
<Error xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
<a:string>Error2</a:string>
</Error>
<Error xmlns:a=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
<a:string>Error3</a:string>
</Error>
</Errors>
Perchè ricevo questo?
Grazie