DotAmf
v1.0.4
Nuget軟件包:dotAMF(序列化器)和dotamf.wcf(服務層)。
.NET序列化器和WCF綁定AMF具有完整的Flex遠程支持。
操作消息格式(AMF)是一種二進制格式,用於序列化對像圖,例如ActionScript對象和XML,或在Adobe Flash客戶端和遠程服務之間發送消息,通常是Flash Media Server或第三方替代方案。 ActionScript 3語言提供了用於從AMF格式編碼和解碼的類。
http://en.wikipedia.org/wiki/action_message_format
AMF比現有的序列化格式具有多個優點,包括維護對像圖的能力,在存儲一組相同對象(包括字符串)或引用對同一對象的相同對象時的佔地面積,以及廣泛的內在支持類型(例如XmlDocument ,dateTime, DateTime和Dictionary )。
考慮以下服務合同:
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; }
}
}
要向服務添加AMF支持,您只需要更新您的配置:
<?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>
有關完整的服務和客戶端實現,請參見Examples文件夾。
您可以使用DataContractAmfSerializer從WCF之外的二進制AMF數據序列化並進行序列化。
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);