DCSIMG
September 2010 - Posts - Ido Flatow's Blog Veni Vidi Scripsi

Ido Flatow's Blog

Veni Vidi Scripsi

News

Have you heard me speak?
Powered
<style type='text/css' media='screen' id='sm_css'> #smix {overflow: visible;height: auto;border-radius: 10px;max-width: 250px;background-color: #323232;text-align: left;font-size: 12px;line-height: 16px;font-family:'Lucida Sans Unicode','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;} #smix a {color: #0056CC;text-decoration: none;} #smix .sm_head {color: #fff; line-height: 1em;font-size: 1.4em;padding: 10px;color: #fff;} #smix .sm_lanyard_wrapper {background-color: #fff;;clear: both;width: 97%;margin: 0 auto;margin-bottom: 0px;} #smix .sm_lanyard_content {padding: 7px;}#smix button.sm_rec, #smix a.sm_rec, #smix input[type=submit].sm_rec { padding: 6px 10px; -webkit-border-radius: 2px 2px;-moz-border-radius: 2px; border-radius: 2px; border: solid 1px rgb(153, 153, 153); background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(255, 255, 255)), to(rgb(221, 221, 221))); color: #333; text-decoration: none; cursor: pointer; display: inline-block; text-align: center; text-shadow: 0px 1px 1px rgba(255,255,255,1); line-height: 1; }#smix .sm_rec:hover { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(248, 248, 248)), to(rgb(221, 221, 221))); }#smix .sm_rec:active { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(204, 204, 204)), to(rgb(221, 221, 221))); }#smix .sm_rec.medium { padding: 3px 7px; font-size: 13px; }#smix .sm_rec span.icon.thumbs_up {background-position: 0px 36px;vertical-align: text-top;display: inline-block;margin-right: 4px;height: 18px;width: 16px;background-image: url(http://speakermix.com/images/new/thumbsold.png);}#smix .sm_rec:hover span.icon.thumbs_up {background-position: 0px 18px;} #smix .sm_events {padding:2px 0px 4px 0px;} #smix .sm_section {font-size: 10px; border-bottom: 1px solid silver; margin-bottom: 6px;} #smix .sm_subline {font-size:120%;margin-top:4px;font-weight:bold} #smix .powered {text-align: right} #smix .powered img {margin: 7px} </style>
Sela Technology Center

Advertisement

September 2010 - Posts

System.Transactions, not only for databases

In every WCF course I gave in the past couple of years, whenever I began teaching how to use transactions in WCF, the same question comes up again – can we use transactions on anything else other than a database when using the TransactionScope class?

The basic answer I give is as follows: every resource manager that implements the necessary interface and enlists itself to the transaction scope is supported, so in short – you can use transactions on anything, IF YOU IMPLEMENT IT.

To allow a resource manager to participate in a transaction scope, it needs to implement the System.Transactions.IEnlistmentNotification interface. You can find more information about this interface and some code samples here.

Fortunately, there are many people that already tried (and succeeded) in writing libraries for transactional resource managers, such as:

1. File system.Most students usually ask me about file system transactions, so this is a good time as any to answer this question thoroughly. Since Windows Vista, the NTFS file system supports transactions, referred to as TxF, but Microsoft hasn’t formally released a managed .NET library that wraps the Win32 API for those functions, only some samples in this MSDN sample page. You can also see an implementation of this sample in the following screencast in channel9. If you’re looking for open-source projects, try the AlphaFS project from CodePlex.

If your file system is older and doesn’t support TxF, but you still want to be able to use transactions on files, you can try the .NET Transactional File Manager project from CodePlex.

By the way, if you do use transactions on the file system, better leave your log file out of the transaction scope, otherwise you might not understand why your logs don’t have any information about failed db transactions that were rolled back.

2. Memory. I vaguely remember someone once told me that in .NET 4 we’ll have transactional memory capabilities (also referred to as Software Transactional Memory, or STM). Apparently, STM.NET was released in the beta 1 version of .NET 4, but was removed from the RTM version. If you’re interested in this concept, there are some white papers on this subject, and some scattered implementations, for example the NSTM (.NET Software Transactional Memory) project from CodePlex.

So as you can see, transactions are not limited to only databases, but can also span to other resources as well, and can also share transactions among resources (what is referred to as a Distributed transaction). If you are planning to use distributed transactions in your services, I advise that you to read the following blog post from Arnon Rotem-Gal-Oz regarding possible problems with this pattern.

ASP.NET Compatible WCF services concurrency problem

In case you didn’t know by now, using sessions in ASP.NET will cause multiple client calls to be synchronized. This behavior, as explained in this blog post, can cause delays and timeouts in page processing, and unfortunately is not always easy to solve.

The easiest solution is to set each page’s EnableSessionState to either Enabled or ReadOnly. Pages which use session for read operations only and marked appropriately can run concurrently. Note that it is enough that one page is marked for read/write to lock the session for all concurrent pages.

BTW, changing the session state from InProc to SessionServer or SqlServer won’t solve this problem, because this behavior does not depend on the session provider.

This problem does not only apply to ASP.NET, but also to WCF services that are defined to have ASP.NET Compatibility. If you turned on the compatibility mode for your WCF service, then multiple concurrent calls from the same client, using the same channel (for example, calls from a silverlight application that are async by nature) will be synchronized and become sequential. This behavior of course requires that you use the Multiple concurrency mode instead of the default Single mode for your service, otherwise you’ll end up synchronizing requests from the client even if you don’t enable the Asp.Net compatibility mode.

What can you do?

1. Stop using the compatibility mode. Sometimes people use this mode because they want to use the session object to store session info, but there are other solution for this situation, such as using AppFabric’s Cache (AKA Velocity). If you want the compatibility mode, but don’t need the session state, just remove the Session_Start method from the Global.asax, this will disable the ability to work with sessions which will prevent the locks from happening.

2. If your WCF service only needs to read session info, you can set the session state behavior to read-only, by placing the following code in the Global.asax:

void Application_BeginRequest(object sender, EventArgs e)
{
  if (Request.Headers["SOAPAction"] != null)
    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.ReadOnly);  
}

As described before, if all calls are read-only calls, then there won’t be any lock on the session state and concurrent calls will run ok.

3. Another solution is to use a web farm with no sticky-session - this way every request might reach a different server (potentially), causing each server to lock only its own session state. This technique is good for several concurrent requests from a client, but if your client performs tens or hundreds of calls concurrently, this solution might not work too good.
The problem with this solution is that if you want to share the session state, you’ll need to change the state from InProc to either SqlServer or StateServer (or the customized Appfabric cache). Because session state is unique to an appdomain, you’ll have to do “tweak” you application to allow the state manager to treat both app pools the same:

a. Use the same machine key for both servers / websites.

b.If you manage your state using SqlServer, you might need to change the stored procedure that identifies the application using this technique, or this technique. If you manage your state using StateService, you’ll can use this technique (tested successfully on IIS 7).

To conclude, using the AspNet compatibility mode and keeping sessions can harm your WCF service’s ability to handle concurrent calls from the same client. There are several solution, and you’ll have to pick the right one for you.

Showing XML in IE9, the “old way”

This week I made a rookie mistake – I’ve installed IE9 a day before I had to give a presentation about WCF where I wanted to show the audience how to look at a WSDL file that is exposed by the service. Needless to say I was a little embarrassed when IE9 didn’t show the content of the WSDL file.

Apparently, one of the bugs/issues that the beta currently has is with showing XML file the old fashion way. IE9 has a new feature that allows you to format an XML document using CSS, which is cool If you only want to display the element values of an XML, but no so cool if you want to see the entire XML document, including element and attribute names.

Because the default behavior of IE9 is showing only the values of elements, opening a WSDL document will only show the address of the endpoints, instead of showing the entire WSDL file.

But … where there’s a bug, there is sometimes a workaround:

1. Open the link to the WSDL file – you will probably see only one line that shows the address of the service.

2. From the address bar, drag the IE icon to any opened folder or to your desktop – A web shortcut will be created.

3. Double-click the shortcut to open it in a new window – this time IE will show the entire content of the XML file, using the old fashion way.

This workaround work about 98 percent of the time. Also, you might need to close opened windows that are already showing this address with the incorrect format.

My thanks to Shlomo for helping me understand the origin of this behavior.

Your vote is important

The Entity Framework team has opened a wish list website to get input about what bugs people in Entity Framework 4.

If you’ve ever used EF and complained about it (which happens to me on a daily basis), this is the time to speak out.

Read more on the original blog post