Skip to main content

#MVC #API Create and Read custom header.



Website
In EmespmdHttpClient class add
public void SetUserIdentifier(string userIdentifier)
        {
            this.DefaultRequestHeaders.Add("EMESPMD-USER", userIdentifier);
        }
From action method send _loggedInUser.XomPersonnelId as a separate param(naming userIdentifier) to each service call.

In Gateway service add below line next to try{
       _emespmdHttpClient.SetUserIdentifier(userIdentifier);

Service

Create an extension(HttpRequestExtensions) method in EMESPMD.Api.MessageHandler folder.
public static class HttpRequestExtensions
    {
        public static string RequestedUserIdentifier(this HttpRequestMessage request)
        {
            if (request.Headers.Contains("EMESPMD-USER"))
                return request.Headers.GetValues("EMESPMD-USER").First();
            return string.Empty;
           
        }
    }
Create a Base controller
public class BaseController : ApiController
    {
        protected string CurrentUserIdentifier() {
            return Request.RequestedUserIdentifier();
        }
    }
Inherit the Controller from base controller
       public class AdminController : BaseController

Call the CurrentUserIdentifier() method and pass the data as separate param to service.
      string userIdentifier = CurrentUserIdentifier();

            _log.LogMessage(_controllerName + " ; Action-" + actionName);
            CustomResponse<XomPersonnel> customResponse = new CustomResponse<XomPersonnel>();
            try
            {
                return _adminService.GetPersonnelInformation(lanId, userIdentifier);
            }
In service method call the LogMessage method which is having 2 arguments.

       _log.LogMessage(user,  _serviceName + "Method-" + actionName);

Comments

Popular posts from this blog

Use Log4Net in C# windows form Application

we are going to learn on how to use the Log4Net library for creating logs. Create a new windows form application in VS. Install Log4Net library Add to AssemblyInfo.cs  Configure in  App.config Use in code  Install Log4Net library Then install the Log4Net library from the Nuget Package library.           log4net by The Apache software foundation 2.0.8 (i installed the latest). Add to AssemblyInfo.cs  After installing this package, open up AssemblyInfo .cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.).    [assembly: log4net.Config.XmlConfigurator(Watch= true )]  Configure in  App.config Now, open the App.config file and enter required details for LogNet to work. <configSections>       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2....

#SQL: How to Drop Database in SQL Server by Closing Existing Connections

How to Drop Database in SQL Server when the users are connected to a SQL Server Database. You may need to closing existing connections in a database before restoring the database, before detaching database, get database in SINGLE_USER mode etc. If you try dropping a database when users are connected to the SQL Server Database you will receive the below mentioned error message. Error Message Drop failed for Database 'RajDB'. (Microsoft.SqlServer.Smo) Cannot drop database "" because it is currently in use. (Microsoft SQL Server, Error: 3702) USE [master] GO ALTER DATABASE RajDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO /* Query to Drop Database in SQL Server  */ DROP DATABASE RajDB GO