Question

I have a problem with user registration servicestack 4 feature.

My code is exactly same as code written in ServiceStack wiki.

public class AppHost : AppHostBase
        {
           public AppHost() : base("Hello Web Services", typeof(HelloService).Assembly { }

            public override void Configure(Funq.Container container)
            {
                Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                  new IAuthProvider[] {
                    new BasicAuthProvider()
                  }));

                Plugins.Add(new RegistrationFeature());

                container.Register<ICacheClient>(new MemoryCacheClient());
                var userRep = new InMemoryAuthRepository();
                container.Register<IUserAuthRepository>(userRep);
            }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            new AppHost().Init();
        }

POST /register with

{"username" : "user1", "password" : "user1Pwd"} 

returns

{
    "ResponseStatus": {
        "ErrorCode": "NullReferenceException",
        "Message": "Object reference not set to an instance of an object.",
        "StackTrace": "[Register: 23.12.2013 11:17:44]:\n[REQUEST: {UserName:user1,Password:user1Pwd}]\nSystem.NullReferenceException: Object reference not set to an instance of an object.\r\n   в ServiceStack.Auth.RegistrationValidator.<.ctor>b__3(String x)\r\n   в ServiceStack.FluentValidation.DefaultValidatorExtensions.<>c__DisplayClass1`2.<Must>b__0(T x, TProperty val)\r\n   в ServiceStack.FluentValidation.DefaultValidatorExtensions.<>c__DisplayClass4`2.<Must>b__3(T x, TProperty val, PropertyValidatorContext propertyValidatorContext)\r\n   в ServiceStack.FluentValidation.DefaultValidatorExtensions.<>c__DisplayClass7`2.<Must>b__6(Object instance, Object property, PropertyValidatorContext propertyValidatorContext)\r\n   в ServiceStack.FluentValidation.Validators.PredicateValidator.IsValid(PropertyValidatorContext context)\r\n   в ServiceStack.FluentValidation.Validators.PropertyValidator.Validate(PropertyValidatorContext context)\r\n   в ServiceStack.FluentValidation.Validators.DelegatingValidator.Validate(PropertyValidatorContext context)\r\n   в ServiceStack.FluentValidation.Internal.PropertyRule.InvokePropertyValidator(ValidationContext context, IPropertyValidator validator, String propertyName)\r\n   в ServiceStack.FluentValidation.Internal.PropertyRule.<Validate>d__8.MoveNext()\r\n   в System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()\r\n   в System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   в System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   в ServiceStack.FluentValidation.AbstractValidator`1.Validate(ValidationContext`1 context)\r\n   в ServiceStack.FluentValidation.AbstractValidator`1.ServiceStack.FluentValidation.IValidator.Validate(ValidationContext context)\r\n   в ServiceStack.FluentValidation.DefaultValidatorExtensions.Validate[T](IValidator`1 validator, T instance, IValidatorSelector selector, String ruleSet)\r\n   в ServiceStack.FluentValidation.DefaultValidatorExtensions.ValidateAndThrow[T](IValidator`1 validator, T instance, String ruleSet)\r\n   в ServiceStack.FluentValidation.DefaultValidatorExtensions.ValidateAndThrow[T](IValidator`1 validator, T instance, ApplyTo ruleSet)\r\n   в ServiceStack.Auth.RegisterService`1.Post(Register request)\r\n   в lambda_method(Closure , Object , Object )\r\n   в ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto)",
        "Errors": []
    }
}

Also, this code works ok with ServiceStack 3.

Was it helpful?

Solution 2

Juast a bug. It was fixed in 4.0.6

OTHER TIPS

I came across the same problem, and this is the only way I got around it without turning off ValidationFeature all together (would have lost too much good stuff).

Looking at the SourceCode for RegistrationFeature, all it does is register a route, a service and a validator.

https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/RegistrationFeature.cs

I simply copied this into my own "Registration Feature", and modified the line that registers the offending validator to register one of my own.

I then registered my own Registration Feature rather than the existing one in app_startup. All appears to be working, although be mindful that you will have lost some validation which will need to be re-implemented.

Global.asax

Plugins.Add(new MyRegistrationFeature());

MyRegistrationFeature.cs

public class MyRegistrationFeature : IPlugin
{
    public string AtRestPath { get; set; }

    public MyRegistrationFeature()
    {
        this.AtRestPath = "/register";
    }

    public void Register(IAppHost appHost)
    {
        appHost.RegisterService<RegisterService>(AtRestPath);
        appHost.RegisterAs<MyRegistrationValidator, IValidator<Register>>();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top