Tag Archives: ASP.NET

How to Use C# 7 Value Tuples in ASP.NET MVC

With the release of Visual Studio 2017, I wanted to use the new Value Tuples that were introduced in C# 7 in a MVC project I was working on. However, I ran into some issues which I discuss below.

Firstly, Value Tuples are included with .Net 4.7, but if the project’s target framework is lower than that, they can still be used by installing the System.ValueTuple nuget package.

image thumb How to Use C# 7 Value Tuples in ASP.NET MVC

After either using .Net 4.7 or installing the nuget package, when attempting to use Value Tuples you may find that the project fails to build, with no errors in the error log, and errors in the output log like error CS1026: ) expected. This is due to the Microsoft.Net.Compiliers nuget package that is included by default when creating a new MVC project is version 1.3.2. This needs to be updated to at least version 2.0.1.

There is also an error that occurs when using the Value Tuples in Razor views:

The type ‘ValueType’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.
This can be resolved by adding a reference to System.Runtime in the compilation section of the web.config e.g:

    
      
        
      

    

However even after fixing this error, the use of Value Tuples in Razor views is still partially limited. Trying to directly set the model of a view to be a Value Tuple or using the new instantiation syntax (e.g. (Name: “John Smith”, Age: 25)) in a Razor view gives a “Feature ‘tuples’ is not available in C# 6.  Please use language version 7 or greater” error.

image thumb 1 How to Use C# 7 Value Tuples in ASP.NET MVC

However, if the tuple is a property of the model, that it can be used without errors.

For an example of how the Value Tuples can be used I have created a small test application. That allows a user to update their current Business Unit.

image thumb 2 How to Use C# 7 Value Tuples in ASP.NET MVC

image thumb 3 How to Use C# 7 Value Tuples in ASP.NET MVC

In this method we are first deconstructing a Value Tuple that has been returned by the GetLoggedInUser method, this allows us to use the results as normal variables since in this case we only need the name. We are also retrieving the users current Business Unit and a List of all Business Units that are returned as Value Tuples.

image thumb 4 How to Use C# 7 Value Tuples in ASP.NET MVC

Here you can see that we can still use the named parameters of the Value Tuples in the view, including the ones in a List. This is much nicer then the Item1… syntax of the regular tuples.

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog

How to Display Informative Errors from ASP.NET Web API in Dynamics 365

When developing a Web API to interact with Dynamics 365, the model of interaction may be like so:

Plugin performs logic, makes call to Web API (GET, PUT, POST, etc.) -> Web API receives data, performs logic, sends response to Plugin -> Plugin receives response and performs logic with it.

However, if the Web API encounters an error while performing the internal logic, often the plugin will receive an unhelpful error message along the lines of “500 Internal Server Error”. This is due to an error occurring within the internal logic of the Web API. But this by itself is quite unhelpful, and contains no useful information that can be used to debug the issue.

This is particularly the case when used in conjunction with Sandboxed plugins in 365 Online, where you may be using a Web API to perform logic that you cannot accomplish within sandboxed plugins (SQL operations, performing logic from 3rd party libraries, etc.).

Take SQL operations for example. When performing a SQL operation that requires authenticating to a remote SQL database, you may want to know whether you’ve failed to authenticate, and whether that was because of a login error, an error with the database, or some other error. If you lose this error somewhere along the process of it being passed to the plugin, the issue will become a lot more difficult to troubleshoot. So how do you preserve the error messages until the end?

Catch Specific Exceptions in Your Web API

If you know the specific regions of code within the web service that the issues may occur, it is possible to either put try catch blocks around these sections, or check the data received for issues using conditionals before processing. For example, with an ASP.NET API, if the input for the request is not verified, it is possible for a null or incorrectly formatted object to be passed as input to the function. You could then put a check at the top to test the integrity of the data, then if the data is compromised, return an error informing the client that the input was not accepted.

Return Specific Errors From Your Web API

Now that you’re catching specific errors, it’s possible to return more specific errors. When raising errors from your Web API, it’s important to use accurate error codes, so that users who are familiar with http error codes can more easily solve the issue. A list of HTTP Status Codes can be found here:
https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

When you’ve caught the error, and determined the appropriate status code to represent it, you can raise this error using the HttpResponseException. This will return the error with the specified code to the client.

clip image002 thumb How to Display Informative Errors from ASP.NET Web API in Dynamics 365

If you want to add a little more information to your error, you can fill out the body of the response also:

 How to Display Informative Errors from ASP.NET Web API in Dynamics 365

This allows you more direct control over the information transmitted back to the client. This will also make debugging from Dynamics 365 much more efficient. The above code is from a try catch block, where the program is trying to connect to a SQL database. Any errors raised by the connection (invalid password, invalid username, invalid connection url, etc.) are directly inserted into the ReasonPhrase for the Response Message, and returned with that to the client.

Catch and Display the Error in Dynamics 365 – Plugin

Once you have the Web API throwing useful errors, you’ll need to catch and interpret these in your plugin. This is actually fairly simple. Simply place your API operation into a try/catch block, and instead of catching a generic exception, catch a WebException – this is the type of exception returned by the Web API should it encounter any issues. You can then format this exception, and raise it as a plugin exception, which will provide a nice error message for the user. An example of how to do this is given below.

 How to Display Informative Errors from ASP.NET Web API in Dynamics 365
 
This returns an error similar to the one below.

 How to Display Informative Errors from ASP.NET Web API in Dynamics 365Which when debugging, is much more useful to the user than a generic Internal Server error. Throwing an InvalidPluginExecutionException also allows you to prevent the plugin from continuing to process with an invalid return from the Web API, however, you could use another method of handling and displaying this error, like tracing it to the plugin trace log. Note that in this example, we are simply raising a Plugin Exception with our formatted error string as input, but it’s also possible to pass an exception object to the InvalidPluginExecutionException, and there are also other parameters you may find useful.

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog