Shyam
Shyam

Reputation: 179

Servicestack - get metadata programmatically

Is there a way to get the metadata of a website built using Servicestack framework programmatically? We're looking to build an app that will look for a Servicestack website hosted on 2 different environments and find out if there are any differences between them. Here are the metadata details that we expect to read from the Servicestack website in the app programmatically:

Upvotes: 2

Views: 295

Answers (1)

mythz
mythz

Reputation: 143319

Yes you can access most of ServiceStack's metadata from Metadata property in your AppHost which returns the ServiceMetadata class. When outside your AppHost you can access it from the HostContext.Metadata singleton.

A lot of the information can be accessed from OperationsMap, e.g:

var allServices = HostContext.Metadata.OperationsMap.Values.ToList();

Which will return a List<Operation> containing most of the metadata about a Service, e.g:

  • Routes for each operation are available from the Routes property
  • The RequestType and ResponseType returns the Request DTO Type and the known Response DTO Type

All Request DTOs are DTOs/complex types. The way to access information about each Type is to use C# Reflection methods. You can use the Type.GetSerializableProperties() extension method to get the serializable PropertyInfo[] of a Type.

Note you can also query a lot of this metadata in real-time using the Metadata Debug Template, enabled when you register the TemplatePagesFeature plugin:

Plugins.Add(new TemplatePagesFeature { 
    EnableDebugTemplate = true,       // viewable by Admin users
    //EnableDebugTemplateToAll = true // viewable by all users
})

Where you can query ServiceStack metadata of your Services in real-time using ServiceStack Templates:

enter image description here

Upvotes: 1

Related Questions