本文整理汇总了C#中ObjectContent.ReadAsStringAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectContent.ReadAsStringAsync方法的具体用法?C# ObjectContent.ReadAsStringAsync怎么用?C# ObjectContent.ReadAsStringAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectContent
的用法示例。
在下文中一共展示了ObjectContent.ReadAsStringAsync方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMethod1
public void TestMethod1()
{
try
{
Task serviceTask = new Task();
serviceTask.TaskId = 1;
serviceTask.Subject = "Test Task";
serviceTask.StartDate = DateTime.Now;
serviceTask.DueDate = null;
serviceTask.CompletedDate = DateTime.Now;
serviceTask.Status = new Status
{
StatusId = 3,
Name = "Completed",
Ordinal = 2
};
serviceTask.Links = new System.Collections.Generic.List<Link>();
serviceTask.Assignees = new System.Collections.Generic.List<User>();
serviceTask.SetShouldSerializeAssignees(true);
var formatter = new JsonMediaTypeFormatter();
var content = new ObjectContent<Task>(serviceTask, formatter);
var reuslt = content.ReadAsStringAsync().Result;
}
catch(Exception ex)
{
Assert.Fail(ex.Message);
}
}
示例2: RoundTripUsingJsonNet
public void RoundTripUsingJsonNet()
{
//Arrange
var formatters = new MediaTypeFormatterCollection() {new JsonNetFormatter()};
var fromContact = new Contact { FirstName = "Brad", LastName = "Abrams" };
// Act
var fromContent = new ObjectContent<Contact>(fromContact, new MediaTypeHeaderValue("application/json"), formatters);
var contentString = fromContent.ReadAsStringAsync().Result;
var toContent = new ObjectContent<Contact>(new StringContent(contentString), formatters);
toContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var toContact = toContent.ReadAsAsync<Contact>().Result;
//Arrange
Assert.AreEqual(fromContact.FirstName, toContact.FirstName);
Assert.AreEqual(fromContact.LastName, toContact.LastName);
}
示例3: PrimitiveTypesSerializeAsOData
public void PrimitiveTypesSerializeAsOData(string typeString, Type valueType, object value)
{
string expected = BaselineResource.ResourceManager.GetString(typeString);
Assert.NotNull(expected);
ODataConventionModelBuilder model = new ODataConventionModelBuilder();
model.EntitySet<WorkItem>("WorkItems");
HttpConfiguration configuration = new HttpConfiguration();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID");
request.Properties[HttpPropertyKeys.HttpConfigurationKey] = configuration;
ODataMediaTypeFormatter formatter = new ODataMediaTypeFormatter(model.GetEdmModel()) { Request = request };
Type type = (value != null) ? value.GetType() : typeof(Nullable<int>);
ObjectContent content = new ObjectContent(type, value, formatter);
var result = content.ReadAsStringAsync().Result;
Assert.Xml.Equal(result, expected);
}
示例4: PrimitiveTypesSerializeAsOData
public void PrimitiveTypesSerializeAsOData(Type valueType, object value, MediaTypeHeaderValue mediaType,
string resourceName)
{
string expectedEntity = Resources.GetString(resourceName);
Assert.NotNull(expectedEntity);
ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<WorkItem>("WorkItems");
IEdmModel model = modelBuilder.GetEdmModel();
string actualEntity;
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
"http://localhost/WorkItems(10)/ID"))
{
request.SetConfiguration(configuration);
IEdmProperty property =
model.EntityContainer.EntitySets().Single().EntityType().Properties().First();
request.ODataProperties().Model = model;
request.ODataProperties().Path = new ODataPath(new PropertyAccessPathSegment(property));
request.SetFakeODataRouteName();
ODataMediaTypeFormatter formatter = CreateFormatter(request);
formatter.SupportedMediaTypes.Add(mediaType);
Type type = (value != null) ? value.GetType() : typeof(Nullable<int>);
using (ObjectContent content = new ObjectContent(type, value, formatter))
{
actualEntity = content.ReadAsStringAsync().Result;
}
}
bool isJson = resourceName.EndsWith(".json");
if (isJson)
{
JsonAssert.Equal(expectedEntity, actualEntity);
}
else
{
Assert.Xml.Equal(expectedEntity, actualEntity);
}
}
示例5: SpecifyMediaTypeAndSerializeAsPlainTextWithACustomFormatter
public void SpecifyMediaTypeAndSerializeAsPlainTextWithACustomFormatter()
{
//Arrange
var content = new ObjectContent<string>("woof", new MediaTypeHeaderValue("text/plain"), new List<MediaTypeFormatter> { new PlainTextFormatter() });
var request = new HttpRequestMessage();
//Act
var result = content.ReadAsStringAsync().Result;
var mediaType = content.Headers.ContentType.MediaType;
//Assert
Assert.AreEqual("text/plain", mediaType);
}
示例6: SpecifyMediaTypeAndSerializeAsJson
public void SpecifyMediaTypeAndSerializeAsJson()
{
//Arrange
var input = new foo { Bar = "hello" };
var content = new ObjectContent<foo>(input, "application/json");
//Act
var result = content.ReadAsStringAsync().Result;
var mediaType = content.Headers.ContentType.MediaType;
//Assert
Assert.AreEqual("application/json", mediaType);
}
示例7: SpecifyMediaTypeAndSerialize
public void SpecifyMediaTypeAndSerialize()
{
//Arrange
var content = new ObjectContent<string>("woof", "text/plain");
// This will cause the test to pass: content.Formatters.Add(new PlainTextFormatter());
//Act
var result = content.ReadAsStringAsync().Result; // Why does this change the media type of my content?
var mediaType = content.Headers.ContentType.MediaType;
//Assert
Assert.AreEqual("text/plain", mediaType); // This fails because it falls back the XmlFormatter
}
示例8: Encrypt
public HttpResponseMessage Encrypt(HttpResponseMessage plainResponse)
{
if (plainResponse == null)
{
throw new ArgumentNullException("plainResponse");
}
var plainContent = plainResponse.Content;
if (plainContent == null)
{
Trace.TraceWarning("Content of response is NULL, replace to ALTERNATIVES!");
plainContent = DefaultHttpMessageCryptoService.EmptyContentAlternatives;
plainResponse.StatusCode = HttpStatusCode.OK;
}
var plain = plainContent.ReadAsByteArrayAsync().Result;
var timestamp = this.lastTimestamp;
byte[] cipher;
string signature;
this.Encrypt(plain, timestamp, out cipher, out signature);
var cipherModel = this.CreateCipherModel(timestamp, cipher, signature);
var cipherContent = new ObjectContent(cipherModel.GetType(), cipherModel, DefaultHttpMessageCryptoService.DefaultFormatter);
Trace.TraceInformation("Encrypted response content: {0}", cipherContent.ReadAsStringAsync().Result);
plainResponse.Content = cipherContent;
plainResponse.Content.Headers.ContentType = plainContent.Headers.ContentType;
return plainResponse;
}