Archive

Posts Tagged ‘.NET’

Microsoft Makes MSTest v2 Open Source


In case you have missed the news. Microsoft has opened sourced the MSTest v2 framework. MSTest v2 is the latest version of MsTest Framework. It is cross-platform which means it can be executed on Windows, Linux, and Mac. It can be used to test desktop, mobile, web, or library .NET applications. And the best part, it is distributed though Nuget as a package.

The advantages of this approach are we

  • no long must wait for a new release or update for the .NET framework nor Visual Studio;
  • we don’t have to wait for Microsoft to fix an issue. It is open source; we can submit a pull request on GitHub with the fix;
  • Code and roadmap are transparent

 

For more info visit the following links

https://github.com/Microsoft/testfx-docs/blob/master/roadmap.md

https://github.com/Microsoft/testfx

About Lajak Technologies

A consulting firm in Ottawa, Ontario that provides services related to Microsoft technologies, Team Foundation Server, DevOps practices, security and more. Contact us today to help you solving your complex software problems.

Advertisements
Categories: .NET Tags: , , ,

Replace JavaScript serializer in Kendo controls with NewtonSoft


I have been doing code optimization for an ASP MVC 5 application that uses the Telerik Kendo controls. Some of the search results may return 7000 records in this project. After I had replaced the serializer, it took 30% less time to load the search results page.

 

The ASP MVC Helpers for Kendo controls by default use the System.Web.Script.Serialization.JavaScriptSerializer which is slow compared to the NewtonSoft Json serializer. Telerik made it easy to replace the default serializer.

Steps:

  • Created a class that implements IJavaScriptSerializer and implement the Serialize method.
    public class CustomKendoJsonSerializer : IJavaScriptSerializer
    {
       public string Serialize(object value)
       {
          return JsonConvert.SerializeObject(value); //use the serializer you want  here
       }
     }
    
  • Create a class that implements JavaScriptInitializer and implement the CreateSerializer method
    
    public class CustomKendoInitializer : JavaScriptInitializer
    
    {
    
    public override IJavaScriptSerializer CreateSerializer()
    
    {
    
    return new CustomKendoJsonSerializer ();
    
    }
    
    }
    
    
  • In the Application_Start event in the Global.asax file, register the new serializer
Kendo.Mvc.Infrastructure.DI.Current.Register<IJavaScriptInitializer>(() => new CustomKendoInitializer());
Categories: .NET, asp.net, C# Tags: , ,

ASP.NET Move Link Elements from the Body Element to Head


ASP.NET: Move Link Elements from the Body Element to Head

Today at a client site, we use a custom asp.net template and some 3rd party custom controls. to build the asp.net pages The custom template is a standard template by the company and we have to use it. Long story short, the template doesn’t initialize the Head property on the page. Hence when 3rd part custom controls finds that page.head ==null, it registers the webresources, as link elements, into the body of the page. Hence, the page fails xhtml strict validation check.

To fix that behaviour I wrote the following code:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim sbPageHtml As New StringBuilder
        'Get the html to be rendered
MyBase.Render(New HtmlTextWriter(New StringWriter(sbPageHtml)))
Dim strPagHtml = sbPageHtml.ToString()
'find the occurences of
Dim matches() As String = (From m As Match In Regex.Matches(strPagHtml, "*") _
Select m.Value).ToArray()
        'matches exist
        If matches.Length > 0 Then
'remove occurances from html
Dim toRenderPageHtml As String = Regex.Replace(strPagHtml, "*", "")
           'Write found link tags into the head tag
toRenderPageHtml = toRenderPageHtml.Replace("", String.Format("{0}", String.Join("", matches)))
            writer.Write(toRenderPageHtml)

        Else 'no matches found, no modifications required
writer.Write(strPagHtml)
        End If
    End Sub

If you can think of other solutions, feel free to post them in the comment section.

Categories: .NET, asp.net, VB Tags: , ,

Unable to write to output file [FileName] .pdb: Unspecified error Visual Studio


Today I had a very weird problem with visual studio 2010 SP1. Whenever I build the solution I keep getting “Unable to write to output file [FileName] .pdb: Unspecified error” for one of the projects.
I tried cleaning the solution, deleting files in the obj and bin folders and rebuilt, made sure that the files are not readonly. That didn’t help.

Figured out that there was a missing file in one of the projects. To know which file it is:

Close visual studio

Reopen the solution (DON’T build)

Check the Errors List

You should see

Error    1    Unable to open module file [FileName].vb : The system cannot find the file specified.    [FileName] [Project]

Categories: .NET, 2010, Visual Studio Tags: ,

WCF Data Service Exception: The LoadAsync method cannot be called when the DataServiceCollection is not a child collection of a related entity


In code you most likely called the no argument LoadAsync() method. Make sure you call pass the correcgt URI to the LoadAsync method. For example: if you service is located at http://localhost:1033/Services/PersonInfoService.svc/People

Make sure you pass the service URI without /People to the service context and pass /People to to the LoadAsync method

var people = new PersonEntities(new Uri("http://localhost:1033/Services/PersonInfoService.svc/"));
var Items = new DataServiceCollection<Person>(people);
//Pass the collection path to the LoadAsync method
Items.LoadAsync(new Uri("/People", UriKind.Relative));
Categories: .NET, C#, OData, Services, WCF Tags: , , ,

Use Red Gate SmartAssembly in Automated Team Build to Obfuscate Assemblies


This post is in response to a question asked in the TFS Automated Build Forum: http://social.msdn.microsoft.com/Forums/en-US/tfsgeneral/thread/44de4e0a-fcc0-4874-a4af-804dc458f43f/#b0c4c0a3-758a-4204-ac4b-d4e1e93e2d75.

Smart Assembly is a product to obfuscate .NET assemblies and automated error reporting. For more information about the tool, you can visit http://www.red-gate.com/products/dotnet-development/smartassembly/

To answer the question in the MSDN forum, I downloaded and installed the trial version Smart Assembly from Red Gate’s website. There wasn’t anything tricky while installing the product and it was straight forward.

I had to create a smart assembly project (.saproj) file which is an xml file that contains all the features that need to be enabled and their configuration. Smart Assembly comes with its configurator which makes it really easy to configure the features to be used. The following is a screen shot of the configurator

Follow these steps to create a project

  • Click New Project
  • Click Browse Assembly button
  • Select the file to be obfuscated, you want to select the dll or exe file located in the bin\release folder

  • Click Set Destination button, the configurator doesn’t allow you to select the same destination file as the input one using the same folder (i.e. over write the input one)

  • I have set the destination to be bin\SmartAssembly\SmartAssemblyPOC.exe
  • Notice the toolbar, clicking on toolbar buttons auto scrolls to the respective section

  • Check the assembly that you want to obfuscate
  • Click Save project on the left list
  • Save the saproj at the same directory where the project is saved. You can choose other location if you want to
  • Test your project by clicking Build on the left

Usually we don’t have one project in a solution. Therefore it is not practical to open the smart assembly tool for every project and build it manually. Luckily, we can integrate building the smart assembly project while building the project using MSBuild.

I basically followed the instruction in this link;

  • Right click on your project
  • Unload the project
  • Find the following line if you have a vb project

<Import Project=$(MSBuildToolsPath)\Microsoft.VisualBasic.targets />

  • Or this line if you are using C#

<Import Project=$(MSBuildToolsPath)\Microsoft.CSharp.targets />

  • Below the above line add the Smart Assembly targets

<UsingTask TaskName=SmartAssembly.MSBuild.Tasks.Build
AssemblyName=SmartAssembly.MSBuild.Tasks, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 />

<Target Name=AfterBuild
Condition= ‘$(Configuration)’ == ‘Release’ >
 <SmartAssembly.MSBuild.Tasks.Build ProjectFile=SmartAssemblyPOC.saproj
input=$(OutDir)SmartAssemblyPOC.exe

output=$(OutDir)SmartAssemblyPOC.exe

OverwriteAssembly=true

MarkAsReleased=true/>
</Target> 

  • Save the project
  • Load the project from solution explorer
  • I will quickly explain what the above lines mean. The UsingTask element maps the AssemblyName to the TaskName. Version is the version number of the SmartAssembly. If you are using version 5, change the version number to 5.0.0.0. AfterBuild target is executed after the build is complete to obfuscate the smartassemblyPOC.exe in the release folder and then override it on the same folder. The $(OutDir) will be replaced by MSBuild with output folder of your assembly. The input and output in the MSBuild task overrides the input and output in the saproj
  • Build the project in Visual studio to make sure it compiles and the assembly gets obfuscated
  • It is not necessary to include the saproj file in the project as long as it is checked in. To include the saproj in the project:

  • Check in your changes
  • You need the SmartAssembly executable to be installed on your build machine otherwise Team Build won’t be able to find the SmartAssembly Tasks
  • After the Team build is done, view the log file
  • Notice the execution of the SmartAssembly task

How to use Team Explorer with Codeplex


I decided to write this post because many including myself found it a bit confusing to connect Team Explorer to Codeplex. I am using Visual Studio 2010 Ultimate Edition.

Create Codeplex User

  1. Click on the Register link
  2. Fill the required fields and hit the Register button

Participating in a Project

To participate in a project on Codeplex, you need to contact the project coordinator. Follow the following steps to find out who the project coordinator is:

  1. Go the project page
  2. Click on the people tab
  3. Click on the coordinator’s link

Connect to Codeplex Project via Team Explorer

Team Explorer is included with Visual Studio 2010 Professional, Premium and Ultimate. Follow the following steps to connect to codeplex using Team Explorer:

  1. Go to the project page on Codeplex. Make sure you are logged in
  2. Click Source Control tab
  3. Click Visual Studio Team Explorer and keep that page open
  4. Open Visual Studio
  5. Click the Team menu. If you can see it, click View –> Team Explorer and click the connect to Team Project button
  6. On the Connect to Team Project dialog
    1. Click Servers button
    2. Click Add button on the Add/Remove Team Foundation Server dialog
    3. Fill the information in the Add Team Foundation Server dialog from Codeplex page in step 4
  7. Enter the Username as you see it in the codeplex page, my username was prefixed with snd\ and suffixed with _cp. I am not sure if all usernames are prefixed and suffixed with the same characters
  8. Click Ok and close the Add/remove TFS dialog
  9. Click the connect button on the Connect to Team Project dialog
  10. Double click the Source Control node
  11. I am mapping the root of the tree to the Codeplex folder on my C directory. You can map yours to what ever directory
  12. Click the Map button
  13. Get latest and you should be good to go 🙂