2

I use the following code to get metadata with Microsoft.Web.Administration.ServerManager:

var manager = new ServerManager();
var site = manager.Sites["siteName"];
var metadata = site.GetMetadata("metaKey");

Now, if "metaKey" is not there, the GetMetadata will throw a System.Runtime.InteropServices.COMException exception with the message "The request is not supported". If I first set a metadata value like this

site.SetMetadata("metaKey", "hello")

the GetMetadata("metaKey") won't throw an error but instead kindly return the value "hello". How do I check if a key exists before I attempt to retreieve it? I want to avoid a try catch if it's possible.

Adrian Rosca
  • 6,922
  • 9
  • 40
  • 57
  • Despite of my answer, you can look at ahadmin.dll in system32 folder, which is the native API for IIS configuration (http://blogs.msdn.com/b/carlosag/archive/2008/09/10/modifying-iis-7-0-administration-config-using-javascript-and-ahadmin.aspx). I mean you can use it without interop that throws exceptions... – Ilya Luzyanin Aug 09 '14 at 08:46

1 Answers1

1

I've tried to dig it a little in disassembling tool. Internally it uses Microsoft.Web.Administration.Interop assembly over native API (as you've already guessed, that is why there is a COMException). Methods to work with metadata are described in IAppHostElement interface.

From it's GetMetadata method's description:

Return value:
S_OK: Indicates that the operation was successful.
ERROR_NOT_SUPPORTED: Indicates that the requested metadata is not recognized.

As you can see, there is no method to check if metadata exists, and interop throws exception in any case when returning result is not S_OK. So unfortunately I guess there is no way to know about the existence of specific metadata without calling GetMatadata method and catching exceptions.

Community
  • 1
  • 1
Ilya Luzyanin
  • 7,910
  • 4
  • 29
  • 49