Tuesday, 2 February 2016

Configure signalr in Mvc using Visual Studio 2012

 Asp.net signalr is used to transfer the data between server and client asynchronously and it is real time communication between server and client.Asp.net signalr maintains persistent connection between server and client. Using signalr we can create multiuser real time applications and chat applications. This is typically required for the real time monitoring applications.Signalr has Hub class to allow the client and server to call methods each other.

The easiest way to setup signalr in mvc is using nugget package manager.
Create sample mvc project name it as SignalRMVC=>Right click on project and click on Manage Nuget Packages and search with signalR now select Microsoft Asp.Net SignalR and click on install,now The SignalR will be successfully installed in your project.

Create one folder name it as Hubs and add one class to this folder and name it as Startup
Please write the following code in Startup class

public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }

The Startup class will be look like as below

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }

Now run the application you will get the following error

The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName].Startup class.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

or

Cannot create/shadow copy 'WebGrease' when that file already exists.

To resolve this you need to add one key in Appsettings in web.config

<add key="owin:AppStartup" value="SignalRMVC.Hubs.Startup" />

SignalRMVC.Hubs.Startup is namespace of your Startup class
SignalrMVC is project name and Hubs is folder name and Startup calss
Now the run the application it will run successfully.
To check whether Signalr install on your application or not ,go to browser and type “http://localhost:58206/signalr/hubs

/signalr/hubs and you will get





keywords:asp.net signalr setup,signalr setup in mvc ,signalr setup in visal studio,signalr configure in visual studio 2010,signalr configure in visual studio 2012,signalr configure in visual studio 2013,signalr configure in visual studio 2015,configure in signalr in mvc in visual studio 2013,signalr in mvc ,signalr chat application ,signalr application ,signalr 





Wednesday, 6 January 2016

Call Web Api from Mvc Controller



This tutorial will explain how to call a web api from Mvc Controller
Oftenly we will cal web api from Jquery using ajax call but here we call web api from C# using HttpClient library.we call web api’s from Windows application and console application using http client libraries.

So Httpclient libraries are very useful for calling web api from mvc application ,windows application and console application and also windows 8 application.
To enhance the  responsiveness of application we will use Asynchronous programming for that we will use async , await ,Task and  ReadAsync and GetAsync keywords.

Async and Await many methods could not return output immediately, they wait for other asynchronous programs execution. An Async method returns only Void or Task.

Task returns no value if it is void .If a Task<string> returns string value, this is generic type.

The GetAsync method is asynchronous method and it sends http get request , the await keyword is suspends execution until asynchronous method completes, it returns HttpResponseMessage that contains Http response.

If the IsSuccessStatusCode is true the response code contain Json ,the ReadAsync method is deserialize Json object to Users Model and IsSuccessStatusCode property is false it returns error code.

Create a MVC project , add one controller and  Action method to the MVC project and add one view to the corresponding action method ,write the following method.

For fake api  I took this url: http://jsonplaceholder.typicode.com/posts/1

Decorate your attribute with HttpGet Attribute

[HttpGet]
        public async Task<JsonResult> getdata()
        {
            Users user=null;
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                // New code:
                HttpResponseMessage response = await client.GetAsync("posts/1");
                if (response.IsSuccessStatusCode)
                {
                     user = await response.Content.ReadAsAsync<Users>();  
                }
            }

            return Json(user, JsonRequestBehavior.AllowGet);
           
        }
Here Users is Model Class to persist data
public class Users
    {
        public int userId { get; set; }
        public int id { get; set; }
        public string title { get; set; }
        public string body { get; set; }
    }


 In fiddler we will get the following result



  Tags: call api from c#,call web api from C# controller, call web api from Mvc,calling api from C#,
calling api from asp.net,calling api from jquery,call web api  from controller,consume api in C#,consume web api from C#