WCF中的KnownType异常

时间:2014-07-18 19:41:05

标签: c# wcf

情景:

我需要将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);
    }
  }

我收到错误:

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);
   }

1 个答案:

答案 0 :(得分:0)

从错误中看,您试图通过将报告控件分配给类型控制器的DataMember来通过wcf传递ReportController。为此,您需要将以下内容添加到Controller类的定义位置:

[KnownType(ReportController)]
class Controller...

如果Controller是内置的.Net类型,这将无效......