Skip to content

Getting started with Web API using the pack does not work, part 2 #17

Description

@rickevry

The TestController-example on the home page does work. When you call http://localhost:8080/api/test you get an error:

An error has occurred.Type 'System.Dynamic.ExpandoObject' with data contract name 'ArrayOfKeyValueOfstringanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.System.Runtime.Serialization.SerializationException at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()


install ScriptCs.WebApi2

scriptcs -install ScriptCs.WebApi2
Installing packages...
Installed: ScriptCs.WebApi2
Package installation succeeded.
Saving packages in scriptcs_packages.config...
Creating scriptcs_packages.config...
Added Microsoft.AspNet.WebApi.Client (v5.2.0, .NET 4.5) to scriptcs_packages.config
Added Microsoft.AspNet.WebApi.Core (v5.2.0, .NET 4.5) to scriptcs_packages.config
Added Microsoft.AspNet.WebApi.Owin (v5.2.0, .NET 4.5) to scriptcs_packages.config
Added Microsoft.AspNet.WebApi.OwinSelfHost (v5.2.0) to scriptcs_packages.config
Added Microsoft.Bcl (v1.1.9, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Bcl.Build (v1.0.14, .NET 4.0) to scriptcs_packages.config
Added Microsoft.Net.Http (v2.2.22, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Owin (v2.0.2, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Owin.Host.HttpListener (v2.0.2, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Owin.Hosting (v2.0.2, .NET 4.5) to scriptcs_packages.config
Added Newtonsoft.Json (v6.0.2, .NET 4.5) to scriptcs_packages.config
Added Owin (v1.0, .NET 4.0) to scriptcs_packages.config
Added ScriptCs.Contracts (v0.10.0, .NET 4.5) to scriptcs_packages.config
Added ScriptCs.WebApi2 (v1.0.0, .NET 4.5) to scriptcs_packages.config
Successfully updated scriptcs_packages.config.


run script

scriptcs test1.cs -modules mono

Activity

glennblock commented on Mar 18, 2015

@glennblock
Contributor

I will take a look, thanks!
On Wed, Mar 18, 2015 at 7:27 AM RickEvry notifications@github.com wrote:

The TestController-example on the home page does work. When you call
http://localhost:8080/api/test you get an error:

An error has occurred.Type 'System.Dynamic.ExpandoObject' with data
contract name 'ArrayOfKeyValueOfstringanyType:
http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not
expected. Consider using a DataContractResolver or add any types not known
statically to the list of known types - for example, by using the
KnownTypeAttribute attribute or by adding them to the list of known types
passed to
DataContractSerializer.System.Runtime.Serialization.SerializationException
at
System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract
dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean
verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
at
System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract
dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle
originalDeclaredTypeHandle, Type graphType)
at
System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator
writer, Object graph, DataContractResolver dataContractResolver)
at
System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator
writer, Object graph, DataContractResolver dataContractResolver)
at
System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator
writer, Object graph, DataContractResolver dataContractResolver)
at
System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter
writer, Object graph)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type
type, Object value, Stream writeStream, HttpContent content)
at
System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type
type, Object value, Stream writeStream, HttpContent content,
TransportContext transportContext, CancellationToken cancellationToken)

--- End of stack trace from previous location where exception was thrown

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)
at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)
at

System.Web.Http.Owin.HttpMessageHandlerAdapter.d__13.MoveNext()

install ScriptCs.WebApi2

scriptcs -install ScriptCs.WebApi2
Installing packages...
Installed: ScriptCs.WebApi2
Package installation succeeded.
Saving packages in scriptcs_packages.config...
Creating scriptcs_packages.config...
Added Microsoft.AspNet.WebApi.Client (v5.2.0, .NET 4.5) to
scriptcs_packages.config
Added Microsoft.AspNet.WebApi.Core (v5.2.0, .NET 4.5) to
scriptcs_packages.config
Added Microsoft.AspNet.WebApi.Owin (v5.2.0, .NET 4.5) to
scriptcs_packages.config
Added Microsoft.AspNet.WebApi.OwinSelfHost (v5.2.0) to
scriptcs_packages.config
Added Microsoft.Bcl (v1.1.9, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Bcl.Build (v1.0.14, .NET 4.0) to scriptcs_packages.config
Added Microsoft.Net.Http (v2.2.22, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Owin (v2.0.2, .NET 4.5) to scriptcs_packages.config
Added Microsoft.Owin.Host.HttpListener (v2.0.2, .NET 4.5) to
scriptcs_packages.config
Added Microsoft.Owin.Hosting (v2.0.2, .NET 4.5) to scriptcs_packages.config
Added Newtonsoft.Json (v6.0.2, .NET 4.5) to scriptcs_packages.config
Added Owin (v1.0, .NET 4.0) to scriptcs_packages.config
Added ScriptCs.Contracts (v0.10.0, .NET 4.5) to scriptcs_packages.config
Added ScriptCs.WebApi2 (v1.0.0, .NET 4.5) to scriptcs_packages.config

Successfully updated scriptcs_packages.config.

run script

scriptcs test1.cs -modules mono

—
Reply to this email directly or view it on GitHub
#17.

tomsw commented on Jan 3, 2020

@tomsw

Calling UseJsonOnly before Configure fixes this for me:

var server = webapi.
	UseJsonOnly().
	Configure(typeof(TestController)).
	Start("http://localhost:8080");

In a real situation you will add your own formatters and the problem won't occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions