這裏的代碼示例的情況下,以上鍊接死亡
public class MerlinStringMediaTypeFormatter : MediaTypeFormatter
{
public MerlinStringMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
public override bool CanReadType(Type type)
{
return type == typeof (YourObject); //can it deserialize
}
public override bool CanWriteType(Type type)
{
return type == typeof (YourObject); //can it serialize
}
public override Task<object> ReadFromStreamAsync(
Type type,
Stream readStream,
HttpContent content,
IFormatterLogger formatterLogger)
{
//Here you put deserialization mechanism
return Task<object>.Factory.StartNew(() => content.ReadAsStringAsync().Result);
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
//Here you would put serialization mechanism
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
}
然後,你需要在Global.asax
註冊您的格式
protected void Application_Start()
{
config.Formatters.Add(new MerlinStringMediaTypeFormatter());
}
希望這可以爲您節省一些時間。
實現['ISerializable'](http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.iserializable.aspx)? –
嗯......沒有那麼像WCF的做事方式。 – Alwyn