我正在尝试将 BitmapSource 从我的 WCF 服务发送到我的客户端应用程序。两者都是 .NET 3.5 应用程序。我似乎能够毫无问题地序列化对象(没有异常发生),但我在客户端上的回调永远不会被调用。在我的客户端上添加跟踪以记录异常后,我看到 DataContractSerializer 无法反序列化 BitmapSource。
错误是:'元素'http://schemas.datacontract.org/2004/07/Test.VisionStudio.Remote:Image'包含'http://schemas.datacontract.org/2004/07/System 的数据。 Windows.Interop:InteropBitmap' 数据协定。反序列化器不知道映射到该合约的任何类型。将与“InteropBitmap”对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给 DataContractSerializer 的已知类型列表中。
所以,我按照它所说的做了,并将已知类型添加到服务合同和回调合同中,但我仍然遇到同样的错误。
这是我正在发送的服务回调合同和数据合同(删除不相关的 DataMembers):
[ServiceKnownType(typeof(BitmapSource))]
[ServiceKnownType(typeof(InteropBitmap))]
public interface IZynVisionStudioRemoteServiceCallback
{
[OperationContract(IsOneWay=true)]
void OnVisionResultsReady(RemoteVisionProductResult result);
[OperationContract(IsOneWay = true)]
void ServiceShuttingDown();
}
[DataContract]
public class RemoteVisionProductResult
{
private bool _status;
/// <summary>
/// Status of the overall inspection. Pass = True, Fail = False
/// </summary>
[DataMember]
public bool Status
{
get { return _status; }
set { _status = value; }
}
private string _product;
/// <summary>
/// Name of the product the results came from
/// </summary>
[DataMember]
public string Product
{
get { return _product; }
set { _product = value; }
}
private string _inspection;
/// <summary>
/// Name of the inspection the results came from
/// </summary>
[DataMember]
public string Inspection
{
get { return _inspection; }
set { _inspection = value; }
}
private List<RemoteVisionImage> _images = new List<RemoteVisionImage>();
[DataMember]
public List<RemoteVisionImage> Images
{
get { return _images; }
set { _images = value; }
}
private List<RemoteVisionImage> _imagesWithGraphics = new List<RemoteVisionImage>();
[DataMember]
public List<RemoteVisionImage> ImagesWithGraphics
{
get { return _imagesWithGraphics; }
set { _imagesWithGraphics = value; }
}
}
[DataContract]
public class RemoteVisionImage
{
/// <summary>
/// Image uploaded from the vision system
/// </summary>
[DataMember]
public BitmapSource Image { get; set; }
/// <summary>
/// Name of the camera the image was uploaded from
/// </summary>
[DataMember]
public string Camera { get; set; }
public RemoteVisionImage()
{
}
}
谁能帮我弄清楚为什么我不能通过网络发送这张图片?
编辑:我应该添加,我的客户端按预期更新服务引用,并与服务共享程序集,因此 RemoteVisionProductResult 和 RemoteVisionImage 的实现也在客户端上。