Sunday 18 November 2012

tfs msbuild MVC3

You know the time spent, when you do change on a web project and want clients to see what you have done?  I have timed one of my releases, with build, publish. It can easily take me up to half an hour, for a bigger project. I have known for a while that you can publish it from TFS build process. So everytime you commit your change into TFS server, you can have automated build triggered and this can do deployment. The company that I work for is doing just this with a website asp.net webforms.

I do not like to copy anyones work, and found out about this process myself and wanted to save myself time when releasing when tfs build succeds.

Step one:

Open visual studio command line:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat""

Now you can run msbuild command.

Step two:
Create your publish.msbuild file.

Step three:

File content
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<PropertyGroup>
        <PublishDestination>C:\Release\Test</PublishDestination>
        <Configuration>Release</Configuration>
    </PropertyGroup>

 <Target Name="Build" >
    <Error Condition="'$(PublishDestination)'==''" Text="The PublishDestination property must be set to the intended publishing destination." />
    <MakeDir Condition="!Exists($(PublishDestination))" Directories="$(PublishDestination)" />
   
    <MSBuild Projects="WebUi\WebUi.csproj"
    Properties="Platform=AnyCPU;Configuration=Release;
    DeployOnBuild=true;    DeployTarget=PipelinePreDeployCopyAllFilesToOneFolder;_PackageTempDir=$(PublishDestination);" />
</Target>
</Project>




Tuesday 13 November 2012

MVC Routing Pages, Css


CSS Routing

If you want ignore routes for all css

RouteTable.Routes.IgnoreRoute("{resource}.css/{*pathInfo}");

Bare in mind that routing of css will not be accepted.

When you want to route css files you need to modify web.config

add into   system.web following

    <compilation>
    <buildProviders>
    <add extension=".css" type="System.Web.Compilation.PageBuildProvider"/>
    </buildProviders>
   </compilation>

and after you can add routing of CSS files

var themePath = CreateThemePath(themeName);
routes.MapPageRoute("layout.css", "layout.css", string.Format("{0}layout.css", themePath));


  private static string CreateThemePath(string themeName)
        {
            return string.Format("~/Content/themes/{0}/", themeName);
        }


After to display your css you need to help to the file with adding code inside of the file:
<%@ ContentType="text/css" %>

Note if you want to run old aspx pages within your mvc application you need to include following route. I am enclosing existing one that I am using.

routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(aspx|js|gif|png|jpg)(/.*)?" });
routes.RouteExistingFiles = true;


Wednesday 7 November 2012

MVC Tips


I have been working with MVC projects for a while and one common issue I have is find out how to do something easy. I will be updating this article as I go along and add the issues I have had and my solution to this. This is by no means the best way, but it is used as it works.

DataAnnotations

 

Formating DateTime using Html.Editor

 Displaying formated DateTime
 
I needed to display custom format on date in editor for.
You can use decoration to achieve custom formating of datetime field in your model. 
 
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime Date { get; set; }

Bare in mind that you need to use
@Html.Editor(model=>model.Date) for this field.

Routing

Custom route with aspx page


I have one aspx page which is used in webforms and my project in mvc. I want to have nice routing in place and do not want to display to the user name of the page.

original page is somethinng like: http://localhost:9999/oldwebsite/SayHelloWorld.aspx
my desired name of the page : http://localhost:9999/SayHelloWorld

To achieve this we can use global.asax with custom route in register routes, into which you need to add your custom route.

routes.MapPageRoute("SayHelloWorld", "SayHelloWorld", "/oldwebsite/SayHelloWorld.aspx");

Friday 2 November 2012

EF "The partner transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D025)"

I will take you with me how to resolve following error that I have recieved through EF

Higher message from EF
The underlying provider failed on Open.

Inner exception message:
"The partner transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D025)"

I have found multiple solutions about this problem.

Source1: msdn forum
Source 2: stackoverflow.com


I have checked running services on my local pc and on server.

I have restarted server. No change. Restarting local pc.

This error lead me to error mesage
ora-12560 tns protocol adapter error

I have run command:
lsnrctl stat

with results:
LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on 03-SEP-2012 11:41
:18

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Produ
ction
Start Date                03-SEP-2012 11:14:57
Uptime                    0 days 0 hr. 26 min. 21 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Default Service           XE
Listener Parameter File   C:\oracle\product\10.2.0\server\network\admin\listener.ora
Listener Log File         C:\oracle\product\10.2.0\server\network\log\listener.log
 

Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC_FOR_XEipc)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=TestOrangeDB001.FocusData.local)(PORT=1521)))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
  Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully


Ok and lastly check if database is online.

MVC and Sending email

I have often needed email class that works with my website.

Here I am adding small memory nudge :)
How do you setup your webconfig?
how does website use the SmtpClient ?


Here is your configuration of web.config:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="name@domain.com">
            <network host="smtp.mail.com" 
                     userName="name@domain.com" 
                     password="blog.dotnetclr.com" port="25"/>
        </smtp>
    </mailSettings>
</system.net>
 
Note: this tag is straight in configuration tag of your web.config


And here is the sample C# code that will use the above configuration settings
 
public void SendBy(string to, string subject, string body)
{
    var mailMessage = new System.Net.Mail.MailMessage();
    mailMessage.To.Add(to);
    mailMessage.Subject = subject;
    mailMessage.Body = body;

    var smtpClient = new SmtpClient();
    smtpClient.EnableSsl = true;
    smtpClient.Send(mailMessage);
}