NativeTypes feature returning (401) Unauthorized error when Adding ServiceStack Reference

I am currently working on plans to update an intranet SPA that use the AspNetWindowsAuthProvider for authentication from Javascript to TypeScript. One feature that I would love to use in this update is the NativeTypes. Unfortunately, I have run into a couple of snags.

The first issue is with Add ServiceStack Reference. Once I have entered the Address and Name and clicked the OK button, the following error is returned:

Failed to generated client types, server responded with ‘The remote server returned an error: (401) Unauthorized.’.

When calling the NativeType Routes (i.e. /types & /types/typescript) from the browser, everything returns as expected.

Any advice on how to get this to function properly? My Visual Studio is running with the same account as everything else.


The second issue is that I am receiving a logged error when I initialize the NativeTypes plugin. Here is the text from the exception:

An error occurred in the application: Applicaton
Exception: ERROR: ServiceBase::Service Exception

Message: Could not register Request ‘ServiceStack.NativeTypes.TypeLinks’ with service ‘ServiceStack.NativeTypes.NativeTypesService’ as it has already been assigned to another service.
Each Request DTO can only be handled by 1 service.

Source: ServiceStack

Target site: Void AddToRequestExecMap(System.Type, System.Type, ServiceStack.Host.ServiceExecFn)

Stack trace: at ServiceStack.Host.ServiceController.AddToRequestExecMap(Type requestType, Type serviceType, ServiceExecFn handlerFn)
at ServiceStack.Host.ServiceController.RegisterService(ITypeFactory serviceFactoryFn, Type serviceType)
at ServiceStack.Host.ServiceController.RegisterService(Type serviceType)

It appears that it’s attempting to register some Type routes multiple times.


IIS Authentication Settings

If you’re getting a 401 this is likely an error before reaching ServiceStack. You can check this by going to the /types/typescript route directly and looking at the Response Headers to see if it has X-Powered-By:ServiceStack/4.050 Win32NT/.NET Response Header or not, if it doesn’t it’s likely some configuration you have in IIS/ASP.NET that’s returning the 401.

The NativeTypesFeature is already registered by default so you can’t register it again, you can instead get the feature with:

var feature = this.GetPlugin<NativeTypesFeature>();

If you are using VS 2013 or 2015 and the application is hosting your services and you SPA like the templates in ServiceStackVS, you can add a TypeScript reference whilst running your application locally. That way you aren’t generating off potentially out of date DTOs for local development. For example, If you’ve made any changes locally not yet deployed, you can easily make service/DTO changes, build/run and update local TypeScript references.

Thanks @mythz! I have resolved both issues.

First the first issue, IIS was setup to require Windows Authentication to the site and the Add ServiceStack Reference does not pass any credentials for the request, the system would typically prompt for a username and password to provide the site, but unfortunately, Visual Studio does not respond the same way as a browser nor does it pass the current credentials.

To solve the issue I added the following to my Web.config so that all requests to the /types route will succeed:

<configuration>
    ...
    <location path="types">
        <system.web>
            <authorization>
                <allow users="?" />
            </authorization>
        </system.web>
    </location>
    ... 
</configuration>

For the second issue, you were correct, I was registering the NativeTypes PlugIn a second time. I wasn’t aware that this feature was enabled by default.

Thanks for your help!