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
Potassium Problems
Sep 28, 2001
It should work if you define the property as a nullable Guid, but you'd have to manually set it to null if you have an empty one. Don't know if there's some kind of mapping you could do in EF.

code:
public class Person 
{
  public Guid PersonId { get; set; }

  public Guid? ReportsToId { get; set; }

  public Guid BusinessId { get; set; }
}

Adbot
ADBOT LOVES YOU

Potassium Problems
Sep 28, 2001

Small White Dragon posted:

2) BitConverter uses the endianness of the machine it's on. Is there a way to use network byte order?

I've always used something like this:

code:
if(BitConverter.IsLittleEndian)
{
  Array.Reverse(bytes)
}

Potassium Problems
Sep 28, 2001

CapnAndy posted:

So today I learned that HttpWebResponse throws an error on 404 and you have to wrap the whole thing in try/catch and you can use the exception's response code in the catch segment.

CHECKING TO SEE IF THE WEBSITE IS THERE IS EXACTLY WHAT I'M USING YOU FOR, YOU STUPID CALL, WHY DO YOU NOT KNOW HOW TO HANDLE loving 404S


I just needed to vent.

It also throws an exception on 304 Not Modified! I hated seeing "The remote server returned an error: 304 (Not Modified)" because the server wasn't in the wrong :argh: If you have the option, using HttpClient in System.Net.Http (.NET 4.5 I think?) is much better.

Potassium Problems
Sep 28, 2001
Nope, the streams get disposed of when the code exits the using block (assuming you're not manually disposing of them in your methods). You're correct in that you need to start each Process method with a seek on the input stream back to its beginning, assuming the streams support seeking.

Edit: whoops, you're right, you need to get rid of the using blocks in your Process methods. They'll kill the streams for sure.

Potassium Problems fucked around with this message at 22:27 on Sep 9, 2015

Potassium Problems
Sep 28, 2001
I'd recommend having them use MiniProfiler in their apps (http://miniprofiler.com/), which will show them issues with the queries EF generates, but to really fix this your devs are going to have to get a better understanding of SQL, and realize that just because the ORM generated a statement for you it doesn't mean it's good or correct.

As far as tutorials, this one was published a couple days ago on the official asp.net site.

edit- whoops, MVC 5 is definitely not beta, got mixed up

Potassium Problems fucked around with this message at 16:56 on Oct 29, 2015

Potassium Problems
Sep 28, 2001

The Wizard of Poz posted:

I have an MVC5 project that I'm working on that, after receiving a file upload, has to notify a person by email that the upload has occurred. I want to hand this off to a separate process so that the user can get the HTTP response back and continue on their way without waiting for the email to send, since it's not a critical part of the process from the user's point of view and there's no reason they should wait.

I think the key lies in the asynchronous features of C# but I have literally no exposure to this and I don't really have the time to fully grok it right at this moment. What I kind of need for now is a basic introduction with maybe a clear example - does anyone know a good resource for this?

I've been using Hangfire for stuff like this, and for scheduled tasks.

example:

code:
BackgroundJob.Enqueue<MailService>(service => service.FileUploaded("recipient@domain.com"));
It requires a database for job storage though, so it might be overkill depending on what your app is doing.

Potassium Problems
Sep 28, 2001

ljw1004 posted:

I wonder where other folks here get their "joy of coding" from?

For me, a bottle of Maker's Mark

Potassium Problems
Sep 28, 2001

Xeom posted:

code:
double[] returnArry = { a, b };
return returnArry;
Anyway to get rid of the returnArry, and just pass my variables?

Is this what you're asking for?
code:
return new double[] { a, b };

Potassium Problems
Sep 28, 2001

Munkeymon posted:

IIS is recycling the app pool. You have to keep it loaded in memory by hitting the site constantly with a script or configure IIS to only recycle if it leaks too much memory.

In addition to this, you might want to look at the application initialization module for IIS, which will restart your site after a recycle

Potassium Problems
Sep 28, 2001

Ciaphas posted:

I guess the only thing I can think of is something like this:

C# code:
DoTotallySafeStuff();
try
{
    UnsafeStuff(); // can throw FooBarException but not WowYouFuckedUpException (at least it's not, er, expected to???)
    try
    {
        ReallyUnsafeStuff(); // can throw FooBarException or WowYouFuckedUpException
    }
    catch (WowYouFuckedUpException e) { }
}
catch (FooBarException e) { }
Looks kinda gross, but I'd need more context :shrug:

Wouldn't something like this work to avoid a nested try/catch?
C# code:
DoTotallySafeStuff();
try
{
    UnsafeStuff(); // can throw FooBarException but not WowYouFuckedUpException (at least it's not, er, expected to???)
    ReallyUnsafeStuff(); // can throw FooBarException or WowYouFuckedUpException
}
catch (FooBarException fbe) 
{ 
}
catch (WowYouFuckedUpException wyfue)
{
}

Potassium Problems
Sep 28, 2001

Shy posted:

I'm pretty sure it appeared long time ago and asp.net propositions weren't there yet.

yeah IIRC Nancy's been around since 2010, as a .NET answer to Sinatra

Potassium Problems
Sep 28, 2001
They've added HttpClientFactory in .NET Core 2.1 (currently in preview/RC), in part to help devs deal with stuff like that

https://blogs.msdn.microsoft.com/webdev/2018/02/28/asp-net-core-2-1-preview1-introducing-httpclient-factory/

Potassium Problems
Sep 28, 2001

epalm posted:

I'm considering rolling my own software licensing scheme. Among other things, the software, when sold, will be licensed to run on a particular computer. I'm looking for a resilient way to identify "a computer" (where the OS in my case will be one of Windows 7, 10, or Windows Server 2008, 2012, 2016). There's lots of literature recommending different ways to do this. Some common ones I don't like are...
  • Hard drive volume number, but hard drives are essentially "consumable" and get replaced too often.
  • Windows serial number, but again, if the HDD blows up, that'll change if there's no backup and Windows gets reinstalled.
  • MAC address, but this can easily be spoofed, and there can be more than one, and they're not always in the same order.
I'm thinking of using motherboard serial number, which I can get on the console with wmic baseboard get serialnumber, and in C# with
C# code:
var moboSerial = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard")
    .Get()
    .Cast<ManagementObject>()
    .FirstOrDefault()?
    .GetPropertyValue("SerialNumber")?
    .ToString();
I've run this query on a dozen PCs, and a couple virtual machines on Azure, and so far the values I'm seeing are unique and palatable (20-30 alphanumeric characters). I do understand that nothing is foolproof, so this is on a "best effort" basis. Just something I can put in place that, if (when?) circumvented, would have to be done willfully and not accidentally. I'm at the point where I can say "this seems fine", which usually comes before "oops, this isn't going to work at all" when applied to a larger scale (hundreds of computers).

Anyone have experience with this?

I've recently had to do this as well, I used this NuGet package to generate a unique machine id, which I believe just uses WMI under the hood anyway. It lets you include different device values to come up with a reasonably unique identifier, or implement your own device component if the built-in ones aren't enough. For my use case, it was acceptable to include only the motherboard serial, OS & MAC address, but these were for machines under our control and any spoofing/tampering with those values would be a larger problem.

Edit: gonna also recommend https://keygen.sh/, software licensing as a service. Not free, so may not be an option.

Potassium Problems
Sep 28, 2001

NihilCredo posted:

Has anybody used Hangfire for background tasks? I saw it mentioned in a HN commenti and it looks pretty good, would save us a good chunk of boring code.

Yep, been using it for a few years to run early morning tasks (update this, email that, etc) and it's worked like a charm.

Potassium Problems
Sep 28, 2001

New Yorp New Yorp posted:

I've worked at and consulted at enough places where NIH syndrome resulted in everyone struggling to use clunky bespoke software that poorly implements a subset of the functionality of any number of robust, SaaS offerings.

Despite my objections, I authored and maintain a clunky bespoke time tracking / ticketing system at my job and this entire topic touches my very soul.

Also more power to Long Sack, let he who has not written a blog throw the first exception

Potassium Problems
Sep 28, 2001

LongSack posted:

Am I the only one that finds it odd that you have to overload the '>=' and '<=' operators explicitly even if you overload '==', '<' and '>' already? Is there a reason for this besides "that's just the way it is"?

I would imagine under the hood they are distinctly different operators (Equals, LessThan, LessThanOrEqualTo, GreaterThan ...) and are handled as such, and the `>=` & `<=` operators aren't just syntactic sugar, if that makes sense.

I would find it odd you're overloading operators in the first place :v:

Potassium Problems
Sep 28, 2001

LongSack posted:

Why is that surprising? Two Account objects are equal if they have the same AccountId whether or not the two objects are the same reference. And IComparable<T>, where it makes sense, is something like
C# code:
public int CompareTo(Firewall other) => Name.CompareTo(other.Name);

Like B-Nasty said, it easily leads to unclear code, and usually other devs (maybe even yourself, 6 months down the line) will have to dig into the overload code to see what the intent really is. I've seen really obtuse stuff from people who thought they were being super clever. Sanitized example:

code:
StatWeight weight = GetWeight();

if(weight <= 0.75) 
{
  ...
}
There's an overload and an implicit operator at work here, and it makes it extremely hard to reason about the code w/o knowing the details of how a StatWeight can become a double (or is it a float? decimal maybe?) and how it's performing its operations. Tread carefully!

Potassium Problems fucked around with this message at 21:27 on Feb 27, 2019

Potassium Problems
Sep 28, 2001

Rocko Bonaparte posted:

I'm trying to use some reflection to expose some C# functions in my toy language interpreter. It looks like I should extract a MethodInfo to get to parameter bindings, but it seems clumsy.
code:
var methodInfo = type.GetMethod("SomeMethod")
Why do I have to use a string here? If I have the method in hand, is it possible to extract information about it and work with it dynamically directly? Is there function where I can give it the method and just get the junk from it? Something like:

code:
var methodInfo = SomeClass.SomeMethod.GetMethod()

Other than "That's just how the reflection APIs work" I dunno what to tell you, except you can and should use nameof instead of a magic string

code:
var methodInfo = type.GetMethod(nameof(SomeClass.SomeMethod));

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

Potassium Problems
Sep 28, 2001
Are you talking about an open generic? like typeof(List<>)

Potassium Problems
Sep 28, 2001

New Yorp New Yorp posted:

Your classes are too big and doing too much.

it's this. also, the decorator pattern might help with concerns like logging.

Potassium Problems
Sep 28, 2001
David Fowler tweeted about this the other day, I think this is what you're looking for?

https://twitter.com/davidfowl/status/1484985441832951810?s=21

Potassium Problems
Sep 28, 2001
Whoops, I completely missed the .NET 4.8 part. Sorry!

Potassium Problems
Sep 28, 2001
Function method bindings are separate from the dependency injection container, if you want an available IConfiguration instance, you'll have to make it so that the class/function isn't static and inject IConfiguration into the class constructor

Potassium Problems
Sep 28, 2001
I don't know what you mean, and maybe it's changed with .NET 5/6, but the way function parameter bindings happen (i.e., ILogger, Trigger-specific arguments like a service bus message or HTTP request object) isn't aware of the dependencies you wire up in Startup.cs, which is why the op is getting binding errors. You have to have a non-static function in a non-static class and wire up the constructor to take a dependency on IConfiguration.

code:
public class Function
{
   private readonly IConfiguration _config;

   public Function(IConfiguration config) => _config = config;

   [FunctionName("Func")]
   public async Task<IActionResult>(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
   {
       // _config is available here
   }
}

Potassium Problems
Sep 28, 2001
I want to say that returning a type that inherits from ObjectResult will ensure you don't have to materialize the list & will stream it to the client, but I don't know if that's true or not and I can't seem to find any documentation on it
C# code:
public IActionResult GetApples()
{
    var result = service.GetApples();

    return Ok(result); //Ok method should be on ControllerBase, returns OkObjectResult
}

Potassium Problems fucked around with this message at 18:59 on Jun 12, 2023

Potassium Problems
Sep 28, 2001

epswing posted:

Oops. Ok, so I'm playing around in LINQpad8 and it must be doing some magic, enough for me to question my entire existence as a developer for a few minutes there. Glad to hear I'm not crazy, this time.



Huh, I wonder what LINQPad is doing behind the scenes, visual studio reports the expected output

Adbot
ADBOT LOVES YOU

Potassium Problems
Sep 28, 2001
Sounds like you want to implement multi-tenancy

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