Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
B-Nasty
May 25, 2005

nielsm posted:

Does this sound like the right approach, and what would be the pattern to implement this?

Personally, I don't like using DI containers to do runtime configuration by swapping implementations. It can definitely be done, but it feels a little hokey.

What if you had an IConfigurableIdentitySupplicant that took a dependency on the other two implementations, as well as a dependency on your config provider. It would use the config setting to call the correct concrete implementation. One advantage to this is that the logic providing the right implementation based on config settings would be testable.

Adbot
ADBOT LOVES YOU

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

nielsm posted:

DI question, I'm using SimpleInjector to set up my application. Now I need to have a dynamic configuration, depending on a configuration setting I'd like to supply a different object for a specific interface. I think.
I'd like to have an IUserIdentitySupplicant, usually fulfilled by an AutoDetectIdentitySupplicant, but the installer should be able to set a setting that would select a FixedIdentitySupplicant that doesn't dynamically detect and supply identity data, just having a static result.

Does this sound like the right approach, and what would be the pattern to implement this?

You're probably better off injecting a POCO with the configuration setting and having your code read that and do different things based on what's in the POCO rather than having than changing which object is registered based on configuration settings. This might not matter if you only care what the setting is at application startup, but simpleinjector locks the container after the first use, so after getInstance() you won't be able to re-register.

Xik
Mar 10, 2011

Dinosaur Gum
I've done conditional injection based on things like the type of clients calling an API endpoint in autofac and various other configuration flags etc.

If you spend a bit of time to learn the library well enough you can swap out complex conditional dependency trees with a minimal amount of clear and easy to read code. In my opinion it's much nicer than passing around flags and stamping conditional logic all over the place.

Sab669
Sep 24, 2009

Is it possible to embed razor code within an HTML element's attribute value? I'm trying to conditionally apply a certain CSS class to 16 different elements, based on a model's property.

So instead of:

code:
@if (Model.SomeProperty > X)
{
 <p class="xyz abc">...</p>
}
else
{
<p class="xyz">...</p>
}
I'd like to do something like this:

code:
<p class="xyz @(if (Model.SomeProperty> X) { "abc"})>...</p>
But this doesn't seem to work.

I can't just do something like

code:
@string cssClass = "";
@if (Model.SomeProperty > X)
{
cssClass = "xyz abc";
}
else
{
cssClass = "xyz";
}
Because I actually need to apply this class to 16 different elements, each that have differing "base" css classes

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



code:
<p class="xzy @(Model.Prop ? "abc" : string.Empty)">
Has worked for me before but, if you have 16, then

code:
@if (Model.Prop) {
<style>
   /*make the selector more specific until it takes over*/
   p.xyz { whatever: youwant; }
   /* or use !important like a monster */
</style>
}
might be less visually noisy.

Sab669
Sep 24, 2009

Munkeymon posted:

code:
<p class="xzy @(Model.Prop ? "abc" : string.Empty)">
Has worked for me before but, if you have 16, then

Brilliant, I must've just had some random syntax typo I'm too tired to have picked up on :downs: Friday.cs

Thank you.

NiceAaron
Oct 19, 2003

Devote your hearts to the cause~

Continuing the PDF talk from the previous page: I'm not sure how well this would work for a WPF application (in fact I suspect it probably wouldn't work very well at all) but something I've done in the past is generate HTML documents and then use headless Chrome to convert them to PDFs. In my case it was an ASP.NET MVC application so it worked very well, since it meant you could design and create the PDF files with the same html/css skills you were already using for the rest of the system.

mystes
May 31, 2006

NiceAaron posted:

Continuing the PDF talk from the previous page: I'm not sure how well this would work for a WPF application (in fact I suspect it probably wouldn't work very well at all) but something I've done in the past is generate HTML documents and then use headless Chrome to convert them to PDFs. In my case it was an ASP.NET MVC application so it worked very well, since it meant you could design and create the PDF files with the same html/css skills you were already using for the rest of the system.
If you're doing something like this you can also use a program/library like weasyprint that's specifically intended for using html for PDF generation, but it might be a pain to distribute with a desktop .net application.

roadhead
Dec 25, 2001

NiceAaron posted:

Continuing the PDF talk from the previous page: I'm not sure how well this would work for a WPF application (in fact I suspect it probably wouldn't work very well at all) but something I've done in the past is generate HTML documents and then use headless Chrome to convert them to PDFs. In my case it was an ASP.NET MVC application so it worked very well, since it meant you could design and create the PDF files with the same html/css skills you were already using for the rest of the system.

I like where your head is at, unfortunately I can't (realistically) include Chromium as a dependency, nor rely on my customer's necessarily even having it on their systems.

I've been granted a stay of execution for at least the next couple of weeks while that feature gets pushed back a bit :)

nielsm
Jun 1, 2009



I've used wkhtmltopdf for generating PDFs, it's a standalone binary with all dependencies compiled in statically. But it's not terribly good to work with/debug and can be rather slow.

redleader
Aug 18, 2005

Engage according to operational parameters
We used EOPdf for HTML -> PDF in a previous job. I never worked with it, paid for it, or had to support it in production, but it seemed ok enough.

EssOEss
Oct 23, 2006
128-bit approved

nielsm posted:

I've used wkhtmltopdf for generating PDFs, it's a standalone binary with all dependencies compiled in statically. But it's not terribly good to work with/debug and can be rather slow.

I have never seen a (non-trivial) page that properly renders with wkhtmltopdf, so if you need proper styling and rendering... eeeh. Be prepared to treat it as a special browser akin to IE6. It is an unmaintained and ancient fork of Chromium, basically.

Macichne Leainig
Jul 26, 2012

by VG

redleader posted:

We used EOPdf for HTML -> PDF in a previous job. I never worked with it, paid for it, or had to support it in production, but it seemed ok enough.

I used this at my previous job for the same thing. Worked well enough. We were in fintech so consistent PDF generation was a must.

SimonChris
Apr 24, 2008

The Baron's daughter is missing, and you are the man to find her. No problem. With your inexhaustible arsenal of hard-boiled similes, there is nothing you can't handle.
Grimey Drawer

EssOEss posted:

I have never seen a (non-trivial) page that properly renders with wkhtmltopdf, so if you need proper styling and rendering... eeeh. Be prepared to treat it as a special browser akin to IE6. It is an unmaintained and ancient fork of Chromium, basically.

https://selectpdf.com/community-edition/

I use SelectPdf Html To Pdf Converter, and haven't had any problems with styling and rendering. It even supports Javascript, so you can print your React app.

The community edition is free for PDFs up to 5 pages.

SimonChris fucked around with this message at 08:35 on Jun 7, 2019

His Divine Shadow
Aug 7, 2000

I'm not a fascist. I'm a priest. Fascists dress up in black and tell people what to do.
I'm working on a .net core application and I was planning on moving the database to a network server on the network instead of running on a local test DB, so I pointed the connection string to the new server, then I ran the update-database command in the package manager console and I am getting user rights issues. Ok I am not wondering about that, but this part of the message has me confused: "CREATE DATABASE permission denied in database 'master'."

WTF is this application doing trying to gently caress around with the master database in the first place? Nothing in this app has got anything to do with the system databases.

EssOEss
Oct 23, 2006
128-bit approved
Well, the master database is where information about other databases is stored. So you need access to the master database if you wish to create/modify certain aspects of other databases.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

His Divine Shadow posted:

I'm working on a .net core application and I was planning on moving the database to a network server on the network instead of running on a local test DB, so I pointed the connection string to the new server, then I ran the update-database command in the package manager console and I am getting user rights issues. Ok I am not wondering about that, but this part of the message has me confused: "CREATE DATABASE permission denied in database 'master'."

WTF is this application doing trying to gently caress around with the master database in the first place? Nothing in this app has got anything to do with the system databases.

You always need to connect to some database to run any query at all. If you want to create your own database from scratch, which this application is trying to do, it's standard to first connect to the default database 'master' (or 'postgres' or whatever), which will always exist. It doesn't mean you want to actually alter the default database at all.

Careful Drums
Oct 30, 2007

by FactsAreUseless

EssOEss posted:

Well, the master database is where information about other databases is stored. So you need access to the master database if you wish to create/modify certain aspects of other databases.

NihilCredo posted:

You always need to connect to some database to run any query at all. If you want to create your own database from scratch, which this application is trying to do, it's standard to first connect to the default database 'master' (or 'postgres' or whatever), which will always exist. It doesn't mean you want to actually alter the default database at all.

I had no idea that all connections to a SQL DB do an initial read from master. TIL. Thanks Goons

zerofunk
Apr 24, 2004

Careful Drums posted:

I had no idea that all connections to a SQL DB do an initial read from master. TIL. Thanks Goons

I may be wrong, but I'm not sure that's necessarily 100% true. SQL Server logins have a default database setting. That database is used when the connection is established. It doesn't have to be master, but that's the default.

B-Nasty
May 25, 2005

zerofunk posted:

I may be wrong, but I'm not sure that's necessarily 100% true. SQL Server logins have a default database setting. That database is used when the connection is established. It doesn't have to be master, but that's the default.

Yeah, the user account definitely doesn't need to connect to master. Just look at Azure SQL, there's a master DB there in addition to 'your' DB, but you have no access to it.

SQL Server itself needs to connect to master to allow connections to any DB, so if your master DB is hosed/unavailable, you won't be able to do anything.

edit: The default DB for your user account can be specified when the user is created/edited, or you can specify it in your connection string with Database=MyCoolDB. The latter option is probably what you're missing here, and/or you're trying to create a new database and you don't have permissions to do so.

B-Nasty fucked around with this message at 21:24 on Jun 13, 2019

EssOEss
Oct 23, 2006
128-bit approved
To avoid overgeneralizing, the specific error you are getting here talks about master because you are creating a database, which involves writing information about this new database to master. Presumably the login you are using does not have permissions for that, so it refuses.

mortarr
Apr 28, 2005

frozen meat at high speed
Anyone got any recommendations for writing an installer/msi for a wpf app? I normally do web dev, so got no ideas...

What I'd like to do is install to a user-specified dir, and set some stuff in the app.config, and be scriptable so it can be run remotely/headless by the ops team. If there's a way to deal with updates too that'd be great, but I don't know enough to figure out if the stuff I'm finding like WiX or AdvancedInstaller is actually fit for purpose.

nielsm
Jun 1, 2009



WiX is the way to go.

If you didn't need to allow headless install, customization of destination and change configuration, then I'd have suggested ClickOnce, but while it's easy it's not very flexible.

EssOEss
Oct 23, 2006
128-bit approved
I tried WiX once and found it a horrible mess of boilerplate. Trying to achieve even the simplest thing took 10 times more effort than I found reasonable. Has it improved in the last 5 years or is it just recommended because it is free?

Advanced Installer, on the other hand, worked a treat and I was very happy with my purchase.

Not sure about your specific requirements - they do not sound very complicated - but I would suggest trying Advanced Installer, as it made life quite easy for me at least.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
We use Wix for some end-user software at my place and when I've had to interact with it I've found it to be a bit complicated, but very flexible and ultimately I could do everything I needed to with it. I put the complexity of it down to installation on Windows just being complicated in general.

Whatever you do, don't try to roll your own installer.

raminasi
Jan 25, 2005

a last drink with no ice
WiX also, in my experience, is pretty stable once you've got it doing what you want. It's not intuitive to set up but once you've got it figured out you don't need to worry about it again.

mortarr
Apr 28, 2005

frozen meat at high speed
Currently using advancedinstaller - wish I could spring for the non-free version though, as it seems there are some options I'd like but am missing out on with free. I took a look at WiX, and if I was doing this for my day job I'd choose that, but I don't have the time to burn coming up to speed on it.

I did try decompiling the advancedinstaller-generated msi with dark.exe to see what the wix xml looked like, which was interesting, but it was a bit much of a mess to read clearly, and I don't want to go down the rabbit hole of tidying it and using it as a basis for future wix work.

Not sure this is the best path, but the resulting msi does what I'm after, so going to leave it at that and move on the next thing.

NoDamage
Dec 2, 2000
WiX will do the job but it has the worst documentation I've ever seen in my life. You will end up confused because there are often multiple ways of accomplishing the same task and different tutorials/docs will tell you different things based on when they were written and what version of WiX they were using at the time.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

NoDamage posted:

WiX will do the job but it has the worst documentation I've ever seen in my life. You will end up confused because there are often multiple ways of accomplishing the same task and different tutorials/docs will tell you different things based on when they were written and what version of WiX they were using at the time.

It's really awful, and it doesn't help that it works alongside project files like .csproj, which if you're not using core are their own gigantic piles of XML that you better not gently caress up, mister!

Edit: I converted a big project at work from using manually managed libraries (reference include hint path=path to binary included in source control ARGHGHGHGHG) to letting the package manager do its thing. Took way too long to set up a basic "harvest all the binaries in the output directory and include them in the installer".

Che Delilas fucked around with this message at 18:44 on Jun 21, 2019

Mr Shiny Pants
Nov 12, 2012

Che Delilas posted:

It's really awful, and it doesn't help that it works alongside project files like .csproj, which if you're not using core are their own gigantic piles of XML that you better not gently caress up, mister!

Edit: I converted a big project at work from using manually managed libraries (reference include hint path=path to binary included in source control ARGHGHGHGHG) to letting the package manager do its thing. Took way too long to set up a basic "harvest all the binaries in the output directory and include them in the installer".

You'd think installers would be a solved problem by now. Especially for the OS with the largest consumer install base in the world. Especially using the manufacturers own development environment.

But I also thought that about GUI toolkits......

EssOEss
Oct 23, 2006
128-bit approved
I look at golang's "compiles to a single exe" with envy! To be fair, you can sort of achieve that in .NET too by embedding the assemblies into the exe but it requires extra hassle (and I bet does not work with netcore).

Potassium Problems
Sep 28, 2001

EssOEss posted:

I look at golang's "compiles to a single exe" with envy! To be fair, you can sort of achieve that in .NET too by embedding the assemblies into the exe but it requires extra hassle (and I bet does not work with netcore).

The ability to generate a single exe is going to be in. NET core 3 https://www.hanselman.com/blog/MakingATinyNETCore30EntirelySelfcontainedSingleExecutable.aspx

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

EssOEss posted:

I look at golang's "compiles to a single exe" with envy! To be fair, you can sort of achieve that in .NET too by embedding the assemblies into the exe but it requires extra hassle (and I bet does not work with netcore).

netcore 3 supports self-contained executables.

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison
it'll also support tree trimming and AOT compilation so you can make small + fast startup single binaries which should be cool for writing utility applications

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Added a unit test project to a solution I'm working on as a personal project. The unit test project itself is .NET Core 2.1 and the project it is testing is .NET Standard 2.0.

Foo.Bar.csproj:
code:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AssemblyName>Foo.Bar</AssemblyName>
    <RootNamespace>Foo.Bar</RootNamespace>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
    <WarningsAsErrors />
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>

</Project>
Foo.BarTests.csproj:
code:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
    <PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Bar\Foo.Bar.csproj" />
  </ItemGroup>

</Project>
When I run the tests in the project by clicking "Run All" in the Test Explorer pane, VS prints out this message:

[22/06/2019 21:42:42 Warning] Test run will use DLL(s) built for framework .NETCoreApp,Version=v1.0 and platform X86. Following DLL(s) do not match framework/platform settings.
Foo.BarTests.dll is built for Framework 2.1 and Platform AnyCPU.
Go to http://go.microsoft.com/fwlink/?LinkID=236877&clcid=0x409 for more details on managing these settings.

Why the hell is it printing this warning? What's "Framework 2.1"? That link printed as part of the message is to a page with a "this page is no longer being updated" banner at the top, that talks about the difference between 32-bit and 64-bit tests - it seems totally irrelevant.

The tests do actually run, but I'm paranoid that it'll arbitrarily stop running them at some point in the future. The reason I am paranoid about this is that at work, we had a build that was supposed to be running unit tests as part of the build process, but after an upgrade of the toolset on the build server the unit tests stopped running because they were no longer being detected (and Microsoft had made the extremely stupid decision that if you pointed vstest.console.exe at a DLL and told it "hey go run the tests in this DLL", and it didn't find any tests in the DLL, it should just print out a warning instead of what would be sensible, i.e. returning a failure code which would have caused our build to fail). So for a period of months we weren't running the unit tests we thought we were running on every build.

I'm running VS version 16.1.3. If I go Help -> Check for Updates, Visual Studio claims that it is up-to-date.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Hammerite posted:

Added a unit test project to a solution I'm working on as a personal project. The unit test project itself is .NET Core 2.1 and the project it is testing is .NET Standard 2.0.

When I run the tests in the project by clicking "Run All" in the Test Explorer pane, VS prints out this message:

[22/06/2019 21:42:42 Warning] Test run will use DLL(s) built for framework .NETCoreApp,Version=v1.0 and platform X86. Following DLL(s) do not match framework/platform settings.
Foo.BarTests.dll is built for Framework 2.1 and Platform AnyCPU.
Go to http://go.microsoft.com/fwlink/?LinkID=236877&clcid=0x409 for more details on managing these settings.

Why the hell is it printing this warning? What's "Framework 2.1"? That link printed as part of the message is to a page with a "this page is no longer being updated" banner at the top, that talks about the difference between 32-bit and 64-bit tests - it seems totally irrelevant.

The tests do actually run, but I'm paranoid that it'll arbitrarily stop running them at some point in the future. The reason I am paranoid about this is that at work, we had a build that was supposed to be running unit tests as part of the build process, but after an upgrade of the toolset on the build server the unit tests stopped running because they were no longer being detected (and Microsoft had made the extremely stupid decision that if you pointed vstest.console.exe at a DLL and told it "hey go run the tests in this DLL", and it didn't find any tests in the DLL, it should just print out a warning instead of what would be sensible, i.e. returning a failure code which would have caused our build to fail). So for a period of months we weren't running the unit tests we thought we were running on every build.

I'm running VS version 16.1.3. If I go Help -> Check for Updates, Visual Studio claims that it is up-to-date.
Have you tried making a runsettings file?

I would suggest providing a run settings file and having it specify TargetPlatform, etc. explicitly (rather than relying on it figuring out what to do by itself.)

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Bruegels Fuckbooks posted:

Have you tried making a runsettings file?

I would suggest providing a run settings file and having it specify TargetPlatform, etc. explicitly (rather than relying on it figuring out what to do by itself.)

I had not tried that. I tried adding this file and telling VS to use it via Test -> Test Settings -> Select Test Settings File:

code:
<?xml version="1.0" encoding="utf-8"?>

<RunSettings>
  <RunConfiguration>
    <MaxCpuCount>1</MaxCpuCount>
    <ResultsDirectory>.\TestResults</ResultsDirectory>
    <TargetPlatform>x64</TargetPlatform>
    <TargetFrameworkVersion>FrameworkCore10</TargetFrameworkVersion>
  </RunConfiguration>
</RunSettings>
The error message still appears (but with "x86" replaced with "x64"). I think the real thing I need to be on top of is framework versions rather than whether the CPU is x86 or x64 (I could be wrong about that)...

I did try "FrameworkCore21" and "FrameworkCore20" as the TargetFrameworkVersion - neither of those is valid, it prints out an error message and doesn't run the tests.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Hammerite posted:

I had not tried that. I tried adding this file and telling VS to use it via Test -> Test Settings -> Select Test Settings File:

code:
<?xml version="1.0" encoding="utf-8"?>

<RunSettings>
  <RunConfiguration>
    <MaxCpuCount>1</MaxCpuCount>
    <ResultsDirectory>.\TestResults</ResultsDirectory>
    <TargetPlatform>x64</TargetPlatform>
    <TargetFrameworkVersion>FrameworkCore10</TargetFrameworkVersion>
  </RunConfiguration>
</RunSettings>
The error message still appears (but with "x86" replaced with "x64"). I think the real thing I need to be on top of is framework versions rather than whether the CPU is x86 or x64 (I could be wrong about that)...

I did try "FrameworkCore21" and "FrameworkCore20" as the TargetFrameworkVersion - neither of those is valid, it prints out an error message and doesn't run the tests.

I'm looking into this more and I'm seeing results like:

https://developercommunity.visualstudio.com/content/problem/579073/test-discovery-reporting-dlls-do-not-match.html

It might just be an open bug with visual studio 2019.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Yes, it looks like that. Thanks for finding that. OK, I guess I'll just have to keep an eye on it.

Adbot
ADBOT LOVES YOU

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
How can I use reflection in C# to create an array (as in T[], not Array)? All I've found involves an explicit cast, but I don't necessarily know the type.

What I'm trying to do is take a List of objects and feed it into a function that has a params field. I have its MethodInfo and can poke the parameters. The List has all the arguments for the params field just appended on. I need to collapse them down to an array, apparently; I got type exceptions otherwise. I'd love to know if there's a better way, by the way. So if I have:

code:
void Foo(string a, int b, params float[] c)
I might wind up with an input List of:
"bar", 1
"bar", 1, 2.0, 3.0, 4.0

I need to convert these into:
"bar", 1, null
"bar", 1, [2.0, 3.0, 4.0]

...and of course use ToArray() to finish the deal. It looks like I can't use Array as an argument instead of T[]. I don't know T at compile time; I just know it for this example. Yes, there are some assumptions about my list and I assume I have to do something to cast the individual elements too.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply