WCF Message Contract and Streaming

自闭症网瘾萝莉.ら 提交于 2019-12-08 10:20:07

问题


I'm going boarder line crazy, I have been working with this for over a day and still have no idea why it doesn't work,

I have a MessageContract that I'm using to send out a stream, but I get the following error,

Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected. 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.

[ServiceContract()]
public interface IContentService
{
    [OperationContract(), FaultContract(typeof(ContentFault))]
    PublishItemResponse PublishFile(PublishFileRequest request);
}


[MessageContract()]
public class PublishFileRequest
{ 
 [MessageHeader()]
 public FileInventoryItem Item {get;set;}

 [MessageHeader()]
 public Request Request  {get;set;}

 [MessageBodyMember()]
 public Stream FileContent {get;set;}
}



 [MessageContract()]
 public class Request
 {
  [MessageHeader()]
  public Guid AuthorizationToken { get; set; }

  [MessageHeader()]
  public string CoreVersion  { get; set; }

  [MessageHeader()]
  public string Password { get; set; }

  [MessageHeader()]
  public DateTime RequestTime { get; set; }

  [MessageHeader()]
  public string ComponentVersion { get; set; }

  [MessageHeader()]
  public string UserName  { get; set; }
 }



[MessageContract()]
[Serializable()]
public class FileInventoryItem : InventoryItemBase
{
 public Stream FileContent { get; set;}
}



[MessageContract()]
[KnownType(typeof(FileInventoryItem))]
[KnownType(typeof(FolderInventoryItem))]
[Serializable()]
public abstract class InventoryItemBase
{
 public List<string> Errors {get;set;}

 public List<string> Warnings  {get;set;}

 [MessageHeader()]
 public StagingAction Action {get;set;}

 [MessageHeader()]
 public string ContentXml  {get;set;}

 [MessageHeader()]
 public int ItemId {get;set;}

 [MessageHeader()]
 public ItemType ItemType { {get;set;}

 [MessageHeader()]
 public string Name  {get;set;}

 [MessageHeader()]
 public int ParentId {get;set;}

 [MessageHeader()]
 public Guid ParentUniqueId  {get;set;}

 [MessageHeader()]
 public Guid UniqueId  {get;set;}

 [MessageHeader()]
 public Guid Version  {get;set;}
}

Any help is greatly appropriated,


回答1:


WCF requires the types that are serialized to exactly match the types that are declared in the contract. You can get around that by adding the KnownType attribute to indicate that you know a particular sub type is going to be used (in this case you would add it to the PublishFileRequest class).

However, while that will eliminate the first error, your code will still not work, since FileStreams are not serializable.




回答2:


The FileStream object points to the filesystem, which cannot be accessed from another computer.

Use a MemoryStream instead to transfer the data. You can use Stream.CopyTo(memoryStream) to copy the data to the MemoryStream object.



来源:https://stackoverflow.com/questions/3208866/wcf-message-contract-and-streaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!