I have a custom claims provider, which uses some services from another assembly. This service assembly basically uses Linq-to-SQL context to read data from database which then custom claims provider uses to augment claims.
The problem is, that this mechanism works OK, and suddenly users can't log in, because SecurityTokenService fails to issue token, because there is an error in custom claims provider, which comes from service assembly. It fails randomly, and I assume problem is with linq data context (see stacktrace).
For now we fix this by restarting iis periodically, but we need to find out better solution.
Example stacktrace with error:
System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'.
at System.Data.SqlClient.SqlBuffer.get_String()
at Read_Szkola(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at MiscServices.DAO.RocznikoKlasa.get_Szkola1()
at MiscServices.Model.ClassItem.CreateFromYearClassEntity(RocznikoKlasa klasa)
Strangest thing is that it all works and sudenly this invalid cast exception occurs. Any ideas what might be the cause and how I could solve this issue?
Update
As requested, I paste below my model:
public class ClassItem
{
public long ID { get; set; }
public string Name { get; set; }
public SchoolItem School { get; set; }
public int StartYear { get; set; }
public string Symbol { get; set; }
public static ClassItem CreateFromYearClassEntity(RocznikoKlasa klasa)
{
if (klasa == null) throw new ArgumentNullException("klasa");
return new ClassItem
{
ID = klasa.ID,
Name = klasa.Nazwa,
StartYear = klasa.RokRozpoczecia,
Symbol = klasa.SymbolOddzialu,
School = SchoolItem.CreateFromEntity(klasa.Szkola1)
};
}
}
public class ClassItem
{
public long ID { get; set; }
public string Name { get; set; }
public SchoolItem School { get; set; }
public int StartYear { get; set; }
public string Symbol { get; set; }
public static ClassItem CreateFromYearClassEntity(RocznikoKlasa klasa)
{
if (klasa == null) throw new ArgumentNullException("klasa");
return new ClassItem
{
ID = klasa.ID,
Name = klasa.Nazwa,
StartYear = klasa.RokRozpoczecia,
Symbol = klasa.SymbolOddzialu,
School = SchoolItem.CreateFromEntity(klasa.Szkola1)
};
}
}
public class SchoolItem
{
public string Code;
public JstItem JST;
public string Name;
public string City;
public string PostCode;
public string Street;
public string Telephone;
public string Fax;
public string Email;
public static SchoolItem CreateFromEntity(MiscServices.DAO.Szkola school)
{
if (school == null) throw new ArgumentNullException("school");
return new SchoolItem {
Code = school.Kod,
Name = school.Nazwa,
City = school.Miasto,
Street = school.Ulica,
PostCode = school.KodPocztowy,
Telephone = school.Telefon,
Fax = school.Fax,
Email = school.Email,
JST = JstItem.CreateFromEntity(school.JST1)
};
}
}
nullableproperty that is returning null or it has changed in the database and not updated in the mapping. – F.Aquino Jan 28 at 16:46