Fluent Nhibernate configuration method wont compile - expression does not produce value - vb.net

Code below will not compile. Line with New SchemaExport(cfg).Create(True, True) - expression does not produce a value.
I'm new to Nhibernate. Am I missing something? This is a conversion of a code from C# which complies fine.
Imports System.Configuration
Imports FluentNHibernate.Cfg
Imports FluentNHibernate.Cfg.Db
Imports NHibernate
Imports NHibernate.Tool.hbm2ddl
Imports Ninject.Activation
Imports Ninject.Modules
Imports OSIM.Core
Imports Configuration = NHibernate.Cfg.Configuration
Public Class IntegrationTestsModule
Inherits NinjectModule
Public Overrides Sub Load()
Bind(Of IItemTypeRepository).To(Of ItemTypeRepository)()
Bind(Of ISessionFactory).ToProvider(New ntegrationTestSessionFactoryProvider)
End Sub
End Class
Class IntegrationTestSessionFactoryProvider
Inherits Provider(Of ISessionFactory)
Protected Overrides Function CreateInstance(context As IContext) As ISessionFactory
Dim sessionFactory = Fluently.Configure().
Database(MsSqlConfiguration.MsSql2008.ConnectionString(Function(c) c.Is(ConfigurationManager.AppSettings("localDb"))).ShowSql()). _
Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of ItemTypeMap)().ExportTo("C:\Temp")). _
ExposeConfiguration(Function(cfg) New SchemaExport(cfg).Create(True, True)).BuildSessionFactory()
Return sessionFactory
End Function
End Class

The ExposeConfiguration method is defined as follows (vb):
Public Function ExposeConfiguration(config As Action(Of Configuration)) As FluentConfiguration
It takes an instance of Action(Of Configuration). You must change Function to Sub like this:
ExposeConfiguration(Sub(cfg) New SchemaExport(cfg).Create(True, True)).BuildSessionFactory()

Came here with same question, but inline code above not working -- possibly because of NHIbernate updates.
Error was: "Expression does not produce a value".
I reckon NHibernate modified the SchemaExport tool in the meantime?
Anyways, here's a working code with NHibernate 5.2.0.0
For those new to lambda functions, please note the newlines
Dim fluentConfig = Fluently.Configure() _
.Database(SQLiteConfiguration.Standard.UsingFile("hibernatetest.db")) _
.Mappings(Function(m) m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())) _
.ExposeConfiguration(Sub(cfg)
Dim se = New SchemaExport(cfg)
se.Execute(True, True, False)
End Sub) _
.BuildSessionFactory()
Hope it helps.

Related

How to upload file with Asp.net Web Api project?

i have a web API project to be consumed from mobile applications and i was trying to make a file upload controller. there are few resources in C# and i don't find anything useful for VB.NET.
i tried this code below converting from c# to VB.NET, but says "Move is not a member of MultiPartFileData".
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Net.Http
Imports System.Threading.Tasks
Imports System.Web
Imports System.Web.Http
Namespace HelloWorld.Controller
Public Class FileUploadingController
Inherits ApiController
<HttpPost>
<Route("api/FileUploading/UploadFile")>
Public Async Function UploadFile() As Task(Of String)
Dim ctx = HttpContext.Current
Dim root = ctx.Server.MapPath("~/App_Data")
Dim provider = New MultipartFormDataStreamProvider(root)
Try
Await Request.Content.ReadAsMultipartAsync(provider)
For Each file In provider.FileData
Dim name = file.Headers.ContentDisposition.FileName
name = name.Trim(""""c)
Dim localFileName = file.LocalFileName
Dim filePath = Path.Combine(root, name)
File.Move(localFileName, filePath)
Next
Catch e As Exception
Return $"Error: {e.Message}"
End Try
Return "File uploaded!"
End Function
End Class
End Namespace
That error message means the compiler think that you are trying to call a method from the MultiPartFileData class. The variable of type MultiPartFileData is the one called file (notice the lowercase) initialized in the For Each loop.
Instead you want to call the Move method from the System.IO.File class (notice the uppercase).
VB.NET services (always running in background looking to catch errors while you type) is case insensitive so, for it, the two names are the same and here arises the error emitted when you code that line.
The best solution is to avoid names like file for your variables when you plan to use the class System.IO.File. Otherwise you could simply add the full qualified method's name in this way
System.IO.File.Move(localFileName, filePath)

convert SOAP to REST and return many records from a function

Convert working SOAP WCF to REST and still use Function that reads and returns the values I need.
ActiveCallSevice reads entity to return several records using SOAP in VB. How can I code it to return all the records when changing to REST? I have tried many different versions of the uriTemplate but nothing is working. I basically should read a view and return the data without getting any parms from uri. I used good example from here, but that one is manually coding value returned from looping and I have a Function that reads and returns the values I need.
My IActiveCalls.vb code current on WebGet line - error states - Constant expression is required. C:\inetpub\VS2013\wcfWazeP1\IActiveCalls.vb 10 13 WcfWazeP1.
Public Interface IActiveCalls
Property UriTemplate As String
<WebGet(UriTemplate = "Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents)")>
<OperationContract()>
Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents)
Implements IActiveCalls
Dim dbP1RDW As New ReportingDWEntities
Public Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents) Implements IActiveCalls.GetWazeCalls
Return dbP1RDW.DIT_CurrentSOTrafficIncidents.OrderBy(Function(o) o.IncidentDate).ToList()
End Function
<DataContract()>
Public Class ActiveCalls
End Class
My ActiveCalls.vb code on Implement line – error states -Class 'ActiveCallsService' must implement 'Property UriTemplate As String' for interface 'IActiveCalls'. Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers. C:\inetpub\VS2013\wcfWazeP1\ActiveCalls.vb 9 16 WcfWazeP1.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text
Public Class ActiveCallsService
Implements IActiveCalls
Dim dbP1RDW As New ReportingDWEntities
Public Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents) Implements IActiveCalls.GetWazeCalls
Return dbP1RDW.DIT_CurrentSOTrafficIncidents.OrderBy(Function(o) o.IncidentDate).ToList()
End Function
End Class
12/07/2017 Thanks GMan80013, After changes. Still have errors that prevent it from running. I tried several placements of the UriTemplate declaration with no luck. How important is the name of the UriTemplate? Could I call it anything, or should it reference the ActiveCallsService? Sorry to be so uninformed.
ActiveCalls.vb two errors
1- Field or property 'UriTemplate' is not found. C:\inetpub\VS2013\wcfWazeP1\IActiveCalls.vbWcfWazeP1
2- 'WebGetAttribute' cannot be used as an attribute because it does not inherit from 'System.Attribute'. C:\inetpub\VS2013\wcfWazeP1\IActiveCalls.vb
Public Interface IActiveCalls
Property UriTemplate As String
<WebGet(UriTemplate:="currentSOTrafficeIncidents")>
<OperationContract()>
Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents)
End Interface
ActiveCalls.vb – I added the Public Property UriTemplate and no errors here
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text
Public Class ActiveCallsService
Implements IActiveCalls
Dim dbP1RDW As New ReportingDWEntities
Public Property UriTemplate As String Implements IActiveCalls.UriTemplate
Public Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents)
Implements IActiveCalls.GetWazeCalls
Return dbP1RDW.DIT_CurrentSOTrafficIncidents.OrderBy(Function(o)
o.IncidentDate).ToList()
End Function
End Class
1) The URI Template value is meant to represent the part of the URL after the host name. If your site address is http://www.myhouse.com/ and you want clients to get all records with this address http://www.myhouse.com/currentSOTrafficIncidents Then "currentSOTrafficIncidents" is the URI Template. Also, possibly the cause of the error, be sure after UriTemplate you have := not just =. This is required in vb any time you are specifying the parameter name.
<WebGet(UriTemplate:= "currentSOTrafficIncidents")>
<OperationContract()>
Function GetWazeCalls() As List(Of DIT_CurrentSOTrafficIncidents)
2) In your interface you declared Property UriTemplate As String and this public property is missing from the ActiveCalls.vb class. I don't think you need this, however and could remove it from the IActiveCalls class. If you really want to keep it there, correct the error by adding this to ActiveCalls.vb.
Public Property UriTemplate As String Implements IActiveCalls.UriTemplate

How to Inject a parameter to constructor using unity

Hi I am using unity in WebAPI 1.0 and registered it under Global.asax.vb file as below
Dim container As IUnityContainer = New UnityContainer()
container.RegisterType(Of Car)(New InjectionConstructor(GetType(Integer)))
Now how do i pass integer value which is passed by the client application into car type using
Public Sub New(ByVal intUserId As Int32)
objCar = DependencyResolver.Current.GetService(Of car)()
End Sub
Can't find anything to use (ParameterOverride("intUserId", intUserId) within DependencyResolver
It is very similar as registering unity container with ASP.NET MVC. Though web api has a different execution pipe line. Follow these steps.
NB: I have converted this code from C# to VB using a converter. So I hope it is syntactically correct. Though it is accurate in c#.
1) Implement IDependencyResolver (Make sure you are resolving correct namespace here. IDependencyResolver should come from System.Web.Http.Dependencies. Make note of Imported namespaces.
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Microsoft.Practices.Unity
Imports System.Web.Http.Dependencies
Namespace YourNamespace.Framework
Public Class UnityApiDependencyResolver
Implements IDependencyResolver
Private _container As IUnityContainer = Nothing
Public Sub New(container As IUnityContainer)
Me._container = container
End Sub
Public Function GetService(serviceType As Type) As Object
Try
Return _container.Resolve(serviceType)
Catch generatedExceptionName As Exception
Return Nothing
End Try
End Function
Public Function GetServices(serviceType As Type) As IEnumerable(Of Object)
Try
Return _container.ResolveAll(serviceType)
Catch generatedExceptionName As Exception
Return Nothing
End Try
End Function
Public Function BeginScope() As IDependencyScope
Return Me
End Function
Public Sub Dispose()
'
End Sub
End Class
End Namespace
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: #telerik
'Facebook: facebook.com/telerik
'=======================================================
2) Configure your container either thgough config or through code.
3) Register your container in Global.asax.vb file
Dim container = New UnityContainer()
Dim section As UnityConfigurationSection = TryCast(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
section.Configure(container, "UnitySection")
'api dependency resolver
GlobalConfiguration.Configuration.DependencyResolver = New UnityApiDependencyResolver(container)
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: #telerik
'Facebook: facebook.com/telerik
'=======================================================
Thats it.
Now you can declare your dependency in any of your API controller and it will be injected by Unity
Public Sub New(repositoryFactory As IRepositoryFactory, serviceFactory As IServiceFactory)
Me.repositoryFactory = repositoryFactory
Me.serviceFactory = serviceFactory
End Sub
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: #telerik
'Facebook: facebook.com/telerik
'=======================================================
Your type registration is wrong as well. you have to specify either an interface or an abstract class as dependency and its concrete implementation as its mapping.
e.g
container.RegisterType(Of IContext, Context)()
I don't understand what are trying to achieve by mapping an integer value to car. Do you want car object to be loaded based on integer value in your parameter?

Migratordotnet : How to write a migration class with VB.net?

I got all C# implementations on Google, so I converted it to VB.Net, but I am not able to convert 1 line where it gives error.
My Class :
Imports Migrator.Framework
[Migration(1)] ' Gives ERROR Here.. How to write this in VB.net ?
Public Class mig_001
Inherits Migration
Public Overrides Sub Up()
Database.AddTable("Planets",
New Column("Id", DbType.Int32, ColumnProperty.PrimaryKeyWithIdentity),
New Column("Name", DbType.String, 100, ColumnProperty.NotNull),
New Column("Diameter", DbType.Double),
New Column("Mass", DbType.Double),
New Column("SupportsLife", DbType.Boolean, False)
)
End Sub
Public Overrides Sub Down()
Database.RemoveTable("Planets")
End Sub
End Class
Once again :
[Migration(1)] => How to write this in VB.net and what does it means in VB.net ?
I read in an article that its mandatory for version of Migration, otherwise Migratordotnet will miss the migration.
So.. How to do it ?
Like this:
<Migration(1)> _
Public Class mig_001
This <…> feature, belonging to the following class, is called an attribute in .NET.

CruiseControl .Net Plugin Vb.net Error

I am trying to make my own Labeller plugin for Cruise Control .Net 1.4.3. I have made a class based on another plug in example but I keep getting an error
Class 'AssemblyVersionLabeller' must implement 'Function Generate(integrationResult As IIntegrationResult) As String' for interface 'ThoughtWorks.CruiseControl.Core.ILabeller'
Here is my code :
Imports Exortech.NetReflector
Imports ThoughtWorks.CruiseControl.Core
Imports ThoughtWorks.CruiseControl.Core.Util
Namespace NetAssembly.CCNet.Label
_
Public Class AssemblyVersionLabeller
Implements ILabeller
Public Sub Run(ByVal result As IIntegrationResult)
result.Label = Generate(result)
End Sub
Public Function Generate(ByVal integrationResult As IIntegrationResult) As String
Dim label As String = integrationResult.LastIntegration.Label
Return label
End Function
<ReflectorProperty("prefix", Required:=False)> _
Public Prefix As String = String.Empty
End Class
End Namespace
What am I doing wrong? What have I missed?
Background Info:
I am using VS2005. I cant use CrusieControl 1.4.4 RC2 (which has an Assembly Labeller) because my source control's plugin (SCM Anywhere) doesnt work with it.
I cannot judge just by looking at your code, but if you need a sample on how to write labellers (C# code though), you could take a look at BrekiLabeller code (written by me).
I believe you forgot the overrides decleration..
Public Overrides Function Generate