Public Properties within VB Module - Cross-client Behavior - vb.net

Can one client call a public property within a VB.NET module and see the value of that public property changed by another client accessing it at the same time?
Example:
Client 1 calls
Public Module DataModule
Private theDateTime As DateTime = GetAdjustedDateTime() //initial TZ value
Public Property GetSetDateTime() As DateTime
Get
Return theDateTime
End Get
Set(ByVal value As String)
theDateTime = value
End Set
End Property
End Module
by first setting the Property and then getting the value throughout WhateverMethod()...
Partial Class admintours
Inherits System.Web.UI.Page
Private Sub WhateverMethod()
GetSetDateTime = Now
...
...
... //code
...
SomeFunction(GetSetDateTime) //value is 10/14/2010 00:23:56
...
...
//almost simultaneously Client 2 sets the value to Now.AddDays(-1)
...
SomeOtherFunc(GetSetDateTime) //value passed in: 10/13/2010 00:23:56
...
...
... //some more code
...
End Sub
End Class
I'm running into random instances where it looks like another client might be modifying (by setting) the value of GetSetDateTime DURING the first client's run through of WhateverMethod(). This is alarming to me and I've been trying to figure out if that's a possibility. Any confirmation or otherwise as to that would be helpful, thanks!

Modules in VB.Net are shared within an AppDomain. So two clients within the same AppDomain will be operating on the same instance of any given Module. This means one could easily see the results of the other writing to the Module if they are running in parallel in the same AppDomain
In many ways it's best to view the data stored in a Module as global (it's not truly global but behaves that way for many samples).

Yes, if by "client" you mean separate threads in a single application (also assuming a single CPU process and a single AppDomain).
Now, you suggest that it is "alarming" if this is a possibility, so I assume that you want to make sure that this doesn't happen? In order words, you want to ensure that the value of GetSetDateTime remains unchanged during the execution of WhateverMethod.
It sounds like WhateverMethod is only run by "client 1", and the "client 2" code that changes the GetSetDateTime property is independent of WhateverMethod. It doesn't sound like SyncLock would help here.
If both clients are able to change GetSetDateTime at any time then you need to modify WhateverMethod like so:
Private Sub WhateverMethod()
Dim localNow = Now
GetSetDateTime = localNow
...
SomeFunction(localNow)
...
SomeOtherFunc(localNow)
...
End Sub
Does this help?

Related

VB.NET Connected SAP WSDL Preventing Solution from building

I'm trying to build a .NET Class Library that utilises a SAP generated WSDL.
In the reference.vb file that one of the WSDLs generates, I'm getting the following error in this line:
<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, Order:=0)> _
With the error being BC30369 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. on System.
This only occurs within one of the generated Partial Public Classes that it generates, and not the rest.
After removing System it works:
'''
<System.Xml.Serialization.XmlElementAttribute(Form:=Xml.Schema.XmlSchemaForm.Unqualified)>
Public Property MESSAGE_V4() As String
Get
Return Me.mESSAGE_V4Field
End Get
Set
Me.mESSAGE_V4Field = Value
End Set
End Property

Entity Framework connecting to wrong database

I have an existing database and I'm trying to connect to it using entity framework, however it throws an exception saying
The server principal "User" is not able to access the database
"DatabaseTwo" under the current security context.
However, I'm not trying to connect to DatabaseTwo, there is no reference to it anywhere in my entire solution.
My DbContext: (DatabaseOne)
Public Class MyContext
Inherits DbContext
Public Sub New()
MyBase.New("DatabaseOne")
End Sub
Public Property Objects As DbSet(Of Object)
End Class
Web.Config connection string:
<add name="DatabaseOne"
connectionString="server=myserver.com;database=DatabaseOne;UID=MyUser;PWD=MyPwd;
APP=MyApp;" providerName="System.Data.SqlClient"/>
The other database does exist on the server and the user does have access to both database one and two, which is also strange consdering it says it dosen't have permission
The Entity had a slightly different name to the table, so using the attribute to specify the exact table name seemed to fix the problem. Still the exception was very strange
<Table("CorrectTableName")>
Public Class MyTable
<Key>
Public Property Id As Integer
End Class

Variable is already declared as private

I have an entity framework model (built using database first) which I have just updated from the database. I now have a whole series of errors in the model, typical of which is;
_Addresses is already declared as Private _Addresses AS System.Data.Objects.ObjectSet(of Address) in this class. If I then double click on the error in the error list it takes me to the following block of codein the Model's designer.vb file.
''' <summary>
''' No Metadata Documentation available.
''' </summary>
Public ReadOnly Property Addresses() As ObjectSet(Of Address)
Get
If (_Addresses Is Nothing) Then
_Addresses = MyBase.CreateObjectSet(Of Address)("Addresses")
End If
Return _Addresses
End Get
End Property
Private _Addresses As ObjectSet(Of Address)
I can see nothing different here to what was there originally, but the project will not build successfully anymore. Can anyone suggest why this may be and what it is that has happened to cause the errors to appear.
More importantly can anyone suggest how one goes about rectifying this without resorting to rebuilding the entity data model from scratch. The data model is in a separate project so it could be rebuilt but it has undergone a lot of customisation so I would prefer not to go down that route.
Thanks for any advice that you can offer.

Unity Container Type Registration Quirk

I'm trying to automatically register all reports in a unity container.
All reports implement IReport and also have a Report() attribute which defines the title, description and unique key (so I can read these without instantiating a concrete class).
So...
I get the report types like this
Public Shared Function GetClassesWhichimplementInterface(Of T)() As IEnumerable(Of Type)
Dim InterfaceType = GetType(T)
Dim Types As IEnumerable(Of Type)
Types = Reflection.Assembly.GetCallingAssembly.GetTypes()
Return Types.Where(Function(x) InterfaceType.IsAssignableFrom(x))
End Function
And register them like this:
Public Sub RegisterReports()
Dim ReportTypes = ReflectionHelper.GetClassesWhichimplementInterface(Of IReport)()
For Each ReportType In ReportTypes
''Previously I was reading the report attribute here and using the defined unique key. I've stopped using this code to remove possible problems while debugging.
Container.RegisterType(GetType(IReport), ReportType, ReportType.Name)
Next
End Sub
There are types being returned by the call to GetClassesWhichimplementInterface() and the Container.RegisterType() call is made without errors. If I call Container.Resolve(of Interfaces.IReport) immediately after the register call, I get the following exception:
Resolution of the dependency failed, type = "MyProject.Interfaces.IReport", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, MyProject.Interfaces.IReport, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving MyProject.Interfaces.IReport,(none)
Can anyone tell me why the container isn't preserving the registration?
The registration is in the container. The thing is that you are calling resolve without passing a named registration as a parameter.
As all your registrations were performed using the following code:
Container.RegisterType(GetType(IReport), ReportType, ReportType.Name)
Then all of them have a name. You must provide the name along with the type to be able to resolve the dependency from the container.
The error you are getting is because there is no type mapping registered without a name.

Code Analysis Error: Declare types in namespaces

Is VS2010, I analyzed my code and got this error:
Warning 64 CA1050 : Microsoft.Design : 'ApplicationVariables' should be declared inside a namespace. C:\My\Code\BESI\BESI\App_Code\ApplicationVariables.vb 10 C:\...\BESI\
Here is some reference info on the error. Essentially, I tried to create a class to be used to access data in the Application object in a typed way.
The warning message said unless I put my (ApplicationVariables) class in a Namespace, that I wouldn't be able to use it. But I am using it, so what gives?
Also, here is a link to another StackOverflow article that talks about how to disable this warning in VS2008, but how would you disable it for 2010? There is no GlobalSuppressions.vb file for VS2010.
Here is the code it is complaining a bout:
Public Class ApplicationVariables
'Shared Sub New()
'End Sub 'New
Public Shared Property PaymentMethods() As PaymentMethods
Get
Return CType(HttpContext.Current.Application.Item("PaymentMethods"), PaymentMethods)
End Get
Set(ByVal value As PaymentMethods)
HttpContext.Current.Application.Item("PaymentMethods") = value
End Set
End Property
'Etc, Etc...
End Class
I suspect that the code you entered is in your App_Code fodler of your web app. This code is accessible from your web code as you have deomnstrated but is not accessible from any other assembly.
You can suppress the instance of the error by right mouse clicking on the particular error and selecting "Suppress Message In Source." That'll result in code being added to your source that says "the next time you check this error-fuggedabodit!"
When to Suppress Warnings
--------------------------------------------------------------------------------
While it is never necessary to suppress a warning from this rule, it is safe to do this when the assembly will never be used with other assemblies.
To suppress the error on all occurences, select "Suppress in Project Suppression File"