Created
January 6, 2015 03:25
-
-
Save ODataTeam/88f325a3f622eaf1205e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
EdmEntityType customer = new EdmEntityType("ns", "customer"); | |
var key = customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); | |
customer.AddKeys(key); | |
customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String); | |
ODataEntry entry1 = new ODataEntry() | |
{ | |
Properties = new[] | |
{ | |
new ODataProperty(){Name = "Id", Value = 51}, | |
new ODataProperty(){Name = "Name", Value = "Name_A"}, | |
} | |
}; | |
ODataEntry entry2 = new ODataEntry() | |
{ | |
Properties = new[] | |
{ | |
new ODataProperty(){Name = "Id", Value = 52}, | |
new ODataProperty(){Name = "Name", Value = "Name_B"}, | |
} | |
}; | |
var stream = new MemoryStream(); | |
var message = new Message { Stream = stream }; | |
// Set Content-Type header value | |
message.SetHeader("Content-Type", "text/csv"); | |
var settings = new ODataMessageWriterSettings | |
{ | |
// Set our resolver here. | |
MediaTypeResolver = CsvMediaTypeResolver.Instance, | |
DisableMessageStreamDisposal = true, | |
}; | |
using (var messageWriter = new ODataMessageWriter(message, settings)) | |
{ | |
var writer = messageWriter.CreateODataFeedWriter(null, customer); | |
writer.WriteStart(new ODataFeed()); | |
writer.WriteStart(entry1); | |
writer.WriteEnd(); | |
writer.WriteStart(entry2); | |
writer.WriteEnd(); | |
writer.WriteEnd(); | |
writer.Flush(); | |
} | |
stream.Seek(0, SeekOrigin.Begin); | |
string msg; | |
using (var sr = new StreamReader(stream)) { msg = sr.ReadToEnd(); } | |
Console.WriteLine(msg); | |
} | |
} | |
internal class Message : IODataResponseMessage, IDisposable | |
{ | |
private readonly Dictionary<string, string> headers = new Dictionary<string, string>(); | |
public IEnumerable<KeyValuePair<string, string>> Headers { get { return this.headers; } } | |
public int StatusCode { get; set; } | |
public Uri Url { get; set; } | |
public string Method { get; set; } | |
public Stream Stream { get; set; } | |
public Action DisposeAction { get; set; } | |
public string GetHeader(string headerName) | |
{ | |
string headerValue; | |
return this.headers.TryGetValue(headerName, out headerValue) ? headerValue : null; | |
} | |
public void SetHeader(string headerName, string headerValue) | |
{ | |
headers[headerName] = headerValue; | |
} | |
public Stream GetStream() | |
{ | |
return this.Stream; | |
} | |
void IDisposable.Dispose() | |
{ | |
if (this.DisposeAction != null) | |
{ | |
this.DisposeAction(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment