Blog - Microsoft .NET, ASP.NET, AJAX and more

Behavior Driven Development (BDD) with Cucumber and ASP.NET MVC

by Damien White 6/10/2010 6:18:40 PM

cucumber_logo So as I’m sure you have guessed by my last bunch of posts, I’ve been hooked on Ruby and Rails development.  The more I use it, the more I love it.  I felt I was loving it a bit too much, so decided to divert my attention back to ASP.NET MVC for a while.  While developing with Rails, I was practicing BDD with Cucumber.   I felt that Cucumber scenarios (written Gherkin) were far more useful for the majority of my testing on a web application.  Sure unit tests are worthwhile for bits of code, but I’ve found that they don’t always make sense when testing a web application (like Steve Sanderson has mentioned).  Some may argue that these tests are brittle, but honestly, I feel it defines my application better and it definitely tests the actual interactions better.  I’m fully on board for unit tests for complex processes, but the majority of web work just isn’t that complex.

I’m not going to go fully into what BDD is here, but my goal was to use a similar process when developing ASP.NET MVC applications as I did in Rails.  With Rails, it’s simple, Rails, Cucumber, Pickle, ActiveRecord, Factory Girl, Webrat, etc. work perfectly together.   In the .NET world, there are options, but nothing that really fit together as nicely.  I started with just wanting to test my basic application (menus, breadcrumbs, etc) and didn’t venture into model work yet, so I figured it would be as simple and efficient as it is in Rails.  Not so much.  I tweeted about this exact thing a while ago.  More...

Shout it kick it on DotNetKicks.com Bookmark and Share

Get and Set Properties using .NET Reflection

by Cedric Boudoya 9/29/2008 5:13:03 PM

Introduction

I would like to set the text value on a textbox control. It sounds easy enough. First, I would like to create a textbox web control then I would like to access one of its properties "Text" and set the text to "Hello World". Well what do you need to do to write your code in a generic way so you are can abstraction of the fact that the web control is actually a text box, it could be any web control.

We will visit different ways to be able to this.
More...

Shout it kick it on DotNetKicks.com Bookmark and Share
Tags: ,
Categories: .NET | C# | Reflection
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Metadata Exchange Using SvcUtil: A Cautionary Tale

by Dave Marini 3/21/2008 7:42:00 AM


One of the projects that I've been working on the past week is a conversion project that involves a web site that has a tightly coupled business logic layer. The goal of the project is to re-engineer the site so that the business logic and corresponding data access tiers are decoupled and distributed as services. For us the obvious choice for this is using the new Windows Communication Foundation framework to accomplish the task. After some heavy reading and a lot of trial and error, we're very close to having this done and I'm proud of the outcome given the short span of the project (~ 5 weeks). We probably could have finished in 4 weeks if it weren't for a seemingly innocuous problem with proxy generation that ended up costing me 3 day's time to solve.  If you're using a tcp based Metadata Exchange binding and having trouble generating a service proxy with SvcUtil, read on!

The Error Rears It's Ugly Head

This is the error that I was getting when trying to have SvcUtil generate my proxy class for a particular service of mine:

WS-Metadata Exchange Error   URI: net.tcp://localhost:8888/MyServices/MyBigFatGreekService

Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8888/MyServices/MyBigFatGreekService'.

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:04:59.9843748'.

An existing connection was forcibly closed by the remote host

Oh how silly of me! As you can see, SvcUtil was built to tell you exactly what's going on, NOT!! this error is terribly vague because it leads you to believe that you have a network issue going on, which hey, you might. But in my case, I had 15 services hosted on the SAME address and I was able to generate 14/15 with no problem. Now, let's take a look at what kind of problems cause this error to appear:

Common Beginner Mistakes

I'm going to take a small digression here to talk about some common mistakes that take 30 seconds to fix that could cause this error to occur.  Most people who get errors from SvcUtil are missing something in the configuration of their services that is causing the problem.  The biggest two culprits I've seen:

1.) Make sure you enable Metadata Exchange. you need to do this explicitly for each service and it's commonly missed. This is done with a Service Behavior Configuration Section, like below. The key piece here is the serviceMetadata element:

<behaviors>
     <serviceBehaviors>
          <behavior name="MetaDataBehavior">
            <serviceMetadata />
          </behavior>
     </serviceBehaviors>
</behaviors>

If you're using a Tcp based binding, just making sure it's there as above enables the exchange. For using an Http based binding, specify either HttpGetEnabled or HttpsGetEnabled, as in the below example:

<serviceMetadata httpGetEnabled="true" />

2.) Make sure that the security settings on your binding match what you want to achieve.  If the discovery of your metadata is to be public, explicitly set your security to "None" so that you are sure not to encounter authentication issues. If not, make sure you configure the security correctly. In my example, we're not securing the metadata endpoint.

<services>
   <service name="Service.MyService" behaviorConfiguration="MyServiceBehavior">
      <host>
        <baseAddresses>
           <add baseAddress = "net.tcp://localhost:8888/MyService" />
        </baseAddresses>
      </host>
      <endpoint address="" binding="netTcpBinding" contract="Service.IMyService" 
           bindingConfiguration="myNetTcp" />
      <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange" 
           bindingConfiguration="mexTcp" />
   </service>
</services>
<bindings>
  <netTcpBinding>
     <binding name="myNetTcp" portSharingEnabled="true">
     </binding>
     <binding name="mexTcp" portSharingEnabled="true">
         <security mode="None" />
     </binding>
     </netTcpBinding>
</bindings>

In this case I'm using port sharing because we want all of our services to share the same Tcp port regardless of how many we have in the future. If you have port sharing enabled for one service, do yourself a favor and keep it consistent. For more detailed information about port sharing, read this blog post by Nicholas Allen to get a true understanding of why you need this.

Sleuthing About

None of my other services were having proxy generation problems, so I knew that I had the services set up correctly. I even double checked to make sure I didn't have any syntax errors on that one service that could cause me problems. After this, I began seeing some strange things. Turns out, switching to an Http based binding like mexHttpBinding fixed the problem. But I didn't want http, I wanted Tcp, so I pressed on. What could be the difference between the two bindings that could cause such a problem?

It turns out that the service I had trouble with was the largest of all the services I was trying to generate (~ 30 methods). I found that by commenting out a few of the methods in the service contract, the Tcp Metadata exchange WORKED! It also didn't matter which methods were commented, which ruled out problems like an incorrectly serialized type or a duplicate operation name.  Now, in Juval Lowy's book on WCF, he states that service contracts should have no more than 5 - 7 methods if they are properly factored and this makes sense,  but I didn't have time to factor this class straight away due to time constraints.  For now, the class has to stay big. There was no documentation stating that service contracts had a maximum size restriction, so again, WHY THIS ERROR?

I thought that maybe I needed to up the default throttling controls on the metadata exchange binding for the services, so I increased the maxReceivedMessageSize and all of the limits in the readerQuotas element, but received the same error. It should be noted here that the mexTcpBinding is a very specific type of netTcpBinding that doesn't allow you to tweak these settings. So, just use netTcpBinding, there's no rule against it.

Send In The Marines!

Hey everyone, SvcUtil has a limit on how much metadata it can receive! Did you know that?  If you did, congratulations, because this is not well documented at all! Especially disconcerting is the apparent inconsistency with which this limit is imposed, which was the reason that my mexHttpBinding worked without a hitch. How do we tell SvcUtil to eat more fiber?  It turns out I was on the right track with increasing the throttling controls on the binding, but not in the right place. It turns out that I needed to create an app.config for SvcUtil itself, place it in the directory with SvcUtil.exe and THEN run it. Here's the config that finally made it work:

<configuration>
  <system.serviceModel>
     <client>
        <endpoint name="net.tcp" binding="netTcpBinding" bindingConfiguration="myTcp"
          contract="IMetadataExchange" />
     </client>
     <bindings>
        <netTcpBinding>
            <binding name="myTcp" maxBufferPoolSize="60000000" maxBufferSize="60000000" 
                 maxReceivedMessageSize="60000000" >
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                 maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                 maxNameTableCharCount="2147483647" />
            </binding>
        </netTcpBinding>
     </bindings>
  </system.serviceModel>
</configuration>

So there it is. Make sure that the binding configuration that you use in the svcutil app.config matches the one you're using in the configuration file for your service host. If you don't, you will be getting a different error when you run SvcUtil, explaining that you have mismatched service settings and therefore the action is invalid.

Roll Credits

I can't say I figured this problem out on my own. In fact, when I was so frustrated that I ripped all my hair out, I finally posted the MSDN Forums to see if ANYONE had the same problem as me. Thanks to the moderators there, I was pointed in the right direction which solved my problem. You can see my post here.

In my next post, I'll be discussing why Visual Studio's Service Reference Proxy Generation Tool isn't adequate for those who are producing and consuming their own services and want to share multiple collection data types between the service and client, and how to use SvcUtil to overcome that inadequacy.

Shout it kick it on DotNetKicks.com Bookmark and Share

Using Exchange Web Services 2007: The Basics

by Dave Marini 3/20/2008 8:02:00 AM
Technorati Tags: ,,

Exchange Web Services (EWS) for Microsoft Exchange 2007 provides a quick an easy way to interface with Exchange data from anywhere without having to interface with ugly COM interop objects if you're living in a managed world like C#.  Today I was curious at how to get calendar appointments from Exchange into a readable format that I can use in another application, perhaps one that synchronizes appointments from exchange with those from other calendaring applications like Google's calendaring feature.  Also, I found the documentation about EWS hard to find and not very helpful, so this post will serve as a single source of good documentation for those who are getting started with EWS.  All of the examples I give will assume you have a basic familiarity with the available operations that the web service offers you.  Here are some great links to get you up to speed:

MSDN Getting Started With EWS: A 3 part intro to Exchange 2007 Web Services.
EWS Operations: A list of the operations that can be performed on the EWS.
FindItem Example in C#: An example of querying calendar items from EWS.
Glen Scales' Blog: This guy knows Exchange inside and out.

The Exchange Web Services module works off the concept of creating a soap message to send to Exchange that will tell the server what you're looking for and then give you back a soap response. The only thing I find to be problematic in the EWS architecture is that the type objects that were used to comprise the API that C# developers will use to create these requests is almost a literal translation of the corresponding XML elements and attributes, which has a tendency of making the code bloated and a bit unintuitive. Regardless, anything is better than using the old ways of communicating with Exchange, so let's continue.

The first step in communicating with Exchange is to obtain a proxy. This is an instance of the ExchangeServicesBinding class. With Visual Studio 2005, you can generate this proxy object by adding a web reference pointed at http://<YourExchangeServer>/EWS/Services.wsdl.  As a user of Visual Studio 2008, I thought that adding a service reference would accomplish the same thing, but it turned out I was wrong. If you attempt to use a service reference to generate these types, you won't get the proxy object back to instantiate.  So, the way to do this is as follows. Right click on your project and choose "Add Service Reference". Then, when the Add Service Reference Dialog comes up, choose "Advanced". This will present you with the Service Configuration Dialog screen. From here, there will be an option on the bottom left to "Add Web Reference" and this will bring you to the screen that will ensure that you generate the appropriate proxy objects from the WSDL. See Figure 1 for some screenshots of this.

Figure 1. Adding a web reference to the exchange services.
ewsConnect

In the case you don't want to go through using a web reference, you can use the wsdl.exe tool to generate the classes that you can then reference in your project. For more information about doing this, go here.  Once you have the proxy, you will want to connect to it in order to post messages to it. The way to do this in C# is quite straightforward:

ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Url = "http://<YourExchangeServer>/EWS/Exchange.asmx";
esb.Credentials = new NetworkCredential("username", "password", "domain"); 

Once authenticated, you will be connected to Exchange as the user in the credentials supplied.  You can also use CredentialCache.DefaultCredentials if you are making a WinForms application and want to use the credentials of the logged in user when connecting to the exchange server.  One thing to keep in mind when querying or creating items in exchange from this point is that all of these operations, with the exception of the free/busy time query, work off a single mailbox at a time, and this mailbox is initially that of the connected user.  The solution if you need to see items from other users is to impersonate the target user(s) and perform the queries.  There are 2 ways to accomplish this, one uses delegates and the other explicit impersonation.

Most operations in the Exchange Services proxy take objects that require you to specify which folders you will be working on.  For example, if I wanted to look for messages that contain the subject "happy holidays", I would probably want to look in the inbox, but also in the deleted items folder since I could have deleted the message I'm looking for.  There is a property on the folder type object called Mailbox, and this allows you to specify another person's mailbox when specifying the folder. So, assume I am connected to the Exchange Server as John Smith, but I need to find all appointments in Anne Dugger's calendar folder. Here would be the code to use delegate access to accomplish this:

FindItemType fit = new FindItemType();
fit.ItemShape = new ItemResponseShapeType { BaseShape = DefaultShapeNamesType.Default };
fit.ParentFolderIds = new DistinguishedFolderIdType[]
{ 
    new DistinguishedFolderIdType
    { 
        Mailbox = new EmailAddressType{ EmailAddress="adugger@contoso.com"}, 
        Id = DistinguishedFolderIdNameType.calendar
    }
};
fit.Traversal = ItemQueryTraversalType.Shallow;

FindItemResponseType firt = esb.FindItem(fit);

In the above example, the FindItemType is the class that we use to tell exchange to find items in a particular user's folder. Here we're specifying that we want to look in the Calendar folder of the user that has an email address of adugger@contoso.com. In order to use delegate impersonation to query Exchange, Anne Dugger would have to grant the connected user access to her calendar folder. This can be done in Outlook by going to tools->options and clicking on the Delegates tab. This tab is only visible if outlook is connected to an exchange server.  This method is useful if the functionality you want to incorporate is opt-in as users can grant a particular system account delegate access to their mailbox so that automated tasks can be performed.

The second way to accomplish querying another user's mailbox folders is explicit impersonation. This method is useful for scenarios where an Exchange Administrator wants to force one or more mailboxes to be maintainable by a system account.  The setup for this scenario is a bit more complex and is covered in detail here. Once configured, we can programmatically invoke impersonation in C# through the following means. Using the same example as above, if John Smith's account had been granted impersonation rights on Anne Dugger's mailboxes, and I wanted to find all appointments in Anne Dugger's Calendar folder, here is the C# code we would use:

ExchangeImpersonationType imp = new ExchangeImpersonationType();
imp.ConnectingSID = new ConnectingSIDType { PrimarySmtpAddress = "adugger@contoso.com" };
esb.ExchangeImpersonation = imp;

FindItemType fit = new FindItemType();
fit.ItemShape = new ItemResponseShapeType { BaseShape = DefaultShapeNamesType.Default };
fit.ParentFolderIds = new DistinguishedFolderIdType[]
{ 
     new DistinguishedFolderIdType{ Id = DistinguishedFolderIdNameType.calendar }
};
fit.Traversal = ItemQueryTraversalType.Shallow;

FindItemResponseType firt = esb.FindItem(fit);

As you can see from the code above, there is no longer a need to explicitly specify the mailbox when indicating which folders we will be searching. Instead, we tell the proxy instance that we are impersonating Anne Dugger's account by specifying her email address in the ConnectingSID property of an ExchangeImpersonationType that we bind to the proxy instance.  You can also use the principal name here if you desire, but note that this is may be different than the email address for the target user.

No matter which way you go about getting a user's information, since you can only query one user at a time, you will probably want to employ your favorite threading techniques so that you can get multiple results back in a relatively short amount of time.

Let's talk a bit about shapes.  At least for the FindItem (and perhaps GetItem) service operations, there is a property called ItemShape. This property essentially defines what we expect to come back in the response message from Exchange.  In the above example, We're telling exchange to bring back the default properties for calendar items, but what if we wanted to include a couple of non standard properties in the result? Well, we can specify these in the AdditionalProperties Property of the ItemResponseShapeType class, as in the below example:

fit.ItemShape = new ItemResponseShapeType
{ 
     BaseShape = DefaultShapeNamesType.Default, 
     AdditionalProperties = new PathToUnindexedFieldType[]
     { 
          new PathToUnindexedFieldType{ FieldURI = UnindexedFieldURIType.calendarIsMeeting },
          new PathToExceptionFieldType{ FieldURI = UnindexedFieldURIType.calendarIsRecurring }
     } 
};

The code above specifies that we want 2 custom properties to be returned, one indicating whether the appointment is a meeting and the other to indicate whether the appointment is recurring or not. We can then use these properties in the response should we have any logic for whether or not to process recurring meetings differently than other types of meeting.

Just as an aside, when searching for items in Exchange, you can specify grouping and paging options as well as restriction and sorting options.  I won't go into details about all of these, suffice it to say that the MSDN example in the link above shows all of these aspects of the FindItem operation. 

So that wraps up the basics of how to connect to and impersonate with Exchange 2007 web services.  I've only really played around with it for an hour, and it's pretty obvious that I only really used the FindItem operation since I only needed basic properties to be returned about the appointments I was searching for. For more details about the appointments (like the invitees, etc) I would have to call GetItem on each item that was returned from FindItem, passing the Item's id in as the reference. The API has the ability to do much more than search, I can programmatically create appointments, messages and contacts as well, but the underlying infrastructure is the same as it is for finding an item, so getting an understanding of it for the FindItem operation will translate to other operations as well. 

Shout it kick it on DotNetKicks.com Bookmark and Share