locked
Problem with a WCF Authentication Service. RRS feed

  • Question

  • Hi guys.

    I'm attempting to add a WCF Ria services library to my project. The library contains a single authentication service.


    Here's how I create the WebContext :

    WebContext ctx = new WebContext()
    
    {
    
    Authentication = new FormsAuthentication()
    
    {
    
    DomainContext = new MyServiceAssembly.Web.MyAuthenticationContext(),
    
    DomainContextType = "MyServiceAssembly.Web.MyAuthenticationContext, MyServiceAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken = null"
    
    }
    
    };


    this.ApplicationLifetimeObjects.Add(ctx);


    When I try to perform an authentication operation - I get the following.

    Load operation failed for query 'Login'. The type initializer for 'MyServiceAssembly.MyAuthenticationService' threw an exception. Inner exception message : Value cannot be null. Parameter name: source


    Here's the stack trace :

    at System.ServiceModel.DomainServices.Server.DomainService.DefaultDomainServiceFactory.CreateDomainService(Type domainServiceType, DomainServiceContext context) at System.ServiceModel.DomainServices.Hosting.DomainOperationInvoker.GetomainService(Object instance)


    Can anyone help me out?

    Friday, October 29, 2010 9:31 AM

Answers

  • Yeah, there's nothing obviously wrong with it. Does this repro running in VS? You might be able to identify the error if you turn on exceptions. Under 'Debug -> Exceptions...' check the 'Thrown' box for 'Common Language Runtime Exceptions'.

    Monday, November 1, 2010 10:20 AM

All replies

  • It sounds like you're getting an ArgumentNullException in the type initialization (static initializer, etc.) of your MyAuthenticationService. I'd recommend verifying the following things are correct; static field initializers, static contructor, field initializers, constructor.

    Friday, October 29, 2010 10:43 AM
  • Ok, but where do I start? I'm viewing the code of the service's client and I don't see any static fields (client is 100% code-generated).

    Friday, October 29, 2010 11:00 AM
  • It's not the client where the error's occurring, it's the server (in MyAuthenticationService). One option might be to set VS to break when an exception's thrown.

    Friday, October 29, 2010 11:13 AM
  • This is the body of my Authentication service class :


    [EnableClientAccess]
        public class AuthenticationService : LinqToEntitiesDomainService<Entities>, IAuthentication<User>
        {
            public AuthenticationService() : base()
            { 
    
            }
    
            public User GetUser()
            {
                if ((this.ServiceContext != null) &&
                    (this.ServiceContext.User != null) &&
                    (this.ServiceContext.User.Identity.IsAuthenticated))
                {
                    return GetUser(ServiceContext.User.Identity.Name);
                }
    
                return AuthenticationService.DefaultUser;
            }
    
            public User Login(string userName, string password, bool isPersistent, string customData)
            {
                if(this.ValidateUser(userName, password))
                {
                    FormsAuthentication.SetAuthCookie(userName, isPersistent);
    
                    return GetUser(userName);
                }
    
                return null;
            }
    
            public User Logout()
            {
                FormsAuthentication.SignOut();
                return AuthenticationService.DefaultUser;
            }
    
            public void UpdateUser(User user)
            {
                if ((this.ServiceContext.User == null) ||
                    (this.ServiceContext.User.Identity == null) ||
                    (!string.Equals(this.ServiceContext.User.Identity.Name, user.Name, StringComparison.Ordinal)))
                {
                    throw new UnauthorizedAccessException("You are only authorized to modify your own profile");
                }
    
                this.ObjectContext.Users.AttachAsModified(user, this.ChangeSet.GetOriginal(user));
            }
    
            #region Private Methods
            private bool ValidateUser(string userName, string password)
            {
                return ObjectContext.Users
                    .Any(u => u.Name == userName && u.Password == password);
            }
    
            private User GetUser(string name)
            {
                return this.ObjectContext.Users.First(u => u.Name == name);
            }
            #endregion
    
            #region Private Members
            private static User DefaultUser = new User()
            {
                Name = string.Empty,
                FullName = string.Empty,
                PhoneNumber = string.Empty,
                Roles = null
            };
            #endregion
        }




    This is a fragment of the web.config :


    <configuration>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </modules>
        <validation validateIntegratedModeConfiguration="false" />
      </system.webServer>
        <system.web>
          <compilation debug="true" targetFramework="4.0">
            <assemblies>
              <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </assemblies>
          </compilation>
          <httpModules>
            <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
          </httpModules>
          <authentication mode="Forms" />
        </system.web>  
      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <connectionStrings>
        <add name="Entities" connectionString="..." />
      </connectionStrings>
    </configuration>

    I believe I've done everything as it should be done?

    Please note that I'm keeping my Entities in a separate assembly.

    Saturday, October 30, 2010 5:08 AM
  • Still struggling to get this resolved.

    Monday, November 1, 2010 8:59 AM
  • Yeah, there's nothing obviously wrong with it. Does this repro running in VS? You might be able to identify the error if you turn on exceptions. Under 'Debug -> Exceptions...' check the 'Thrown' box for 'Common Language Runtime Exceptions'.

    Monday, November 1, 2010 10:20 AM
  • That did the trick, I've located the issue. Thank you VERY much - that advice was priceless.

    Tuesday, November 2, 2010 3:00 AM