Search code examples
.netweb-servicesservicestack

Hide web method services in webconfig or in deployment


I have a .net webservice solution with several webmethods exposed. I need to deliver two deployments with this solution. One with all the methods exposed and other with some of those methods hidden.

Is there a way to hide certain methods easily so when I deploy them I can choose which ones to hide.

Maybe in webconfig or maybe some compilation/deployment options Thanks


Solution

  • You can use the Restrict Attribute to both limit and hide Services in different scenarios, e.g you can hide Services from being visible by external Users by annotating your Request DTO's with:

    [Restrict(VisibleInternalOnly = true)]
    public class InternalAdmin { }
    

    The [Exclude] attribute is an alternative attribute to hide Services from everyone and appearing in Metadata Services:

    [Exclude(Feature.Metadata)]
    public class Hidden {}
    

    As this is a popular scenario you can also use [ExcludeMetadata] instead.

    So you could use #def's to create different builds with and without these attributes applied.

    Otherwise these attributes can also be dynamically added at runtime using the dynamic attribute API, e.g:

    typeof(Hidden)
        .AddAttributes(new ExcludeAttribute(Feature.Metadata));
    
    new AppHost().Init();
    

    Note attributes need to be added before AppHost.Configure() is run, so either in your AppHost constructor or before your AppHost is initialized as seen above.