WCF Rest Services

WCF REST Starter Kit provides easy way to create rest web services. Visual studio has project templates for rest WCF services. First you need to go download “WCF REST Starter Kit Preview 2” from codeplex. http://aspnet.codeplex.com/releases/view/24644

Then Open a new project from installed templates tab Visual C#->Web->WCF Rest Service Application.

In the global.asax file you can specify the routing with RegisterRoutes() method. By changing the first argument of the ServiceRoute:

RouteTable.Routes.Add(new ServiceRoute(“RestServiceTest”, new WebServiceHostFactory(), typeof(HelloService)));

you can deploy your rest service with the url: http://localhost:58944/RestServiceTest (port will not be same in your localhost).  It uses ASP.NET routing.

OperationContract is optional in Rest web services. In the template project you will see the SampleItem.cs file.

public class SampleItem
{
public int Id { get; set; }
public string StringValue { get; set; }
}

You can return objects of this type , or a collection of this type.

Service1.cs has some unimplemented method declerations. These will guide you to implement your own services. I will give some additional examples for service methods and how they can be called by a client.

[WebGet(UriTemplate = “GetItems”)]
public List<SampleItem> GetItems() {

//your code

}

If you implement the above method in your service, it will be available at http://localhost:58944/RestServiceTest/GetItems , because we specified UriTemplate=”GetItems” in the method template.

You can also call methods with argumens. An example call for the method
public int Square(int number) { return number*number; }
is
http://localhost:58944/RestServiceTest/Square/number

Then the number you typed ine the above url will be mapped to the argument of the method. This is the beauty of rest services.

Now we will switch our topic to the type of response. WCF WebHttp Services returns an XML response as default. If you need JSON response then a little modification is needed. We need automaticFormatSelectionEnabled = true for changing the format of response. An example web.config:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/>
<standardEndpoints>
<webHttpEndpoint>
<!–
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
–>
<standardEndpoint name=”” helpEnabled=”true” automaticFormatSelectionEnabled=”true”/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>

We can switch our web service to return json format for every request or we can give that flexibility to the client.

[WebGet(UriTemplate = “GetItems?Format={format}”)]
 public List<GetItems> GetItems(string format) { // your code }

If the format sepcified by the client is json we can set the reponse format to json in the method:

WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;

An Http request with correct format will be enough to call this method and it will return a json string as response. WCF REST Starter Kit Preview 2 assemblies provides a method to read json object: response.Content.ReadAsJsonDataContract<T>()

References:

http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx

Turkish: http://www.buraksenyurt.com/archive.aspx#WCF-WebHttp-Services

Esri Silverlight Bezier Curve Rendering

Esri Silverlight API graphics samples have some well explained examples for rendering basic graphic objects. It is possible to define graphics symbols both using xaml and code(c#).

I will explain how to render a bezier curve using esri simple renderer with a xaml. First of all, add your silverlight project a resources.xaml. It should contain a resource dictionary that will define the behaviour of the renderer for our graphicslayer object. An example resource.xaml  :

In the resources.xaml file there are some bindings that may take your attention. For example the Fill property of the symbol is bound to Color attribute. I will initialize this attribute with a SolidColorBrush while constructing Graphic object. Than I will add the Graphic object to graphics layer.

Initialize graphicslayer with the resource dictionary and add it to map’s Layers:

Now our graphics layer is ready to render a Bezier curve. We just need to apply the bezier formula to our map points and obtain a Polyline object.

In my case I have two MapPoints ( P0 and P2) and need to draw a curve between them. It is obvious that I need one more intermediate point to apply bezier formula. I generated an intermediate point whose coordinates have equal distances from my initial points and a little away from the straight line between the initial points. The generated intermediate point,  P1, and the midpoint between the initial points forms a 2D vector which is orthogonal to the vector<p0,p2>.Now I will use the Quadratic Bézier curves formula which is explained here.

The polyline object created above contains an array of PointCollections. Each PointCollection object containts two points. First one is the end point of previous PointCollection and second one is the end point for the current PointCollection. The second point will be used in the loop as a start point for the next PointCollection.

It is time to create a Graphic object and give it to a predefined GraphicsLayer of the map to render.

That’s all. Our bezier curve should just appear on the map.

References:

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm

http://en.wikipedia.org/wiki/B%C3%A9zier_curve