Browsed by
Tag: C#

How to create a NuGet package

How to create a NuGet package

A few days ago I looked into setting up our private NuGet server as a way for us to share code, instead of using git sub modules for example. Turns out setting up the server is as simple as starting an empty web project in visual studio and adding the NuGet server package: http://nugetserver.net/. On a side note, if you are already using team city, you can use this as a NuGet repository, just select create artifact with the package…

Read More Read More

Unit Testing with NUnit

Unit Testing with NUnit

A very short intro and comparison between the MSTest framework and the very popular NUnit framework. For a more complete list of differences between the frameworks, see: https://xunit.github.io/docs/comparisons.html. Getting started Add a new project to your visual studio solution. I found 3 different ways to get started: Add a unit test (MSTest) project to your solution, remove the references to MSTest and add the nuget package NUnit to your project. Start with an class library and add the nuget package:…

Read More Read More

.NET Web API OData and Entity Framework Database First: No Key defined

.NET Web API OData and Entity Framework Database First: No Key defined

When using EF database first, I came across an error: No Key defined… Turns out the generated classes are not decorated with the [Key] attribute. You can manually add them but once you generate your model(s) again, it’s all gone. There are workarounds floating across the internet to edit the T4 template(s) that generate these models, but every developer has to do that, plus I didn’t want to fiddle around with those files. Another way is to map your key(s)…

Read More Read More

.NET Web API and OData: Cannot serialize a null ‘feed’

.NET Web API and OData: Cannot serialize a null ‘feed’

Recently I came across an error when working with oData and web API. Cannot serialize a null ‘feed’. System.Runtime.Serialization.SerializationException I couldn’t really find a solution online, but it turns out i just forgot to decorate my method parameter with [FromODataUri]. [EnableQuery] public IQueryable Get([FromODataUri] ODataQueryOptions queryOptions) { … } Now you can use $expand and $select in your URL without issues.

C#.NET: Does object have method or property?

C#.NET: Does object have method or property?

I wanted to see if I could determine if an object had a certain property. I came across this post, that explained how to do this using reflection in .NET. Here is what the little extension class looks like: public static class Extensions { public static bool HasMethod(this object objectToCheck, string methodName) { var type = objectToCheck.GetType(); return (type.GetMethod(methodName) != null); } public static bool HasProperty(this object objectToCheck, string propertyName) { var type = objectToCheck.GetType(); return (type.GetProperty(propertyName) != null); }…

Read More Read More

Keyset does not exist error in .NET application

Keyset does not exist error in .NET application

When using WCF to communicate with a web service that required a client certificate, I couldn’t get the solution to run unless I started Visual Studio as an administrator. The error that I got was : System.Security.Cryptography.CryptographicException Keyset does not exist I knew full well the private key did exist (you can see this in MMC > Certificates). So it had to be a security related issue, seeing that when I run it as admin I have no issues. After…

Read More Read More

Intercept raw soap messages in code using WCF

Intercept raw soap messages in code using WCF

I faced the problem today that I needed to intercept the soap messages that are send to a webservice using WCF. To make things more complicated I needed to have them as a variable in my code and most of the stuff I found online was how to enable tracing/logging. A very good example to enable tracing is found on this website. After some extensive google’ing I came across this post. The post does an excellent job of describing how…

Read More Read More

Use environment variables in .NET

Use environment variables in .NET

Ever wanted to use environment variables in .NET ? Just using “%UserProfile%\path to file” won’t work as it will be added to the startup path. Use this little “trick” to use those variables: Environment.ExpandEnvironmentVariables(“%UserProfile%\Desktop\myfile.txt”);

How to deploy a web site project using msbuild

How to deploy a web site project using msbuild

A website project differs a bit from a normal web application. The main thing is that it doesn’t require compilation, so how do I deploy this then? A “normal” msbuild deploy will look like this: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 MSBuild “C:\STAGING\website\\.sln” /p:DeployOnBuild=true /p:PublishProfile=ToProduction /p:WebPublishTargetsVersion=11.0 /p:username= /p:password= If you try to use this on a web site project (provided you created a solution file for it), you will get following error: The precompilation target directory cannot be in the same tree as the…

Read More Read More