ASP.NET Web API is a microsoft's framework for creating HTTP services that can reach a broad range of clients including browsers and mobile devices. In this post we are going to discuss how we can create a simple HTTP service using ASP.NET Web API and observe them using Fiddler.
Implementing ASP.Net Web API Service
We can create ASP.NET Web API projects using ASP.NET MVC project template. As soon as we select this type of project, we can select Web API in the second dialog shown for selection.
The services can also be added later as project items as follows:
The HTTP based rest services are implemented as Api Controllers in ASP.NET Web API. It is an object that handles HTTP requests. The service must inherit from ApiController in System.Web.Http namespace in System.Web.Http assembly.
As we discussed above, ASP.NET Web API suggests implementing HTTP based REST services as controllers. We can add a controller directly to Controllers folder in ASP.NET web API project. We can also use shortcut keys to bring up Add Controllers dialog.
Since we would be providing the complete definition ourselves, let's just add an empty MVC controller. You can see that we have added definitions of Get, Post, Put and Delete methods. These methods would be used by the framework as we received the corresponding GET, POST, PUT and DELETE http requests for the service. These names are based on conventions, alternatively we can decorate these methods with HttpGet, HttpPost, HttpPut and HttpDelete attributes from System.Web.Http namespace.
Now we update the contents of the file adding our definition of StudentsController.
Inspecting / Analyzing & Generating HTTP Requests
We can use Fiddler for debugging ASP.NET web api based HTTP services. Fiddler is a Telerik utility which is used to inspect, analyze and generate HTTP requests. It can also be used to compose HTTP request messages. After analyzing a request, we can update and replay the message.
ASP.NET Web API fully supports content negotiation out of the box. We can request data in the required format by specifying the details in the GET request. Here we have picked up a HTTP GET request in Fiddler. The request has the details about the content types supported by client browser.
Requesting Content in JSON format
We can very well compose the GET request in Fiddler to request data in a different format. As we have just stated that ASP.NET Web API fully supports content negotiation. We can also request data in a different format. In the following we are requesting data in text/json format using Fiddler.
The response from the API can be viewed in Fiddler. The above request results in the following response:
HTTP POST to ASP.NET Web API
We can also generated HTTP POST requests using Fiddler.Here we want to post some data in JSON format. We need to specify the content type to text/json in order for the service to correctly handle the data.
The request is received by ApiController in the method used for handling HTTP post requests. By convention, this is the method starting with name as Post. We can also decorate the methods with specific HttpPost attribute if we don't want to follow conventions.
Download
Wednesday, November 13, 2013
ASP.NET Web API based REST Service - An Introduction
Labels:
.net framework,
asp.net,
asp.net mvc,
C#,
fiddler,
HTTP Services,
REST,
web api,
web service
Wednesday, November 6, 2013
Using Win ADK's Windows Performance Analyzer with EventSource
This is the third post in our discussion regarding the use of ETW tools with EventSource API in .net framework 4.5. In the previous posts, we have discussed how we can use PerfView (link), LogMan, PerfMon, PerfMonitor and TraceRpt with our EventSource (link). This post is about using Windows Performance Analyzer.
Windows Performance Analyzer can be used to analyze ETL [Event Trace Log] files. These ETL files can be generated from any ETW tool including PerfView, XPerf or Windows Performance Recorder (WPR). Here XPerf is a legacy ETW tool. PerfView is available as a separated single file download from Microsoft Download Center.
Here WPR and WPA are part of Windows Performance Toolkit, which can be installed with Windows ADK (Assessment & Deployment Toolkit). The toolkit can be downloaded from Microsoft's Download Center.
Just make sure that Windows Performance toolkit is installed by the installer.
After installation, you should see the toolkit in the start menu. There are two tools including Windows Performance Analyzer (WPA) and Windows Performance Recorder (WPR).
Let's use PerfView to generate ETL file for ETW events generated for a sample application. Now WPA relies on additional specific events in ETL files when a trace is stopped. The release version of PerfView doesn't generate those events in the file but there is a separate download from Vance Morrison's blog. Hopefully, this feature should be added to the release version soon.
We need to run PerfView with Admin Privileges. We have a very simple application which just writes a couple of events to ETW and exits. PerfView should be able to push those events to ETL file. But first let's first look at event's data with Activity Id (s) and RelatedActivity Id (s).
Just look at the extra events generated from Morrison's PerfView in order to support WPA.
PerfView generates the ETL file in the same folder by default. It is in compressed format. We can decompress and open the ETL file in Windows Performance Analyzer. We can drop the Generic Events to Analysis view to see the details about particular events and their order. I think this can be improved further as you still don't see activity and related activity Ids here.
Download
Windows Performance Analyzer can be used to analyze ETL [Event Trace Log] files. These ETL files can be generated from any ETW tool including PerfView, XPerf or Windows Performance Recorder (WPR). Here XPerf is a legacy ETW tool. PerfView is available as a separated single file download from Microsoft Download Center.
Here WPR and WPA are part of Windows Performance Toolkit, which can be installed with Windows ADK (Assessment & Deployment Toolkit). The toolkit can be downloaded from Microsoft's Download Center.
Just make sure that Windows Performance toolkit is installed by the installer.
After installation, you should see the toolkit in the start menu. There are two tools including Windows Performance Analyzer (WPA) and Windows Performance Recorder (WPR).
Let's use PerfView to generate ETL file for ETW events generated for a sample application. Now WPA relies on additional specific events in ETL files when a trace is stopped. The release version of PerfView doesn't generate those events in the file but there is a separate download from Vance Morrison's blog. Hopefully, this feature should be added to the release version soon.
We need to run PerfView with Admin Privileges. We have a very simple application which just writes a couple of events to ETW and exits. PerfView should be able to push those events to ETL file. But first let's first look at event's data with Activity Id (s) and RelatedActivity Id (s).
Just look at the extra events generated from Morrison's PerfView in order to support WPA.
PerfView generates the ETL file in the same folder by default. It is in compressed format. We can decompress and open the ETL file in Windows Performance Analyzer. We can drop the Generic Events to Analysis view to see the details about particular events and their order. I think this can be improved further as you still don't see activity and related activity Ids here.
Download
Tuesday, November 5, 2013
Transfer Events & Activity Based Logging using EventSource .net 4.5.1
Logs are never looked at in isolation. If we need to look at the log in order to determine the root cause of a problem then it is a particular scenario that we are interested in. With the great testing efforts we do before releasing a product, there might be still some dormant issues which might have been missed. But since they are not apparent and not very easy to reproduce, we are specially interested in the particular order of operations user performed in order to uncover the dormant issue. We need end-to-end tracing support for our .net applications.
Enterprise applications never work in isolation. We have learnt that there cannot ever be a global enterprise system which deals with everything in the Enterprise. So these applications and machines work together in order to accomplish an enterprise operation. That is why everyone has started talking about enterprise integration, I think this would continue to grow.It is possible that our applications perform great in isolation but there are issues when they work together. The only way to uncover such issues is to use the same logging using some identifiers so that we could correlate these operations to understand the scenario.
ETW has long supported the scenario based logging using Transfer Events. You can even find some reference in Park & Buch's article published in 2007 in msdn magazine. The support is provided through tagging these events with Activity Id. The technology supports a scenario based Id (Activity Id), with a related Activity Id for each event.
When .net framework 4.5 was released, the transfer events were missing from the API. EventSource API is based on Manifest based ETW providers rather than the classic provider (no need to register events at install time). This feature has been there for both of these provider. Since technology already supports it, it was just a matter of time to include the support in the API, and .net 4.5.1 does include that support now.
Improvements in EventSource API to support Transfer Events
In order to support transfer events, Microsoft has provided various little updates in EventSource API in .net 4.5.1. Let's start with the EventSource type itself. In order to identify scenario, EventSource is provided with a new property called CurrentThreadActivity. This can be used as the Activity Id for a particular scenario. This is a read-only property. In order to specify the value, there are two modifier methods. This is one of the biggest improvement in .net framework 4.5.1. This is strategic.
We use WriteEvent() method in [Event] methods in order to write events data to ETW infrastructure. There are two new WriteEvent() methods introduced by the same type to include related Activity Id for the operation. The latter of the following methods provides better performance.
Both of these Ids are available through EventWrittenEventArgs in EventListener's OnEventWritten() method. There are two new properties added to the type. The ActivityId is the same as the CurrentActivityId value for the EventSource when WriteEvent() is called; and RelatedActivityId is the value passed as one of the argument to the method.
Sample Event Source with Transfer Events
Let's create a sample C# application using .net framework 4.5.1. Here we are using Visual Studio 2013 RC. The EventSource has two event methods; one of which is TransferEventEx which uses activity Ids.
And here is an EventListener using AcitivityId and RelatedActivityId from EventWrittenEventArgs.
Now we can use the above EventListener to enable the events written using MyEventSource. First we need to set the ActivityId for the scenario, then we need to specify the operation based Id; used as RelatedActivityId.
Transfer Events for Inter-application Enterprise Scenarios
As stated in the beginning, the real benefit of transfer events can be observed tracing end-to-end scenarios performed across application boundaries. The following is an example of a scenario identified by an Id = X (Activity Id). There are three related activities in the scenario tagged with related activity Id (s) identified with A, B and C. These applications can use ETW for their event logs. For the case of EventSource (manifest based provider), these events can be picked up by a central Semantic Logging Service. The data for these events can be pushed to an event store or any other sink of interest.
Download
Enterprise applications never work in isolation. We have learnt that there cannot ever be a global enterprise system which deals with everything in the Enterprise. So these applications and machines work together in order to accomplish an enterprise operation. That is why everyone has started talking about enterprise integration, I think this would continue to grow.It is possible that our applications perform great in isolation but there are issues when they work together. The only way to uncover such issues is to use the same logging using some identifiers so that we could correlate these operations to understand the scenario.
ETW has long supported the scenario based logging using Transfer Events. You can even find some reference in Park & Buch's article published in 2007 in msdn magazine. The support is provided through tagging these events with Activity Id. The technology supports a scenario based Id (Activity Id), with a related Activity Id for each event.
When .net framework 4.5 was released, the transfer events were missing from the API. EventSource API is based on Manifest based ETW providers rather than the classic provider (no need to register events at install time). This feature has been there for both of these provider. Since technology already supports it, it was just a matter of time to include the support in the API, and .net 4.5.1 does include that support now.
Improvements in EventSource API to support Transfer Events
In order to support transfer events, Microsoft has provided various little updates in EventSource API in .net 4.5.1. Let's start with the EventSource type itself. In order to identify scenario, EventSource is provided with a new property called CurrentThreadActivity. This can be used as the Activity Id for a particular scenario. This is a read-only property. In order to specify the value, there are two modifier methods. This is one of the biggest improvement in .net framework 4.5.1. This is strategic.
We use WriteEvent() method in [Event] methods in order to write events data to ETW infrastructure. There are two new WriteEvent() methods introduced by the same type to include related Activity Id for the operation. The latter of the following methods provides better performance.
Both of these Ids are available through EventWrittenEventArgs in EventListener's OnEventWritten() method. There are two new properties added to the type. The ActivityId is the same as the CurrentActivityId value for the EventSource when WriteEvent() is called; and RelatedActivityId is the value passed as one of the argument to the method.
Sample Event Source with Transfer Events
Let's create a sample C# application using .net framework 4.5.1. Here we are using Visual Studio 2013 RC. The EventSource has two event methods; one of which is TransferEventEx which uses activity Ids.
And here is an EventListener using AcitivityId and RelatedActivityId from EventWrittenEventArgs.
Now we can use the above EventListener to enable the events written using MyEventSource. First we need to set the ActivityId for the scenario, then we need to specify the operation based Id; used as RelatedActivityId.
Transfer Events for Inter-application Enterprise Scenarios
As stated in the beginning, the real benefit of transfer events can be observed tracing end-to-end scenarios performed across application boundaries. The following is an example of a scenario identified by an Id = X (Activity Id). There are three related activities in the scenario tagged with related activity Id (s) identified with A, B and C. These applications can use ETW for their event logs. For the case of EventSource (manifest based provider), these events can be picked up by a central Semantic Logging Service. The data for these events can be pushed to an event store or any other sink of interest.
Download
Monday, November 4, 2013
Visual Studio 2013 RC Synchronized Settings & Options Enhancements
Visual Studio 2012 introduced various improvements in IDE for improving developer's productivity. One such improvement is Quick Launch Text Box. It allows launching of operations from various places in the IDE including Options dialog, Menu and Nuget. It also allows opening of documents from most recently used and open documents list.
Visual Studio 2013 has added various improvements in IDE. One such improvement is the synchronized settings between various IDEs. It is common for developers to use Visual Studio IDE from various different machines. It also includes the machines @home and @work. Now Visual Studio keeps these settings in the cloud. We can always disable the synchronization altogether or selectively control various options. As you can see below, there are various Environment options. They are from the sub-tabs in Environment page in Options dialog; also includes Text editor options.
Yes we can search in Options list [ CTRL + E ]. This is the similar option as in Quick Launch. This is great productivity enhances as we don't have to dig deeper for the extraordinary feature that we forgot is burried somewhere that we don't remember.
This is bi-directional control i.e. if an option is disabled then neither the settings would be downloaded nor uploaded for the specified option. We just need to login using the same account from all these machines and Visual Studio would make sure that all these settings are synchronized between the IDEs.
Although this feature is available on Express edition of Visual Studio as well but the tool doesn't support the synchronization between Express and non-express IDEs. It supports synchronization between all express machines. On the other hand the settings can be synchronized between professional+ editions including Visual Studio Professional, Premium and Ultimate. It also includes the synchronization of startup options; Here is the comparison of the page between Visual Studio 2012 and 2013; the startup page is not synchronized.
Other Updates in Options Dialog
There are also other updates in the options dialog. One such update is the settings for Preview tab; which was first introduced in Visual Studio 2012. This now includes supports previewing the items in Navigate To dialog [ CTRL + , ]; which was first introduced in Visual Studio 2010.
There is also a new option Performance & Diagnostics under Performance Tools. Let's discuss about it sometime later.
Visual Studio 2013 has added various improvements in IDE. One such improvement is the synchronized settings between various IDEs. It is common for developers to use Visual Studio IDE from various different machines. It also includes the machines @home and @work. Now Visual Studio keeps these settings in the cloud. We can always disable the synchronization altogether or selectively control various options. As you can see below, there are various Environment options. They are from the sub-tabs in Environment page in Options dialog; also includes Text editor options.
Yes we can search in Options list [ CTRL + E ]. This is the similar option as in Quick Launch. This is great productivity enhances as we don't have to dig deeper for the extraordinary feature that we forgot is burried somewhere that we don't remember.
This is bi-directional control i.e. if an option is disabled then neither the settings would be downloaded nor uploaded for the specified option. We just need to login using the same account from all these machines and Visual Studio would make sure that all these settings are synchronized between the IDEs.
Although this feature is available on Express edition of Visual Studio as well but the tool doesn't support the synchronization between Express and non-express IDEs. It supports synchronization between all express machines. On the other hand the settings can be synchronized between professional+ editions including Visual Studio Professional, Premium and Ultimate. It also includes the synchronization of startup options; Here is the comparison of the page between Visual Studio 2012 and 2013; the startup page is not synchronized.
Other Updates in Options Dialog
There are also other updates in the options dialog. One such update is the settings for Preview tab; which was first introduced in Visual Studio 2012. This now includes supports previewing the items in Navigate To dialog [ CTRL + , ]; which was first introduced in Visual Studio 2010.
There is also a new option Performance & Diagnostics under Performance Tools. Let's discuss about it sometime later.
Labels:
Options,
Productivity,
Visual Studio,
Visual Studio 2013
Friday, November 1, 2013
October 2013, What a month this has been...
October is about to pass. But what an amazing months it has been...
The greatest skill a developer has to have is to adjust within a group of people. Believe me, this is even difficult than developing a technical skill. Interpersonal communication is a craft and one needs to master it in order to better survive for selling the ideas. Since we are in the business of consulting for software services, we need to move between organizations every few months and years. Not all the people are welcoming, they have different psychologies and motivations. These events prove to be a practice. You need to go to the venue, introduce yourself, present in the presence of total strangers - you have no idea about their backgrounds.
Few Advices & Lessons Learned
Humor is an interpersonal lubricant. Since we are interacting with the audience for the first time, we need to break the barrier of being strange. Humor is the universal lubrication of friction between people. You are never strange for the people who smile with you. Although this is the most effective technique, but this can go either way. You need to pull off the joke really well. Like an actor, the punch line has to be effective. Practice it, tell it a few times to your friends no matter how annoying they become.
One must go through the session material before the session. I have realized that we try to be very detail oriented when are creating slides for our presentation. These conferences and code camps are not class room sessions. People are interested in links and ideas they can take home. This give them direction for their technical development. We need to give them as much as possible in the limited time we have. But the most important thing is to motivate them enough about the stuff that they study further after the event. We need to be fresh for the event, so take a nap before the event.
We can also record the session if possible. This is one of the take away item for yourself. A recorded session can be useful for preparing yourself if you are speaking about the same stuff later on. This is specially useful if you have been away focusing on some other technological stuff for a few months. You can also publish them for general audience if you think this might help them learning about the topic.
We must time box our session. A session has a list of agenda items. We need to make sure that we time box the agenda so that we are able to cover each item and still be able to finish our session in time. If there is an item which is taking more time, tell about some pointers the audience might take away with them. We must also be ourselves, confident and to the point. Call your family or friend for a little chit chat in the morning.
Session Slides
I have presented my thoughts and ideas over two areas from a huge list of Microsoft technology stack. They are as follows:
Session Videos
Here are the session videos from the events in Silicon Valley, Boston and Southwest Florida Code camps.
You can also download the code from these sessions. I even uploaded that to my sky drive.
The greatest skill a developer has to have is to adjust within a group of people. Believe me, this is even difficult than developing a technical skill. Interpersonal communication is a craft and one needs to master it in order to better survive for selling the ideas. Since we are in the business of consulting for software services, we need to move between organizations every few months and years. Not all the people are welcoming, they have different psychologies and motivations. These events prove to be a practice. You need to go to the venue, introduce yourself, present in the presence of total strangers - you have no idea about their backgrounds.
Few Advices & Lessons Learned
Humor is an interpersonal lubricant. Since we are interacting with the audience for the first time, we need to break the barrier of being strange. Humor is the universal lubrication of friction between people. You are never strange for the people who smile with you. Although this is the most effective technique, but this can go either way. You need to pull off the joke really well. Like an actor, the punch line has to be effective. Practice it, tell it a few times to your friends no matter how annoying they become.
One must go through the session material before the session. I have realized that we try to be very detail oriented when are creating slides for our presentation. These conferences and code camps are not class room sessions. People are interested in links and ideas they can take home. This give them direction for their technical development. We need to give them as much as possible in the limited time we have. But the most important thing is to motivate them enough about the stuff that they study further after the event. We need to be fresh for the event, so take a nap before the event.
We can also record the session if possible. This is one of the take away item for yourself. A recorded session can be useful for preparing yourself if you are speaking about the same stuff later on. This is specially useful if you have been away focusing on some other technological stuff for a few months. You can also publish them for general audience if you think this might help them learning about the topic.
We must time box our session. A session has a list of agenda items. We need to make sure that we time box the agenda so that we are able to cover each item and still be able to finish our session in time. If there is an item which is taking more time, tell about some pointers the audience might take away with them. We must also be ourselves, confident and to the point. Call your family or friend for a little chit chat in the morning.
Session Slides
I have presented my thoughts and ideas over two areas from a huge list of Microsoft technology stack. They are as follows:
- Cross Platform Development using Portable Class Libraries
- Event Source API, ETW and Semantic Logging Application Block
Session Videos
Here are the session videos from the events in Silicon Valley, Boston and Southwest Florida Code camps.
semanticLogs from Muhammad Siddiqi on Vimeo.
Portable Class Libraries - Boston Codecamp 20 from Muhammad Siddiqi on Vimeo.
slab from Muhammad Siddiqi on Vimeo.
Cross Platform Development with Portable Class Libraries from Muhammad Siddiqi on Vimeo.
Download CodeYou can also download the code from these sessions. I even uploaded that to my sky drive.
Subscribe to:
Posts (Atom)