我需要专家的帮助。我有一个名为MyException的自定义类。这个类的目的是用自定义信息记录异常。这个类的定义如下所示:WCF和Silverlight之间的序列化
[DataContract]
public class MyException
{
[DataMember]
public string StackTrace { get; set; }
[DataMember]
public string Message { get; set; }
[DataMember]
public string Component { get; set; }
[DataMember]
public string TypeName { get; set; }
[DataMember]
public string Miscellaneous { get; set; }
public MyException()
{}
public MyException(string message)
{
this.Message = message;
}
public MyException(string message, string stackTrace)
{
this.Message = message;
this.StackTrace = stackTrace;
}
}
我有打算接受JSON格式的MyException,写它的内容与数据库的WCF服务。由于我打算跟踪的信息量很大,因此我需要使用POST操作,因此我决定以我的实现为基础执行off this blog post。我的服务描述可以在这里找到:
[OperationContract]
[WebInvoke(UriTemplate="/LogError", Method="POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string LogError(Stream stream)
{
try
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(MyException));
MyException exception = (MyException)(serializer.ReadObject(stream));
// Write the exception details to the database
}
catch (Exception ex)
{
// Write the exception details to the database
}
}
[OperationContract]
public void Test(MyException exception)
{ }
我添加了“测试”的操作,以便将MyException代理的生成过程中会接触到我的Silverlight应用程序。我的Silverlight应用程序尝试使用下面的代码发布到LOGERROR:
MyServiceProxy.MyException exception = new MyServiceProxy.MyException();
exception.Message = e.Error.Message;
exception.StackTrace = e.Error.StackTrace;
exception.Component = GetComponentName();
exception.TypeName = e.Error.FullName;
string json = string.Empty;
using (MemoryStream stream = new MemoryStream())
{
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(MyServiceProxy.MyException));
serializer.WriteObject(stream, exception);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
json = reader.ReadToEnd();
}
Uri uri = new Uri("/myService.svc/LogError", UriKind.Absolute);
WebClient myService = new WebClient();
myService.UploadStringCompleted +=
new UploadStringCompletedEventHandler(myService_UploadStringCompleted);
myService.Headers["Content-type"] = "application/x-www-form-urlencoded";
myService.Encoding = Encoding.UTF8;
myService.UploadStringAsync(uri, "POST", json);
}
当我运行这段代码,我得到了我的Silverlight应用程序的错误,即:数据合同名称' “类型‘MyServiceProxy.MyException’ MyException:http://schemas.datacontract.org/2004/07/MyNamespace'不需要将未知的静态类型添加到已知类型的列表中 - 例如,使用KnownTypeAttribute属性或将它们添加到列表中传递给DataContractSerializer的已知类型“。
我在做什么错?
为什么你重新发明轮子,你不从System.Exception派生你的自定义异常! – 2011-02-08 14:56:30
@Davide Piras:.NET异常对于WCF通信来说不是很好 - WCF通讯应该是可互操作的,并且应该只使用SOAP错误(通常伴随一个自定义类来保存更详细的错误信息) – 2011-02-08 15:13:56