How to get Servicename from a plugin

Is there a recommend way to retrieve the servicename from within a plugin?

Currently only have the IAppHost interface passed in and the ServiceName is declared on the ServiceStackHost. Currently casting which works but wondering if I missing something.

private void MyPluginHandler(IAppHost host)
{
    var serviceName = ((ServiceStackHost)host).ServiceName;
}

Yeah you can do that, it will always work since all AppHost’s inherit from ServiceStackHost, you can also use the singleton:

HostContext.AppHost.ServiceName

Which itself is just a wrapper around:

ServiceStackHost.Instance.ServiceName

Good to know, thanks

Also there’s a shorter convenience property for ServiceName:

HostContext.ServiceName

Thanks again.

Is there a reliable method (self or iis hosted) to obtain the http://url:port/path that the AppHost is running on from the appHost.AfterInitCallbacks?

Currently just passing this in via the constructor to the apphost

public override void Configure(Container container)
{
    SetConfig(new HostConfig { WebHostUrl = selfhostUrlFieldSetViaConstructor });
    AfterInitCallbacks.Add(host =>
    {
        // how to get http://uri:port/path of service?
        var usingThis = host.Config.WebHostUrl;
        var path = host.Config.HandlerFactoryPath;
    });
}

No, the url where ServiceStack is called from is only available within the context of a HTTP request at runtime.

You can get the path where ServiceStack is mounted at Config.HandlerFactoryPath but not the url unless you explicitly specify it yourself with Config.WebHostUrl, but you wont be able to predict what the url is at StartUp.

For self-hosting you can get the prefixes where HttpListener is mounted at from the Listener.Prefixes collection but this doesn’t always equate to a url since it’s typically started with a wild card, e.g. http://*:8080 and you’ll only know what url your service gets called with at runtime.

Thanks, pretty much what I’d gotten from a SO post but wanted to check if there had been any changes since. So long as I’m not doing something daft! :smile: