WCF中的KnownType异常

本文关键字:异常 KnownType 中的 WCF | 更新日期: 2023-09-27 18:13:29

场景:

我需要将ControllerContext对象从我的ASP.NET MVC Controllers作为参数传递给WCF方法。

我的参数类:

[MessageContract]
public class FileParameter
{
    [MessageHeader(MustUnderstand = true)]
    public ControllerContext Context { get; set; }
   [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }
}

调用控制器中的方法:

 public class ReportController : Controller
{
public ActionResult GeneratePdf()
    {
        var file = new FileParameter();
        {
           Context = this.ControllerContext;
           FileName = "test"
        };
       TestProxy.GetInstance().GetPdfByte(file);
    }
  }

i get the error:

Unhandled Exception: System.Runtime.Serialization.SerializationException: Type
'App.Web.Controllers.ReportController' with data contract name 
'Dog:http://schemas.datacontract.org/2004/07/Serialization' is not expected. 
Consider using a DataContractResolver 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 DataContractSerializer.

有什么问题吗?它理解ControllerContext必须是通用的,WCF将接受ReportController上下文作为ControllerContext

编辑Wcf方法:

   public byte[] GetBytes(FileParameterfile file)
   {
      var rotativa = new Rotativa.ActionAsPdf("Index");
     return rotativa.BuildPdf(file.Context);
   }

WCF中的KnownType异常

从错误来看,您试图通过将ReportController分配给类型为Controller的datammember来传递它。为了做到这一点,您需要像这样在Controller类的定义位置添加以下内容:

[KnownType(ReportController)]
class Controller...

如果Controller是一个内建的。net类型,这将不能工作…