ReSharper cannot resolve symbol

Sometimes R# tends to go nuts and stops 'understanding' the code properly: 


In such case wiping out its cache and rebooting VS seems to do the trick.
R# 10 cache folder is: %localappdata%\JetBrains\Transient\ReSharperPlatformVs14

Web API 2 params binding

This is pretty much a reblog from http://damienbod.com/2014/08/22/web-api-2-exploring-parameter-binding/ so the full credit goes there too.

Posted here as i find it useful and it's way easier for me to rememeber one url ;)

anyway, a couple of nice examples of param binding in web api 2


  1. Sending a simple parameter in the Url
  2. [RoutePrefix("api/values")]
    public class ValuesController : ApiController
    {
      // http://localhost:49407/api/values/example1?id=2
      [Route("example1")]
      [HttpGet]
      public string Get(int id)
      {
         return "value";
      }
    }
    
  3. Sending simple parameters in the Url
  4. // http://localhost:49407/api/values/example2?id1=1&id2=2&id3=3
    [Route("example2")]
    [HttpGet]
    public string GetWith3Parameters(int id1, long id2, double id3)
    {
        return "value";
    }
    
  5. Sending simple parameters using attribute routing
  6. // http://localhost:49407/api/values/example3/2/3/4
    [Route("example3/{id1}/{id2}/{id3}")]
    [HttpGet]
    public string GetWith3ParametersAttributeRouting(int id1, long id2, double id3)
    {
       return "value";
    }
    
  7. Sending an object in the Url
  8. // http://localhost:49407/api/values/example4?id1=1&id2=2&id3=3
    [Route("example4")]
    [HttpGet]
    public string GetWithUri([FromUri] ParamsObject paramsObject)
    {
      return "value:" + paramsObject.Id1;
    }
    
  9. Sending an object in the Request body
  10. [Route("example5")]
    [HttpPost]
    public string GetWithBody([FromBody] ParamsObject paramsObject)
    {
      return "value:" + paramsObject.Id1;
    }
    
  • Calling the method using Urlencoded in the body
  • User-Agent: Fiddler
    Host: localhost:49407
    Content-Length: 32
    Content-Type: application/x-www-form-urlencoded
     
    id1=1&id2=2&id3=3
    
  • Calling the method using Json in the body
  • User-Agent: Fiddler
    Host: localhost:49407
    Content-Length: 32
    Content-Type: application/json
     
    { "Id1" : 2, "Id2": 2, "Id3": 3}
    
  • Calling the method using XML in the body (requires some extra code in Global.asax
  • protected void Application_Start()
    {
       var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
       xml.UseXmlSerializer = true;
    
    User-Agent: Fiddler
    Content-Type: application/xml
    Host: localhost:49407
    Content-Length: 65
     
    <ParamsObject><Id1>7</Id1><Id2>8</Id2><Id3>9</Id3></ParamsObject>
    
  1. Sending a simple list in the Url
  2. // http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9
    [Route("example6")]
    [HttpGet]
    public string GetListFromUri([FromUri] List paramsObject)
    {
      if (paramsObject != null)
      {
        return "recieved a list with length:" + paramsObject.Count;
      }
     
      return "NOTHING RECIEVED...";
    }
    
  3. Sending an object list in the Body
  4. // http://localhost:49407/api/values/example8
    [Route("example8")]
    [HttpPost]
    public string GetListFromBody([FromBody] List paramsList)
    {
      if (paramsList != null)
      {
         return "recieved a list with length:" + paramsList.Count;
      }
     
      return "NOTHING RECIEVED...";
    }
    
  • Calling with Json
  • User-Agent: Fiddler
    Content-Type: application/json
    Host: localhost:49407
    Content-Length: 91
     
    [{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]
    
  • Calling with XML
  • User-Agent: Fiddler
    Content-Type: application/xml
    Host: localhost:49407
    Content-Length: 258
     
    <ArrayOfParamsObject>
    <ParamsObject><Id1>3</Id1><Id2>76</Id2><Id3>19</Id3></ParamsObject>
    <ParamsObject><Id1>56</Id1><Id2>87</Id2><Id3>94</Id3></ParamsObject>
    <ParamsObject><Id1>976</Id1><Id2>345</Id2><Id3>7554</Id3></ParamsObject>
    </ArrayOfParamsObject>
    
  1. Sending object lists in the Body
  2. [Route("example8")]
    [HttpPost]
    public string GetListsFromBody([FromBody] List> paramsList)
    {
      if (paramsList != null)
      {
        return "recieved a list with length:" + paramsList.Count;
      }
     
      return "NOTHING RECIEVED...";
    }
    
    POST http://localhost:49407/api/values/example8 HTTP/1.1
    User-Agent: Fiddler
    Content-Type: application/json
    Host: localhost:49407
    Content-Length: 185
     
    [
     [
      {"Id1":3,"Id2":76,"Id3":19},
      {"Id1":56,"Id2":87,"Id3":94},
      {"Id1":976,"Id2":345,"Id3":7554}
     ],
     [
      {"Id1":3,"Id2":76,"Id3":19},
      {"Id1":56,"Id2":87,"Id3":94},
      {"Id1":976,"Id2":345,"Id3":7554}
     ]
    ]
    

Installing pgsql as a aservice on Windows

Get the latest pgsql from here and if you need postgis from here.

Unpack the pgsql zip to a desired destination.

Create a directory for the db files.

Navigate to the bin folder of pgsql and run the following command: 

initdb -U postgres -E UTF-8 -A md5 -D <path to data directory> -W

When prompted, provide the master pass. Your console should look like this:


Now it's time to adjust the db cfg. You may find it in the db files folder, it is called "postgresql.conf". Make sure you adjust the port the db listens on and also that it is available for localhost:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = 'localhost'		# what IP address(es) to listen on;
					# comma-separated list of addresses;
					# defaults to 'localhost'; use '*' for all
					# (change requires restart)
#port = 5432				# (change requires restart)

By default pgsql listens on 5432. If you already have another pgsql live or need to change it for whatever reason, simply adjust the port value.

Once this is ready, it's time to create a service for our db (you may need to run the console as an admin though):

pg_ctl register -N ServiceName -U postgres -P <password> -D <path to data directory> -w

The console should look like this:


And the service should be visible in the service manager:


If you wish to rename the service then simply delete it and register again:

sc delete ServiceName

The final step is to adjust the service properties, such as a user that runs the service (by default it runs as NetworkService) or the startup mode:



Now, if you also need postgis, simply use the exe installer and you're good to go.

OGC xml schemas to c# classes using xsd.exe

The process of generating c# classes off the xsd is usually rather simple. One needs the xsd.exe utility (for example located here: C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools) and then a simple command to get things done:

xsd schema.xsd /c

Sometimes there may be some problems though with missing refs, namespaces and such.

In this example, I will generate classes for WMS 1.3.0 based on the OGC schemas - it is possible to grab them one by one, or all the goodies wrapped as a zip archive.

I have extracted WMS 1.3.0 schemas to f:\wms\schemas, so my command should look like this when in f:\wms (I have copied xsd.exe there too):

xsd schemas\capabilities_1_3_0.xsd /c

Unfortunately there are some problems and classes do not get generated:

The problem here is caused by unresolved references. Xsd was not able to find xlink.xsd. Although there is a xlink schema in OGC repo, this is not the one we're after. The one in question is the mentioned http://www.w3.org/1999/xlink, and its schema definition is http://www.w3.org/1999/xlink.xsd, pretty much as defined in the very beginning of the capabilities_1_3_0.xsd I am trying to process:

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.opengis.net/wms" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:wms="http://www.opengis.net/wms" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.3.0.2">
	
	<!--
		WMS is an OGC Standard.
		Copyright (c) 2004,2010 Open Geospatial Consortium.
		To obtain additional rights of use, visit http://www.opengeospatial.org/legal/ .
	-->

	<import namespace="http://www.w3.org/1999/xlink" schemaLocation="http://www.w3.org/1999/xlink.xsd"/>

Unfortunately xsd.exe does not resolve the import automatically, so in order to make it work I just copied the xlinx.xsd to my schemas folder and specified it in the command:

xsd schemas\xlink.xsd schemas\capabilities_1_3_0.xsd /c

Looks like this did the trick and the classes got generated as expected. The complete set of files is here: 

ogc_wms_cs_from_xsd.zip (51.1KB)