Subscribe Now: Feed Icon

Monday, December 26, 2011

ArcObjects: Workspace Provider

This post is the fifth post in my ArcObjects series.

As I mentioned in my previous post you cannot create a workspace you have to receive it from some place. That place is called the WorkspaceFactory (or as usual IWorkspaceFactory).  Now since getting the workspace is useful and usually only done once we put it in a singleton class named WorkspaceProvider.

As can be seen in the IWorkspaceFactory documentation there are many implementation of this interface. The implementation differ in the Space where the workspace is doing it’s work:

  1. SdeWorkspaceFactory – works on a Sde DB
  2. CadWorkspaceFactory – works on CAD files
  3. AccessWorkspaceFactory and FileGDBWorkspaceFactory – work on File based Geodatabases

These classes provide the interface to connect to the Space the same way ArcDesktop tools do so by the Open method. You can either use a file containing the connection (created in ArcCatalog), use IPropertySet a list containing the connection information (see here for details) or by using connection string (the other options just use this option behind the scene).

Sde connection string are separated to two categories (more details in one of my previous posts: ArcSDE: Connection to the Geodatabase):

  • Regular connection: "SERVER={0};INSTANCE={4};USER={2};PASSWORD={3};Database={1};VERSION=SDE.Default"
  • Direct connection: "SERVER={0};INSTANCE=sde:{5}:{0};USER={2};PASSWORD={3};Database={1};VERSION=SDE.Default"


  • {0} – Server name
  • {1} – Database name
  • {2} – User name
  • {3} – password
  • {4} – Sde service number
  • {5} – Type of the server (i.e. sqlserver)
  • If you need to you can also add the Sde version

As you can see direct connection doesn’t use the Sde service number, that is because it doesn’t use the Sde service (it does all the work at the client). Most times you won’t need to change the Server type so that’s out as well. My code only uses Direct Connection string (since ESRI recommends using it).


In my experience most applications will use only one IWorkspace – the one connecting them to the application Sde. For that reason WorkspaceProvider uses DefaultDbConfigSection which is both a Configuration Section and a Connection String builder (I use it for both the Sde and regular DB Connection string). To prevent unnecessary creation of IWorkspace objects WorkspaceProvider will also store the last IWorkspace created and the connection string used to create it.

As it is the only public methods needed for this class are:

1. Using Sde Connection String from web/app.config file:

  1. public WorkspaceUtils GetWorkspace()

2. Using Sde Connection String provided:

  1. public WorkspaceUtils GetWorkspace(string sdeConnectionString)

3. Using a Cad file:

  1. public CadWorkspaceUtils GetCadWorkspace(string path)

4. Using either GDB or MDB file paths:

  1. public FileWorkspaceUtils GetFileWorkspace(string filePath)

The full code can be found here.


Most of the code is self-explanatory except one part – getting a cached Workspace. Now you might think this is the most simple part but when taking into account that sometimes connections fail, servers are down and Murphy is constantly prowling than you must be ready for that “What If…”. I will write more on this in one of the next posts.

The rest of the code is pretty much self-explanatory and can be found in my CodePlex Project, or directly here.



MSDN: How to: Create Custom Configuration Sections Using ConfigurationSection