Pacotes Nuget: DOTAMF (Serializer) e Dotamf.wcf (camada de serviço).
.NET Serializer e WCF Bindings para AMF com suporte completo de remoção flexível.
Formato de mensagem de ação (AMF) é um formato binário usado para serializar gráficos de objetos, como objetos ActionScript e XML, ou enviar mensagens entre um cliente flash da Adobe e um serviço remoto, geralmente um servidor de mídia flash ou alternativas de terceiros. O idioma ActionScript 3 fornece classes para codificação e decodificação do formato AMF.
http://en.wikipedia.org/wiki/Action_Message_Format
A AMF tem várias vantagens sobre os formatos de serialização existentes, incluindo a capacidade de manter o gráfico de objetos, uma pequena pegada ao armazenar um conjunto de objetos idênticos (incluindo seqüências de caracteres) ou referências ao mesmo objeto e uma ampla gama de tipos nativamente suportados (como XmlDocument , DateTime e Dictionary ).
Considere os seguintes contratos de serviço:
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Xml;
namespace ExampleService
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
ProductVo[] GetAllProducts();
[OperationContract(Name = "GetUser")] //Custom procedure name
User GetUserDataContract(int id);
[OperationContract]
int AddUser(User user);
[OperationContract]
Content SendContent(Content content);
[OperationContract]
User[] SendGraph(User[] users);
[OperationContract]
void DoStuff();
[OperationContract]
[FaultContract(typeof(CustomFault))]
void DoFault();
}
[DataContract] //Will have the "ExampleService.User" alias
public class User
{
[DataMember(Name = "id")] //Custom field name
public int Id { get; set; }
[DataMember(Name = "is_active")]
public bool IsActive { get; set; }
[DataMember] //Use explicit name
public string name { get; set; }
[DataMember(Name = "products")]
public ProductVo[] Products { get; set; }
}
[DataContract(Name = "Product")] //Custom alias
public class ProductVo
{
[DataMember(Name = "id")]
public int Id { get; set; }
}
[DataContract]
public class CustomFault
{
[DataMember(Name = "date")]
public DateTime Date { get; set; }
[DataMember(Name = "message")]
public string Message { get; set; }
}
[DataContract]
public class Content
{
[DataMember(Name = "data")]
public byte[] Data { get; set; }
[DataMember(Name = "xml")]
public XmlDocument Xml { get; set; }
}
}
Para adicionar um suporte à AMF a um serviço, você só precisa atualizar sua configuração:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="amfBehaviorExtension" type="DotAmf.ServiceModel.Configuration.AmfBehaviorExtensionElement, DotAmf.Wcf"/>
</behaviorExtensions>
<bindingElementExtensions>
<add name="amfBindingExtension" type="DotAmf.ServiceModel.Configuration.AmfBindingExtensionElement, DotAmf.Wcf"/>
</bindingElementExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="amfEndpoint">
<amfBehaviorExtension/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="amfServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="amfBinding">
<amfBindingExtension/>
<httpTransport/>
</binding>
</customBinding>
</bindings>
<services>
<service name="ExampleService.MyService" behaviorConfiguration="amfServiceBehavior">
<endpoint address="" contract="ExampleService.IMyService" binding="customBinding" bindingConfiguration="amfBinding" behaviorConfiguration="amfEndpoint"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="false"/>
</system.webServer>
</configuration>
Consulte a pasta Examples para obter um serviço completo e implementações do cliente.
Você pode usar DataContractAmfSerializer para serializar e desserializar a partir de dados binários da AMF fora do WCF.
CustomType objectToWrite;
var knownTypes = new[] {
typeof(KnownType1),
typeof(KnownType2)
};
var serializer = new DataContractAmfSerializer(typeof(CustomType), knownTypes);
using (var stream = new MemoryStream())
serializer.WriteObject(stream, objectToWrite);