Monthly Archives: April 2018

Cannot attach the file ‘C:\…database.mdf’ as database x – Entity Framework

Hi everyone,

I ran into the following error when attempting to run ‘update-database’ on an initial migration:

Cannot attach the file ‘C:\Users\…\App_Data\aspnet-…115933.mdf’ as database ‘aspnet-…15933’

The solution to this one is pretty easy, remove the initial catalog property from your connection string.

…33.mdf;Initial Catalog=aspnet…

This is apparently caused by issues with EntityFramework and multiple projects in the same database. See the following stackoverflow answer for more info: https://stackoverflow.com/a/20176660/522859

Retrieving User Id in Web API 2 Controller – .NET

Hi everyone,

Just a quick post on how to retrieve the current user’s id in a Web API 2 controller:

var userId = RequestContext.Principal.Identity.GetUserId();

Note that you’ll need the following using statements:

using Microsoft.AspNet.Identity;
using System.Web.Http;

Thanks to the following stackoverflow post for the info: https://stackoverflow.com/a/21618056/522859

Git Ignore Template for .NET

Hi everyone,

A sample .gitignore file for if you’re working with .net:

*.cache
*.dll
*.exe
*.pdb
/build/
*.suo
*.user
_ReSharper.*/
*.sdf
*.opensdf
*.tlog
*.log
TestResult.xml
*.VisualState.xml
Version.cs
Version.h
Version.cpp

Also, if you happen to have added some files you didn’t mean to:

git rm –cached file_you_dont_want.pdb
git commit -m “Remove pdb file”

Thanks to these stackoverflow posts for the info:
https://stackoverflow.com/a/8675415/522859
https://stackoverflow.com/a/11629271/522859

Import an Existing Project to BitBucket

Hi everyone,

Today I needed to import an existing project to Bitbucket. The documentation is really good, but just in case you have trouble finding it:

  1. Navigate to the root directory of your project.
  2. Run the following commands in terminal/command prompt:
    git init
    git add –all
    git commit -m “Initial Commit”
  3. Login to Bitbucket and create repository.
  4. Locate the clone URL (left menu) e.g. https://email_address.bitbucket.domain:7999/project_name/repo.git
  5. Upload your files:
    git remote add origin https://email_address.bitbucket.domain:7999/project_name/repo.git
    git push -u origin master

    Check out the following link for more info: https://confluence.atlassian.com/bitbucketserver/importing-code-from-an-existing-project-776640909.html

Unsupported_Grant_Type – MVC Web Api (error)

Hi everyone,

I ran into the following error while attempting to authenticate using .NET Web Api:

POST http://localhost:63720/Token HTTP/1.1
Host: localhost:63720
Content-Type: application/json
Content-Length: 0
HTTP/1.1 400 Bad Request
Content-Type: application/json;charset=UTF-8
Date: Mon, 16 Apr 2018 14:18:06 GMT
Content-Length: 34

{“error”:”unsupported_grant_type”}

This one was pretty straight forward. Ensure that have the correct content-type:

Content-Type: application/x-www-form-urlencoded

And finally, ensure that you provide a grant type in the request body:

grant_type=password&username=test_username&password=test_password

Thanks to the following stackoverflow post for the info: https://stackoverflow.com/a/29261024/522859

An error occurred (UnrecognizedClientException) when calling the CreateFunction operation: The security token included in the request is invalid.

Hi everyone,

I ran into the following error today while attempting to create a Lambda function using the CLI:

An error occurred (UnrecognizedClientException) when calling the CreateFunction operation: The security token included in the request is invalid.

The first thing to check is that your aws config is setup correctly. For me this is under c:\users\chris-pc\.aws\.

If that looks fine, double check that the associated user has the correct rights by viewing the IAM page in the server console: https://console.aws.amazon.com/iam/home

Finally, ensure that the user has programmatic access and that the keys match those in your config.

FAILED. Reason: Requires capabilities : [CAPABILITY_IAM] – AWS SAM

Hi everyone,

I ran into an error today while trying to deploy using AWS SAM:

aws cloudformation deploy –template-file C:\Users\xxx\serverless-output.yaml –stack-name events-app
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Requires capabilities : [CAPABILITY_IAM]

The solution is pretty straightforward. You need to explicitly add –capabilities CAPABILITY_IAM to you command:

aws cloudformation deploy --template-file C:\Users\xxx\serverless-output.yaml --stack-name events-app --capabilities CAPABILITY_IAM>

Thanks to Bfreis for his comment on this github issue: https://github.com/awslabs/serverless-application-model/issues/51

Serverless Offline Error – sls offline start

Hi everyone,

I ran into the following error while trying to start serverless offline:

serverles events.js:160
throw er Unhandled ‘error’ event spawn java ENOENT at exports._errnoException (util.js:1020:11) at Process.ChildProcess._handle.onexit (internal/child_process.js:197:32) at onErrorNT (internal/child_process.js:376:16) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickDomainCallback (internal/process/next_tick.js:128:9)

Solution
It turned out to be because I didn’t have the Java JDK installed.

I expect you’ll have the same problem if the command prompt doesn’t have access to the JAVA_HOME variable or if your JAVA_HOME path isn’t set correctly.

AWS CLI Copy from Bucket – An error occurred (AccessDenied) when calling the CopyObject operation: Access Denied

Hi everyone,

I ran into the following error while trying to copy files from one bucket to another using the AWS CLI:

An error occurred (AccessDenied) when calling the CopyObject operation: Access Denied

Thankfully this one is pretty self-explanatory. My user was missing the permissions required to view the bucket.

To see your user: aws configure list
To add permissions: view the IAM Management Console

Thanks,
Chris

AWS CLI Copy to Bucket – Could not connect to the endpoint URL

Hi everyone,

I ran into the following error while attempting to copy files to a bucket via the AWS CLI:

Could not connect to the endpoint URL: “https://wildrydes-us-east-1.s3.wildrydesxxxx.amazonaws.com/?prefix=WebApplication%2F1_StaticWebHosting%2Fwebsite%2F&encoding-type=url”

The solution turned out to be pretty straight-forward. The region on the cli-config was set incorrectly. For example, mine was set to “Sydney” instead of “ap-southeast-2”. Type “aws configure list” to check what yours is set to.

Thanks to the following stackoverflow post for the info: https://stackoverflow.com/a/40411174/522859