Tuesday, December 11, 2012

Svcutil and xsd imports

When wanting to generate a client proxy through svcutil, I always forget the exact syntax when I have a WSDL with xsd imports - i.e. when you get the syntax wrong (or don't have the xsd's included in the command line at all) you'll no doubt get the following errors:

Error: Schema with target namespace 'http://namespace/v1' could not be found.

Error: Cannot import wsdl:portType. 

etc...

The basic command line syntax is svcutil [location of wsdl] [location of xsds] - where if there are multple xsds, then the locations are seperated by a space, e.g:

svcutil C:\MyWsdl.wsdl C:\Folder1\Xsd1.xsd C:\Folder2\Xsd2.xsd

A lot of the wsdls I build share xsds, which are in different locations, however some share the same location - but I just need to specify the location and then specify a wildcard  *.xsd, which will result in all xsd files in the folder being picked up by svcutil - e.g.

svcutil C:\MyWsdl.wsdl C:\Folder1\*.xsd C:\Folder2\*.xsd

Friday, October 12, 2012

405 Method Not Allowed on PUT and DELETE verbs

PUT and DELETE requests to a MVC 4 Web Api application we released to a customer server were failing with a 405 status code - Method Not Allowed. 

After some investigation on the web, the WebDAV module was the cause - this thread on the IIS forum has a good overview of why it occurs (PUT and DELETE are considered WebDAV verbs) and how to uninstall the feature. 

Tuesday, June 19, 2012

ASP.Net Web API and Exception Shielding

Exception shielding (or exception sanitation) is a integration pattern that should always be implemented to ensure unexpected exceptions never arrive at the client (a.k.a bleed through). 

For MVC 4's Web Api - this is achieved by overriding the OnException method, on the ExceptionFilterAttribute class. Here you can inspect the exception that was thrown, and sanitise if required (change the exception to be more generic). 

However, the mechanism to return http status codes - e.g. 404 - when a resource is not found, is to throw an HttpResponsException with the appropriate status code. This ensures that RESTful semantics are upheld. 

During development, I updated the OnException method to ignore exceptions of type HttpResponsException, as these are intended to reach the client. However the method was never invoked. After some googling, I discovered the filter is smart enough to know that HttpResponseExceptions should be ignored - "An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception. TheHttpResponseException type is a special case, because it is designed specifically for returning an HTTP response."

Monday, April 30, 2012

Fiddler & redirecting local traffic

Because I always forget the rule for redirecting requests that Fiddler receives while listening on port 8888 here it is below:

static function OnBeforeRequest(oSession: Session) {

//other statements

if (oSession.host == "localhost:8888") { oSession.host = "localhost:80" }

}

... where you can change localhost:80 to where the request needs to be redirected to

Monday, March 12, 2012

AppFabric, No monitoring data, and an empty ASEventSourcesTable table

A few months back, on some developer machines, their local install of AppFabric was failing to populate the local AppFabric monitoring database. I wrote at length here the trouble shooting I went through - i.e. all the typical components were working as expected, services, sql agents etc.... and no errors / warnings were being logged. 

The reason the monitoring events where not arriving at the monitoring database was because the ASEventSourcesTable (located in the monitoring database) was empty. 

This table is populated when the AppFabric Event Collector service is started, and for whatever reason was not being populated automatically on the machines who were not logging (the table contains a list of the services that are hosted in IIS who are applicable event sources). To date, I still haven't figured out why AppFabric is unable to populate it on certain machines. 

Using another install of AppFabric, where the ASEventSourcesTable was populated automatically, I manually inserted the missing rows into the empty ASEventSourcesTable, and boom - events start arriving in the ASWcfEventTables. 

So, along with the usual places to check when you cannot see events in the AppFabric monitoring database, check to make sure the ASEventSourcesTable has been populated. So far I have only seen this occur on Window 7 machines.

Wednesday, February 29, 2012

ActionFilterAttribute and ASP.Net Web API

Similar problem to my post around IDependencyResolver,

Custom action filters for web api controllers need to be derived from  System.Web.Http.Filters.ActionFilterAttribute

.... rather than System.Web.Mvc.ActionFilterAttribute

Thursday, February 23, 2012

IDependencyResolver and ASP.Net Web API

I've started playing around with the new ASP.Net Web Api (available as part of MVC 4), and was completely stumped as to why the GetService method on IDependencyResolver was not being called when my API controller was being requested. 

Controllers that were derived from Controller, GetService was called, however any controller deriving from ApiController GetService was not being fired (resulting in the No parameterless constructor defined for this object exception being thrown). 

Little did I know, IDependencyResolver for ApiControllers is a completely different type and is 'set' on a different resolver. 

Web API: System.Web.Http.Services.IDependencyResolver

Http: System.Web.Mvc.IDependencyResolver
And for setting the resolver for ApiControllers, it should be done as GlobalConfiguration.Configuration.ServiceResolver.SetResolver()

Rather than DependencyResolver.SetResolver()