An easy to miss gotcha in Azure Service Fabric service with multiple listeners


If you’ve ever worked with Azure Service Fabric, then there is a high probability that you will have a service that creates multiple listeners, either to multiple ports to accept requests from different protocols or listeners to Azure services like service bus or IotHub.

In this case, you might have to create multiple communication listeners in your application. While doing this, there is a chance that you might miss one small thing which would eventually result in you getting the following error in the Service Fabric Explorer

As you can see in the error message, it says that a Unique name must be specified for each listener when multiple listeners are used. We can specify this name as the second parameter of ServiceInstanceListener class as shown below.

This will solve the error.

Hope this helps!

Posted in .NET, C# | Tagged , , , | Leave a comment

CSS Browser Compatibility settings in Resharper


This post will show you how we can enable/disable browser compatibility settings for CSS in Resharper. This setting is very useful which shows us clearly whether a particular css selector or other css features are compatible with specific versions of browsers or not. See the example below.

We can see here that resharper points out specifically by highlighting the selector saying it is not compatible with IE 8.0.

We can enable this setting by navigating to Resharper menu item –> Options –> Code Editing –> CSS –> Inspections

As you can see, we have the option of enabling the check for browser compatibility along with multiple browsers as well as minimum version for the browser.

For more such useful information, do refer the below site.

Hope this helps!

Posted in .NET, General | Tagged , , , , , , | Leave a comment

Fixing issue of not being able to add view to Database First Entity Framework model


If you’re using Database First entity framework (EF) model, you might require to use views. Now to add views to the EF model, the process is straight forward, just the same way we add Tables. However, there is a chance the view may not show up in the Model at all.

Solution: Your view must have a column that it can infer as a Key. Not necessarily a primary Key but any key. Entity Framework will take any non-nullable, non-binary column and mark it as key. This is because entity framework does indexing to the view internally.

Consider for example a simple console application where we’re trying to add an EF model. I’ve created a sample table Users with 4 columns:

  1. ID (Primary Key)
  2. FirstName
  3. LastName
  4. IsActive (Bit Field)

I’ve also created 3 views with the following names. I hope the names are self-explainatory

  1. 1_UsersViewWithNoKey
     SELECT [FirstName], [LastName] FROM [dbo].[1_UsersViewWithNoKey]
  2. 2_UsersViewWithBitColumnIncluded
     SELECT [FirstName], [LastName], [IsActive] FROM [dbo].[2_UsersViewWithBitColumnIncluded]
  3. 3_UsersViewWithPrimaryKeyIncluded
     SELECT [Id], [FirstName], [LastName] FROM [dbo].[3_UsersViewWithPrimaryKeyIncluded]

Note that the first view just includes FirstName and last name. As we try to include this view in the .EDMX, we see that the view doesn’t get added to EDMX and the below statement appears in the output window

The model was generated with warnings or errors.SampleDataModel.edmx Please see the Error List for more details. These issues must be fixed before running your application.

In the errors window, we see the following error message

It shows that Entity Framework is expecting the view’s select list to contain some key column. Let’s try to add the second view now.

After clicking Finish, we see that View is added. However, take a note of the highlighted column and ALSO the error message that shows in the error list.

This shows that Entity Framework inferred the key to be the IsActive Column because it is of type bit (=boolean in C#) which won’t be null. Now let’s try to add the final view which has the primary key column included.

We can see that this view also gets added to EDMX and we can also see the Key symbol beside the Id column.

So make sure that when you’re adding views to the EDMX, the view needs to have some column which Entity Framework can consider as a key.

Hope this helps!!



Posted in .NET, General | Tagged , , , , , , , , , | Leave a comment

Add file header to new class/other items automatically on creation in Visual Studio


This post shows how we can make little changes to the Class template in visual studio so that we can add a file header to a newly created class. It’s generally a good practice to add a file header to any class/file in a solution but it’s equally a pain when we have to copy-paste a file header from a different file and remember to change the name at the top. Instead you can just follow the below steps to make Visual Studio add it automatically for you with the right filename when you create a new class.

Note: I’ve done this for Visual Studio 2015. But it is pretty much the same for other versions

  1. Navigate to the below folder
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\
  2. Open the Class.cs file in notepad or any editor you find comfortable. Edit it as shown below. Basically just add the file header you wish to add to every class. But edit the filename to $safeitemrootname$.cs. This will ensure that the name of the file you’ve provided is replaced here.
  3. Make any more changes as required. Save.
  4. Go back to visual studio to check if it is actually working.
  5. Modify other templates like for Interface or any other file type if required. Note :  you might want to check the folders at the 1033 level in the above path.

Hope this helps!


The below post by Abhijit Jana shows very clearly how the same can be achieved with more controls using Macros. Do have a look.

Add document header for files automatically in Visual Studio


Posted in General, visual studio | Tagged , , , | 4 Comments

Creating WCF Service hosted in Azure Service Fabric over Https with Basic Authentication


This post will show how we can create a WCF Service in Azure Service Fabric and how to configure Basic authentication to it using simple username and password validation.

Follow the below steps in the given order:

  1. Create Service fabric service.
    Create a service fabric stateless service in Visual studio.
  2. Add a sample contract and implementation with a sample method. In this case it just returns a random guid.

  3. Create a communication listener for the Wcf Service. Read next steps which would be part of the communication listener
  4. In our example, we’re going to expose the WCF Service over HTTPS on the port 44000. As part of the communication listener, we’ll create a BasicHttpsBinding and set the security mode to Transport and CredentialType to Basic.
  5. Next create the listener object which is of type WcfCommunicationListener<ContractType>. This type is in the namespace Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime. You will have to add the NuGet package Microsoft.ServiceFabric.Services.Wcf. See sample code below.
  6. Next, set the Credentials for the service host listener. Since, we’re using custom Username authentication, we will set the UserNamePasswordValidationMode to Custom and we would also specify the custom validator class object which would override the UserNamePasswordValidator class. This is an abstract class that provides a Validate method that takes in 2 parameters, the username and password.
  7. The Custom validator class implementation is as shown below.
  8. Coming back to the listener creation part. Now we also want to add a service metadata behavior so that clients can access the service metadata. Also we have to make sure that we set the httpsGetEnabled to true so that metadata is available over https. See sample code below.
  9. Now return the above created listener. The complete Listener creation method is given below.
  10. This method is called from the CreateServiceInstanceListener method provided by Service Fabric.
  11. Run the service and check that it is healthy in the service fabric explorer. Make a note of the URL.
  12. If you click on the URL, you would see something like below.

    This is because the specified port doesn’t have any certificate bound to it. We need to bind a certificate to the specified port, in this case port 44000. In your dev machine, you can create a self-signed certificate using the New-SelfSignedCertificate powershell cmdlet. Next run the below command in command prompt in admin mode to bind the certificate to the port.

     netsh http add sslcert ipport= certhash=<cert_thumbprint> appid={someguid}

    See sample below.

  13. Now run the service again and try opening the URL. You would be able to see the service wsdl as seen below.
  14. As you can see in the wsdl above, there are references to other files (see wsdl:import tag). If you want everything in a single wsdl, we can add a endpoint behavior for that. I’ve added it in the sample application code uploaded with this post (refer link at the end of post). I thank the author of this post for explaining it clearly.
  15. Now for the client side. Create a wcf client. I’m creating a sample console application here.
  16. Add a service reference and put the URL you copied above. After clicking on Go, it shows a popup that certificate is not trusted, which is ok in our case as we’ve created it locally. Then it prompts for username and password. Enter the username and password that your WCF service expects. Now service reference is added.

  17. In the code, make sure that you’ve provided the credentials (username and password) as shown in below sample.
  18. As mentioned above, make sure that small piece of code marked with comment doesn’t go into production. That is meant for local dev purposes only.
  19. When we run the client, if we’re passing correct username and password, we will get a guid response as shown below.

I’ve uploaded the complete sample application here. Alternatively, you can clone the repo using this git url directly.

Hope this helps!

Posted in .NET | Tagged , , , , , , , , | Leave a comment

Entity Framework not saving data to database on context.SaveAsync() while using Autofac


Recently I was using Entity Framework with a DB first approach in an application and using the Repository pattern with Unit of Work to have easy management of DB operations.

So my repository constructor accepted an IDataContext object (IDataContext is a custom interface I had written which encapsulates the DbContext generated after adding Entity Data model. Refer sample code below.


public class RepositoryBase<TEntity> : IRepositoryBase<TEntity>
where TEntity : class


        public RepositoryBase(IDataContext context)




The UnitOfWork class had a constructor that accepted the ILifetimeScope object and using this object, I was resolving the context in the constructor. I also had a couple of repositories specified as properties in this class. Refer sample code below.


public class UnitOfWork : IUnitOfWork


    public UnitOfWork(ILifetimeScope scope)

        this.scope = scope.
        this.Context = this.scope.Resolve<IDataContext>();


    public Task<int> SaveAsync()


        return this.Context.SaveAsync();


   // Repositories

    public IRepositoryBase<SampleClass> SampleRepository


            if(this.sampleClassRepository == null) this.sampleClassRepository = this.scope.Resolve<IRespositoryBase<SampleClass>>();    // Observe above line later
             return this.sampleClassRepository;



The code that was using this repository was something like

using(var uow = this.scope.Resolve<IUnitOfWork>())

    uow.SaveAsync();    // This should actually save above new entity into DB but it wasn't saving anything.

The problem with this approach was that When I was working with the repository, Autofac initially sees that the repository object has not been created and so creates a new object by resolution by injecting a NEW CONTEXT object as it sees that repository constructor requires an IDataContext object.

So because of this issue, we end up with 2 different contexts, one which just got created while trying to access the repository and this also has the change (insert), and the other which got created while creation of UnitOfWork object. And what happens is insert action goes into repository context object and Save action works upon the UnitOfWork’s context object. Hence, nothing fails but nothing happens in DB as well.

To make sure that repositories are also using the same context as UnitOfWork, what we can do is to pass the unitOfWork’s context to the Resolve method of ILifetimeScope as shown below.

if(this.sampleClassRepository == null) 
    this.sampleClassRepository = 
        (new TypedParameter(typeof(IDataContext), this.Context));

This is present in the UnitOfWork class. And this.Context refers to unitOfWork’s context. This statement ensures that when repository instance is being created, instead of creating a new context, we’re going to reuse the context of unitOfWork and so any Save operations performed would thus perform the operations on the database as well.

Hope this helps!

Reference :

Posted in .NET, General | Tagged , , , , , , , | Leave a comment

Resolving Azure Data Lake Authorization Issue


While trying to access Azure Data Lake Store from my application, I encountered the below exception.

The client 'guid' with object id 'guid' does not have authorization 
to perform action 'Microsoft.Authorization/permissions/read' 
over scope '/subscriptions/guid....

Resolution :

I realized that I had not given permission to the Active directory application in the Data Explorer tab in ADLS. To give permission, open Data Explorer –> Access –> Add –> Search for your AD application, Give Read, Write, Execute permission –> Save.

This should resolve the issue.

Hope this helps!

Posted in .NET, General | Tagged , , , , , | Leave a comment