Browsed by
Tag: .NET

.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”);

Enable web deploy on IIS 8 running on 2012 R2

Enable web deploy on IIS 8 running on 2012 R2

Web deploy enables a developer (or anyone with sufficient privileges) to build and deploy a website using visual studio or via the command line using MSBuild. An example of such a build command can be found in my previous posts. We use it mainly in our continuous integration environment to automatically build, test and deploy new code if a new commit to the git master branch was detected. Installation via web platform installer First you will need administrator rights to…

Read More Read More

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

Mark validation errors

Mark validation errors

I faced a problem today that I needed to give controls, that failed validation, an error class. I found this little gem online: function WebForm_OnSubmit() {var errorControls = new Array();if (typeof (ValidatorOnSubmit) == “function” && ValidatorOnSubmit() == false) {for (var i in Page_Validators) {try {var control = document.getElementById(Page_Validators[i].controltovalidate);if (!Page_Validators[i].isvalid) {errorControls[errorControls.length] = control;}control.className = “”;} catch (e) { }}for (var i in errorControls) {errorControls[i].className = “errorClass”;}return false;}return true;} Where “errorClass” is the class you want to give your controls. Thank you…

Read More Read More