Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367 Scattered Code – Just another opsAI site

SQL Server hide database list from a particular user

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

One of the things I've been searching for is to hide the list of databases a user doesn't have access to. By default, when a user connects to SQL Server, especially a shared instance, he sees all the databases available on the server, even though he doesn't have access to view or access any of them. The goal is to only show a user the database(s) he should see. This isn't fully supported yet by SQL, but there's a way to solve the problem for the majority of cases. First,...

C# MVC Drop down List for Months

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367
public IEnumerable Months
{
    get 
    {
        return DateTimeFormatInfo
               .InvariantInfo
               .MonthNames
               .Select((monthName, index) => new SelectListItem
               {
                   Value = (index + 1).ToString(),
                   Text = monthName
               });
    }
}

RavenDB Optional Queries and Boosting

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

After implementing query-time boosting, I found that I wasn't getting the results I was expecting ... or any at all. I discovered there was an overload when adding queries that tells the query whether it "must," "must not," or "should" occur in the results. Inside a query object that inherits from QueryBase, do the following: Optional Queries // "query" creation elided // ... this.AddQuery(query, BooleanClause.Occur.SHOULD); return this; This made the query optional, allowing the other matching results to be returned. But, since it's boosted, if results are found matching this...

RavenDB “In” operation

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

RavenDB supports an "in" operator, although vaguely documented.

First, include the namespace

using Raven.Client.Linq;

Usage

var ids = new[] { "myobjects/1", "myobjects/2" };
session
  .Query<MyObj>()
  .Where(o => o.Id.In(ids));

C# MD5 Hash

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Quick script for generating an MD5 Hash

private string ComputeHash(string input)
{
  using (var md5 = System.Security.Cryptography.MD5.Create())
  {
    var data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
    
    var sb = new StringBuilder();
    
    foreach (var c in data) {
        sb.Append(c.ToString("x2"));
    }
    
    return sb.ToString();
  }
}

RavenDB Load Balancing

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

Somewhat counter-intuitive, this behavior is set at the client level, not the server. When servers are setup for replication, they create system documents of the other servers involved in replication. When the client accesses the primary server, it downloads and caches the replication information, so the client can "fail over" properly. To set this up, you need to assign the FailoverBehavior convention in your DocumentStore. Here is how we're doing it with Ninject: // RavenDB Services Bind().ToMethod(context =&gt; { // Create the DocumentStore (expensive operation). var documentStore = new DocumentStore...

RavenDB Lucene Query

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

How to build a Lucene query using extension methods, and not have the request go out until ToList() is called. private List GetMembers(string nickname) { var query = DocumentSession.Advanced.LuceneQuery(AllMembersIndex.Name); // Search for nickname if (!nickname.IsNullOrWhiteSpace()) query = query.Search("Nickname", nickname); // Execute query return query.ToList(); }

RavenDB Create static index

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

/// <summary> /// Gets the document store. /// </summary> /// The document store. /// /// Do this only once per AppDomain load. It's very expensive. /// private static IDocumentStore GetDocumentStore() { // Create the DocumentStore (expensive operation). IDocumentStore documentStore = new DocumentStore { ConnectionStringName = "RavenDB", Credentials = System.Net.CredentialCache.DefaultNetworkCredentials // For "trusted connections": see comments at http://ravendb.net/docs/client-api/connecting-to-a-ravendb-datastore }; // Read from and write to all servers. documentStore.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers | FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries; // Initialize the store (must be done before creating indexes) documentStore = documentStore.Initialize(); // Create static indexes CreateRavenStaticIndexes(documentStore); //...

Background tasks in MVC and IIS

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

As you might've noticed, keeping threads running after a request returns, for processing post operational tasks (such as performing analytics on a file that was uploaded, etc) don't always complete in a web project. There are several issues with spawning threads in the context of an ASP.NET project. Phil Haack's post explains the issues in more detail. The following classes solve the problem of IIS killing threads before they complete. First part is the IISTaskManager: using NLog; using System; using System.Threading.Tasks; using System.Web.Hosting; namespace Web.Models { /// <summary> /// Static...

Value types, reference types and practical uses

Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367

C# has two different types of variables: value types and reference types. While in C and C++ primitive types can contain values or references and certain complex types (arrays, objects) can only be used via reference, in C# the line between the two types is very clear. Numeric types(int, decimal, double, etc.), bool and structs access the values directly. Class, object, interface, delegate string and dynamic are only accessed and used via reference. Because of all the awkwardness with referencing and dereferencing in C and C++, C# uses the 'ref'...