我们目前有一种在测试页面中调用我们的Web服务的机制。但是,我试图查看WCF服务的更好实践并使用FaultException。
因此有些情况下我们的服务抛出了FaultException,我想将错误序列化为xml并显示在页面上。
到目前为止,我查看了XmlSerializer
和DataContractSerializer
。
所以考虑一下代码:
public SomeResponse DoSomething()
{
throw new FaultException<AuthenticationFault>(
new AuthenticationFault(),
new FaultReason("BooHoo"),
new FaultCode("1234"));
}
无序尝试序列化:
public static string Serialize(object obj)
{
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
public string Serialize<TObject>(TObject obj)
{
if (obj == null)
{
return string.Empty;
}
XmlSerializer serializer = new XmlSerializer(obj.GetType());
XmlWriterSettings settings = new XmlWriterSettings()
{
Encoding = new UnicodeEncoding(false, false), string
Indent = true,
OmitXmlDeclaration = true
};
using (StringWriter textWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
{
serializer.Serialize(xmlWriter, obj);
}
return textWriter.ToString();
}
}
public override string Invoke(string request)
{
try
{
var service = new AcmeService();
return Serialize(service.DoSomething());
}
catch (FaultException ex)
{
return Serialize(ex);
}
}
[DataContract]
public class AuthenticationFault
{
}
以上场景中引发了以下异常。但是,我很欣赏通用FaultException没有无参数构造函数。运行时必须能够通过线路进行序列化。
System.Runtime.Serialization.SerializationException occurred
HResult=0x8013150C
Message=Type 'BuyerAcmeApp.Services.Faults.AuthenticationFault' with data contract name 'AuthenticationFault:http://schemas.acme.com/p4/services/2017/11' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
System.InvalidOperationException occurred
HResult=0x80131509
Message=There was an error reflecting type 'System.ServiceModel.FaultException'.
Source=App_Code.etbsvkgf
StackTrace:
at AcmeApp.WebServices.Tests.WebServiceMethodItem`2.Serialize[TObject](TObject obj) in T:\acmejpa\AcmeAppTemplate\dev\solution\AcmeApp.Template\src\AcmeApp\App_Code\WebServices\Tests\WebServiceMethodItemBase.cs:line 70
at AcmeApp.WebServices.Tests.WebServiceMethodItemWithError`2.Invoke(String request) in T:\acmejpa\AcmeAppTemplate\dev\solution\AcmeApp.Template\src\AcmeApp\App_Code\WebServices\Tests\WebServiceMethodItemBase.cs:line 104
at AcmeApp.Diagnostics.WebServiceTestPage.CallMethodButton_OnClick(Object sender, EventArgs e) in T:\acmejpa\AcmeAppTemplate\dev\solution\AcmeApp.Template\src\AcmeApp\Diagnostics\WebServiceTestPage.aspx.cs:line 44
Inner Exception 1:
NotSupportedException: Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary.
答案 0 :(得分:1)
序列化时,需要将已知类型提供给DataContractSerializer
。
public static string Serialize(object obj)
{
var settings = new XmlWriterSettings { Indent = true };
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(
obj.GetType(), new Type[]
{
typeof(AuthenticationFault)
});
serializer.WriteObject(memoryStream , obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}