DotAmf
v1.0.4
Nugetパッケージ:Dotamf(Serializer)およびdotamf.wcf(サービスレイヤー)。
.NETシリアイザーおよびFLEXリモートサポートを備えたAMF用のWCFバインディング。
Action Message Format(AMF)は、ActionScriptオブジェクトやXMLなどのオブジェクトグラフをシリアル化するために使用されるバイナリ形式であるか、Adobe Flashクライアントとリモートサービス(通常はFlash Media Serverまたはサードパーティの代替)間でメッセージを送信します。 ActionScript 3言語は、AMF形式からエンコードおよびデコードするためのクラスを提供します。
http://en.wikipedia.org/wiki/action_message_format
AMFには、オブジェクトグラフを維持する機能、同じオブジェクト(文字列を含む)のセットまたは同じオブジェクトへの参照を保存する際の小さなフットプリント、幅広いネイティブにサポートされているタイプ( XmlDocument 、 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);