May 22, 2014

Windows Azure Diagnostics: Logging from RoleEntryPoint methods failed [resolved]


I was trying to enable Microsoft Azure Diagnostics in a MVC 4 web application which is to be hosted in a Windows Azure web role. I enabled diagnostics via web role properties page



I put few 'Trace.WriteLine' statements in RoleEntryPoint 'Run' method. After deploying to Azure I don't see 'WADLogTable' being created.

I tried putting the same log statements in one of the MVC controller method, then 'WADLogTable' got created and logs got written to this table, still the 'Trace.WriteLine' in RoleEntryPoint 'Run' method not get logged.

Once we enable Microsoft Azure Diagnostics, the following line will get added to web.config file:

  ....
  <system.diagnostics>
    <trace>
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
          <filter type="" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
  ...

ah, I got a clue, I know web application and RoleEntryPoint methods are running under different processes, see this post for details.

As i mentioned in the above post, I created a an application configuration file with name same as the compiled DLL (with .config extension). i.e. if the name of Web Role DLL is NorthWindWebRole.DLL, then the name of the application configuration file must be 'NorthWindWebRole.dll.config'.

Don't forget to set 'Build Action' property for this file to 'Content' and 'Copy To Output Directory' property to 'Copy Always'.

Added diagnostics listener configuration to this file as below, done!! now i can see logging from role entry methods are working:

<?xml version="1.0"?>
<configuration>
  <configSections/>
  <system.diagnostics>
    <trace>
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
          <filter type="" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

May 20, 2014

Hosting ASP .NET MVC4 and WCF Data Service together with Entity Framework 6.1.0 [Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext']


I had to work on a POC which is based on ASP .NET MVC4 and Entity Framework 6.1.0, we also want to enable WCF Data Service in the same project. I faced few issues, which took some time for me to fix, so thought of writing it here:

As usual I created a ASP.NET MVC4 project (Internet application) using VS 2012 and as expected it worked fine:


Now I installed latest entity framework using Nuget, the version got installed is EntityFramework.6.1.0



As next step I created entity data model classes based on NorthWind database.



Next created a WCF Data service and configured it to use NorthWindEntites (see below selected section in red box)



I tried running the MVC app and browsing to 'NorthWindService.svc', but got the error:


Server Error in '/' Application.

The resource cannot be found.   Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name ...


After few trial and error found that this error can be fixed by adding following line in 'RegisterRountes' function in App_start\RoutingConfig.cs

routes.IgnoreRoute("NorthWindService.svc/{*pathInfo}");

Executed again, better this time i am not getting 404 error now but got another error:

Request Error: The server encountered an error processing the request. See server logs for more details

To debug this further, I again started debugging and enabled "Common Language Runtime Exceptions" from "Exception Dialog" (Ctr + Alt + E).

Browsing to http://localhost:1234/NoerhWindService.svc cause VS 2012 to throw below exception:

Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext'

I found this blog http://blogs.msdn.com/b/odatateam/archive/2013/10/02/using-wcf-data-services-5-6-0-with-entity-framework-6.aspx, which clearly says to use Entity Framework 6.* as data provider for a Data service class, the service needs to be derived from EntityFrameworkDataService<T> intead of DataService<T>


To get 'EntityFrameworkDataService' you need to install 'WCF Data Services Entity Framework Provide' nuget package https://www.nuget.org/packages/Microsoft.OData.EntityFrameworkProvider/1.0.0-alpha2

Run the command:

Install-Package Microsoft.OData.EntityFrameworkProvider -Pre

in Nuget Package Manager Console (Tools => Nuget Package Manager => Package Manager Console)

Update your data service class to inherit from EntityFrameworkDataService.


Now tried again browsing to http://localhost/NorthWindService.svc, this time it worked and i got the metadata!!

Few thing to note, when you add WCF Data service, following versions of packages will be added:

Microsoft.Data.Edm.5.2.0
Microsoft.Data.OData.5.2.0
Microsoft.Data.Services.5.0.0
Microsoft.Data.Services.Client.5.0.0

Once you install 'WCF Data Services Entity Framework Provider' (Microsoft.OData.EntityFrameworkProvider.1.0.0-alpha2), all the above packages will be updated to new versions:

Microsoft.Data.Edm.5.6.0
Microsoft.Data.OData.5.6.0
Microsoft.Data.Services.5.6.0
Microsoft.Data.Services.Client.5.6.0

Everything is working on my local environment now. I deployed this basic application to Microsoft Azure and browsing to data service threw below error:

Could not load file or assembly 'Microsoft.Data.Services, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

See data service is still trying to use version 5.0.0.0 of Microsoft.Data.Services, but installation of 'WCF Data Services Entity Framework Provider' updated the references to Microsoft.Data.Services 5.6.0.

If you open  the .svc file [right-click it, select View Markup] then you can see it still contains following line:

<%@ ServiceHost Language="C#"
  Factory="System.Data.Services.DataServiceHostFactory,
          Microsoft.Data.Services, Version=5.0.0.0,
          Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  Service="MVC4WCFDataServiceFE5.NorthWindService" %>

Just update the version number to '5.6.0.0' as below and all started working.

<%@ ServiceHost Language="C#"
  Factory="System.Data.Services.DataServiceHostFactory,
          Microsoft.Data.Services, Version=5.6.0.0,
          Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  Service="MVC4WCFDataServiceFE5.NorthWindService" %>

May 7, 2014

VS 2012: configuring Web https://.. for ASP.NET 4.5 failed, you must manually configure this site for ASP .NET 4.5.

I got this error when i tried opening a sample download from internet, which was configured to use 'IIS Express' with a project url that has https protocol part. Fix is:

1. Close the VS project.
2. Open C:\Users\USER_NAME\Documents\IISExpress\config
3. Create a backup of 'applicationhost.config'
4. Open 'applicationhost.config', look under sites for your site name (e.g. TodoListService in my case)

            <site name="TodoListService" id="17">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="D:\projects\..\TodoListService" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:44321:localhost" />
                </bindings>
            </site>

5. Change the 'binding protocol' to 'https'
6. Start Visual Studio 2012 with Admin privileges
7. Open the project