Kern
Kern

Reputation: 457

wcf returns invalid data

I have a test wcf webservice (REST) based on EF4 model and published on IIS7.5. One of the basic queries is

 public CfgUser GetUser(string fspId, string userName)
     {
     return _context.Users.FirstOrDefault(a => a.FspID == fspId && a.UserName == userName && a.WadtDeleted == false);
     }

I then do a test call with the following url in ie9:

https://10.201.2.70/webservices/service.svc/rest/GetUser(EDS,kern)

the XML data is correctly returned. The SQL profiler shows

exec sp_executesql N'SELECT TOP (2) 
[Extent1].[UserId] AS [UserId], 
[Extent1].[FSP] AS [FSP], 
[Extent1].[UserGroupId] AS [UserGroupId], 
[Extent1].[ContactID] AS [ContactID], 
[Extent1].[UserName] AS [UserName], 
[Extent1].[UserPassword] AS [UserPassword], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[MidName] AS [MidName], 
[Extent1].[LastName] AS [LastName], 
[Extent1].[CommonName] AS [CommonName], 
[Extent1].[enumUserType] AS [enumUserType], 
[Extent1].[enumActive] AS [enumActive], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[wadtDeleted] AS [wadtDeleted], 
[Extent1].[wadtModifiedBy] AS [wadtModifiedBy], 
[Extent1].[wadtModifiedOn] AS [wadtModifiedOn], 
[Extent1].[wadtModifiedOnDTOffset] AS [wadtModifiedOnDTOffset], 
[Extent1].[wadtRowID] AS [wadtRowID], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[CreatedOnDTOffset] AS [CreatedOnDTOffset], 
[Extent1].[Email] AS [Email], 
[Extent1].[isDeviceUser] AS [isDeviceUser], 
[Extent1].[rowguid] AS [rowguid], 
[Extent1].[PasswordHash] AS [PasswordHash], 
[Extent1].[PasswordModifiedOn] AS [PasswordModifiedOn]
FROM [dbo].[cfgUsers] AS [Extent1]
WHERE (''EDS'' = [Extent1].[FSP]) AND ([Extent1].[UserName] = @p__linq__0) AND (0 = 
[Extent1].[wadtDeleted])',N'@p__linq__0 varchar(8000)',@p__linq__0='kern'

If I then do another immediate call with url:

https://10.201.2.70/webservices/service.svc/rest/GetUser(recserv,recserv-admin)

I receive a request error. It seems that the EF linq query retains the value passed into the original fspId parameter i.e 'EDS'. The SQL Profiler shows

exec sp_executesql N'SELECT TOP (2) 
[Extent1].[UserId] AS [UserId], 
[Extent1].[FSP] AS [FSP], 
[Extent1].[UserGroupId] AS [UserGroupId], 
[Extent1].[ContactID] AS [ContactID], 
[Extent1].[UserName] AS [UserName], 
[Extent1].[UserPassword] AS [UserPassword], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[MidName] AS [MidName], 
[Extent1].[LastName] AS [LastName], 
[Extent1].[CommonName] AS [CommonName], 
[Extent1].[enumUserType] AS [enumUserType], 
[Extent1].[enumActive] AS [enumActive], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[wadtDeleted] AS [wadtDeleted], 
[Extent1].[wadtModifiedBy] AS [wadtModifiedBy], 
[Extent1].[wadtModifiedOn] AS [wadtModifiedOn], 
[Extent1].[wadtModifiedOnDTOffset] AS [wadtModifiedOnDTOffset], 
[Extent1].[wadtRowID] AS [wadtRowID], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[CreatedOnDTOffset] AS [CreatedOnDTOffset], 
[Extent1].[Email] AS [Email], 
[Extent1].[isDeviceUser] AS [isDeviceUser], 
[Extent1].[rowguid] AS [rowguid], 
[Extent1].[PasswordHash] AS [PasswordHash], 
[Extent1].[PasswordModifiedOn] AS [PasswordModifiedOn]
FROM [dbo].[cfgUsers] AS [Extent1]
WHERE (''EDS'' = [Extent1].[FSP]) AND ([Extent1].[UserName] = @p__linq__0) AND (0 = 
[Extent1].[wadtDeleted])',N'@p__linq__0 varchar(8000)',@p__linq__0='recserv-admin'

If I restart IIS service the second request will then work so there is obviously a tie in with IIS somewhere? There is a one to many association between FSP and Users.

Any help/ pointers aprpeciated

Upvotes: 0

Views: 112

Answers (1)

xplat
xplat

Reputation: 8636

 public CfgUser GetUser(string fspId, string userName)
     {
     return _context.Users.FirstOrDefault(a => a.FspID == fspId && a.UserName == userName && a.WadtDeleted == false);
     }

If this is a real code example from your app then you shouldn't have a field as I see to treat the context as a created instance in every service instance that is created.

You should change the context to create an instance with a using statement to dispose after the return of the data you need. This should fix your error.

public CfgUser GetUser(string fspId, string userName)
     {
        using (MyEntities _context = new MyEntities())
        {
     return _context.Users.FirstOrDefault(a => a.FspID == fspId && a.UserName == userName && a.WadtDeleted == false);
        }
     }

Upvotes: 1

Related Questions