How to configure the database tables with Fluent? - fluent-nhibernate

Iam trying to generate the tables of the database but the tables are not generated.
I'm doing the mapping like this:
using Domain.Entidades;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Domain.Infraestrutura
{
public class Conexao
{
public static void CriarTabelasBanco()
{
FluentConfiguration configuration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(x => x.FromConnectionStringWithKey("psConnection")).ShowSql())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<Pessoa>());
configuration.BuildSessionFactory();
}
}
}

Add your mappings first and then call ExposeConfiguration, otherwise the mappings are not known to the config at the point you call the SchemaExport.
Apart from that, SchemaExport fails silently, so if there are any errors, it will not throw and exception, instead you can have a look into the exceptions list of the SchemaExport instance, therefore you would have to structure the call slightly different.

Related

Third party IIS Module not printing to log files

I have an asp.net core application hosted and webdav enabled in IIS for storing files as well. I want to know when a user saves a file in the webdav server (ie, if opened and saved in Microsoft word). I have put together this IIS module to try and intercept all http requests so I can find out what a webdav HTTP requests look like and then hopefully create a if statement using this logic once I can identify the save request. So far here is the IIS module I have created:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace WebDAVTargetDetectorIIS
{
public class LoggingModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
context.PostLogRequest += new EventHandler(PostLogEvent);
}
#endregion
public void OnPreRequestHandlerExecute(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
if (!String.IsNullOrEmpty(request["HTTP_CF_CONNECTING_IP"]))
{
request.ServerVariables.Set("REMOTE_ADDR", request["HTTP_CF_CONNECTING_IP"]);
}
}
public void PostLogEvent(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
app.Response.AppendToLog("This is a test");
}
}
}
I have created the BIN folder in the root folder of my web application and used the IIS Manager to add the new module. It is added to the end of the ordered list. The log files in IIS seem to save to "C:\inetpub\logs\LogFiles\W3SVC1" however my module does not seem to be printing my test line in there. Is there a particular reason the code above would not implement on any even a login request? Or should I be attaching this to some other event?

DotNet Core Dependency Injection Multiple Projects

I am building a Blazor WASM application.
The application is working but I am now looking to split the project into sensible self contained projects but having a problem working out how to implement the dependency injection without creating a circular dependency between projects.
Projects:
App.Client - UI Razor Pages
App.Server - Main project, controllers, defines interfaces
App.Shared - Shared models between Client & Server
App.Data - Implements repositories, unit of work, Db Context, migrations
The problem I am having is that the App.Data project has a dependency on the App.Server project to implement the interfaces it requires, but then I am not sure how to configure the dependencies in the startup.cs file in the App.Server project as this cannot have a dependency on the App.Data project.
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Test.Models;
using Test.Models.Data;
namespace Test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("database"), b => b.MigrationsAssembly("Test")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAntiforgery antiforgery)
{
...
}
}
}
Project structure
I ended up combining the two comments to implement a Clean Architecture structure modified for the Blazor WASM application.
Adding a Core project, defining the interfaces, and Infrastructure project implementing infrastructure interfaces such as the database. Server then has dependencies on these 2 projects which makes sense.
Startup then configures the interfaces via DI, using the appropriate implementations either in Core or Infrastructure.

NHibernate Envers IntegrateWithEnvers error

Receiving no extension method found
'NHibernate.Cfg.Configuration' does not contain a definition for 'IntegrateWithEnvers' and no extension method 'IntegrateWithEnvers' accepting a first argument of type 'NHibernate.Cfg.Configuration' could be found (are you missing a using directive or an assembly reference?)
I am using NHibernate Envers 2, NHibernate 4 and FluentNHibernate 1.4 with asp.net 4.5.
My Configuration is
using CancerConnect.Domain;
using CancerConnect.Infrastructure.Conventions;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using System;
var enversConf = new NHibernate.Envers.Configuration.Fluent.FluentConfiguration();
enversConf.Audit<Users>();
var cfg = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(p => p.FromConnectionStringWithKey("dbConnectionString")))
.Mappings(m => m.AutoMappings.Add(persistenceModel))
.ExposeConfiguration(conf =>
{
conf.IntegrateWithEnvers(enversConf);
})
.BuildConfiguration();
Am I missing anything
The extension method IntegrateWithEnvers is defined in the namespace NHibernate.Cfg see here.
So be sure to add the following using:
using NHibernate.Cfg;
And you will have access to the extension method.

Trying to implement a Breeze controller in asp.net mvc

I am trying to implement a simple Breeze controller in Asp.Net MVC4, but can't seem to access it. Is it possibly conflicting with .Net's standard Web.Api ?
If my url is http://localhost:49479/api/values then I get a good return value from Web Api.
However if my url is http://localhost:49479/breeze/Breeze I get "Http 404" error "Resource not found".
If my url is http://localhost:49479/breeze/Breeze/5 I get error No HTTP resource was found that matches the request URI 'http://localhost:49479/breeze/Breeze/5'.
Your advice is greatly appreciate.
Here's what I have in ..Controllers/BreezeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Breeze.ContextProvider;
using Breeze.WebApi2;
using Newtonsoft.Json;
namespace RageSys.Controllers
{
[BreezeController]
public class BreeezeController : ApiController
{
// GET api/values
public string Get(int id)
{
return "value";
}
public IEnumerable<string> GetMtm(int id)
{
return new string[] { "value1", "value2" };
}
}
}
and in BreezeWebApiConfig.cs :
using System.Web.Http;
[assembly: WebActivator.PreApplicationStartMethod(
typeof(RageSys.App_Start.BreezeWebApiConfig), "RegisterBreezePreStart")]
namespace RageSys.App_Start {
///<summary>
/// Inserts the Breeze Web API controller route at the front of all Web API routes
///</summary>
public static class BreezeWebApiConfig {
public static void RegisterBreezePreStart() {
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "BreezeApi",
routeTemplate: "breeze/{controller}/{action}"
);
}
}
}
The result you are getting from your api/values request is not coming from the listed controller. You must have the default ValuesController and WebApiConfig (which defines a route that takes a parameter) still in your project.
You do not have a route for http://localhost:49479/breeze/Breeze/5. The third segment (currently 5) needs to be the name of an Action method. For you, that means GetMtm. You do not have a route that takes any parameters, so you'll get nothing from: http://localhost:49479/breeze/Breeze/GetMtm/5 unless you define such a route. You probably don't want to do this though, because Breeze coupled with Entity Framework will make life very easy. You should implement the simplest possible Breeze / Entity Framework application and see how it works from there.
If you are using parameters and using Breeze, then ensure you use the .withParameters({ ParameterName: "Fred"}) or .withParameters({ id: id-value}), for example, in your Breeze query and ensure the parameter name in your function to be called (GetMtm in your case) at the server matches the parameter name you are using at the client.

Can't subscribe to CheckinEvent

I am trying to subscribe to CheckinEvent, for some reason my Notify method isn't called.
This is my contract -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace TFSubscriber
{
[ServiceContract(Namespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
public interface IRollupService
{
[OperationContract(Action = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify")]
[XmlSerializerFormat(Style = OperationFormatStyle.Document)]
void Notify(string eventXml, string tfsIdentityXml);
}
}
My implementation is pretty simple, empty method. I put a breakpoint in my Notify method and it's not called.
This how I subscribe to CheckinEvent -
C:\Program Files\Microsoft Team Foundation Server2010\Tools>bissubscribe.exe /eventType CheckInEvent /address http://localhost:4556/Rollupservice.svc /collection http://localhost:8080/tfs/defaultcollection
I have a solution that I added to source control already, and I am checkin' some files and the breakpoint isn't getting hit.
What am I doing wrong?
Are you sure your subscription works? For example try logging some information regarding input parameter eventXML. Because, you need to be sure about if your subscription works or not. If so, you can run your SVC project and attach a debugger on it. That is how debugger supposed to hit your breakpoint.