<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://beyondrelational.com/live/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'defining data contracts'</title><link>http://beyondrelational.com/live/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=defining+data+contracts&amp;orTags=0</link><description>Search results matching tag 'defining data contracts'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Introduction to WCF Service</title><link>http://beyondrelational.com/live/blogs/dinesh/archive/2010/07/11/introduction-to-wcf-service.aspx</link><pubDate>Sun, 11 Jul 2010 20:22:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:7249</guid><dc:creator>dinesh</dc:creator><description>&lt;p&gt;In my &lt;u&gt;&lt;a href="http://beyondrelational.com/blogs/dinesh/archive/2010/06/26/bing-maps-binding-xml-data-source.aspx"&gt;&lt;font color="#0000ff"&gt;previous post&lt;/font&gt;&lt;/a&gt;&lt;/u&gt; I described how to add multiple pushpins on Bing Map by fetching data from XML data source. In this post I will explain some basics of &lt;b&gt;WCF Services&lt;/b&gt; to add multiple pushpins on Bing Map by fetching data from SQL Server Database.&lt;/p&gt;  &lt;p&gt;We cannot access SQL Server directly from Silverlight. There needs to be a layer in between Silverlight and the SQL Server. You need to use &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms731082.aspx"&gt;&lt;font color="#0000ff"&gt;WCF Services&lt;/font&gt;&lt;/a&gt;&lt;/strong&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx"&gt;&lt;font color="#0000ff"&gt;ADO.NET Entity Services&lt;/font&gt;&lt;/a&gt;&lt;/b&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are many resources available for WCF Services over the internet. Here I would like to describe basic concepts related with WCF required to achieve our main goal.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;font size="4"&gt;What is WCF&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/aa480190.aspx"&gt;&lt;font color="#0000ff"&gt;Windows Communication Foundation (WCF)&lt;/font&gt;&lt;/a&gt;&lt;/b&gt; is a part of the .NET Framework that provides a unified programming model for rapidly building service-oriented applications that communicate across the web and the enterprise.&lt;/p&gt;  &lt;p&gt;&lt;i&gt;&lt;/i&gt;WCF comes up as a replacement for &lt;b&gt;Remoting&lt;/b&gt; and &lt;b&gt;Web service &lt;/b&gt;in donet.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="3"&gt;Web Services:&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It can be accesses only over HTTP. &lt;/li&gt;    &lt;li&gt;It works in &lt;b&gt;stateless&lt;/b&gt; environment. &lt;/li&gt;    &lt;li&gt;It supports only the data types defined in the XSD type system, limiting the number of objects that can be serialized. &lt;/li&gt;    &lt;li&gt;It support interoperability across platforms, and are ideal for &lt;b&gt;heterogeneous&lt;/b&gt; environments. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="3"&gt;.Net Remoting:&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;It can be accessed over any protocol &lt;/li&gt;    &lt;li&gt;Provide support for both &lt;b&gt;stateful&lt;/b&gt; and &lt;b&gt;stateless&lt;/b&gt; environments through Singleton and SingleCall objects &lt;/li&gt;    &lt;li&gt;Using binary communication, .NET Remoting can provide support for rich type system. &lt;/li&gt;    &lt;li&gt;It requires the client be built using .NET, enforcing &lt;b&gt;homogenous&lt;/b&gt; environment. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="3"&gt;WCF Service:&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;WCF support of almost all protocols like TCP, MSMQ, NamedPipe , Peer2Peer and HTTP. &lt;/li&gt;    &lt;li&gt;WCF Service works asynchronously. &lt;/li&gt;    &lt;li&gt;WCF service can be hosted on self hosting application, IIS and WAS (windows application service). &lt;/li&gt;    &lt;li&gt;WCF is used to implement Service oriented architecture. Service oriented architecture means collection of services communicated with each other with the help of messages &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;font size="4"&gt;ABC of WCF&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://beyondrelational.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/dinesh/wcfabc_5F00_08809BF8.png"&gt;&lt;img title="ABC of WCF Services" style="border-top-width:0px;display:block;border-left-width:0px;float:none;border-bottom-width:0px;margin-left:auto;margin-right:auto;border-right-width:0px;" height="302" alt="ABC of WCF Services" src="http://beyondrelational.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/dinesh/wcfabc_5F00_thumb_5F00_64E98F01.png" width="544" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&amp;quot;A&amp;quot; stands for Address: Where is the service? &lt;/li&gt;    &lt;li&gt;&amp;quot;B&amp;quot; stands for Binding: How do I talk to the service? &lt;/li&gt;    &lt;li&gt;&amp;quot;C&amp;quot; stands for Contract: What can the service do for me? &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Writing and configuring a WCF service is always a three-step process: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;You define a contract and implement it on a service &lt;/li&gt;    &lt;li&gt;You choose or define a service binding that selects a transport along with quality of service, security and other options &lt;/li&gt;    &lt;li&gt;You deploy an endpoint for the contract by binding it (using the binding definition, hence the name) to a network address. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="4"&gt;Contracts&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The contract is a platform-neutral and standard way of describing &lt;b&gt;what the service does.&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;WCF defines four types of contracts. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms733070.aspx"&gt;&lt;font color="#0000ff"&gt;Service Contracts&lt;/font&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms733127.aspx"&gt;&lt;font color="#0000ff"&gt;Data Contracts&lt;/font&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms733721.aspx"&gt;&lt;font color="#0000ff"&gt;Fault Contracts&lt;/font&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms730255.aspx"&gt;&lt;font color="#0000ff"&gt;Message Contracts&lt;/font&gt;&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;b&gt;&lt;font size="3"&gt;Service Contracts&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Describe which operations the client can perform on the service. There are two types of Service Contracts.      &lt;br /&gt;&lt;b&gt;ServiceContract&lt;/b&gt; - This attribute is used to define the Interface.       &lt;br /&gt;&lt;b&gt;OperationContract&lt;/b&gt; - This attribute is used to define the method inside Interface.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;b&gt;&lt;font color="#000000" size="3"&gt;Data contracts&lt;/font&gt;&lt;/b&gt; &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as &lt;strong&gt;int&lt;/strong&gt; and &lt;strong&gt;string&lt;/strong&gt;, but we can easily define explicit opt-in data contracts for custom types.       &lt;br /&gt;There are two types of Data Contracts.       &lt;br /&gt;&lt;b&gt;DataContract&lt;/b&gt; - attribute used to define the class       &lt;br /&gt;&lt;b&gt;DataMember&lt;/b&gt; - attribute used to define the properties&lt;/p&gt;    &lt;p&gt;If DataMember attributes are not specified for a properties in the class, that property can&amp;#39;t be passed to-from web service. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;b&gt;&lt;font size="3"&gt;Fault contracts&lt;/font&gt; &lt;/b&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Define which errors are raised by the service, and how the service handles and propagates errors to its clients. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;b&gt;&lt;font size="3"&gt;Message contracts&lt;/font&gt; &lt;/b&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Allow the service to interact directly with messages. Message contracts can be &lt;strong&gt;typed&lt;/strong&gt; or &lt;strong&gt;untyped&lt;/strong&gt;, and are useful in interoperability cases and when there is an existing message format we have to comply with.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;b&gt;&lt;font size="4"&gt;Defining Service Contracts&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;A contract is defined &lt;em&gt;explicitly&lt;/em&gt;, via a class. You add a [&lt;b&gt;ServiceContract&lt;/b&gt;] attribute to the class. All methods you want to expose in your service, you mark as [&lt;b&gt;OperationContract&lt;/b&gt;], as methods are called operations in services.&lt;/p&gt;  &lt;pre class="brush: csharp"&gt;[ServiceContract]
public class Hello
{
    [OperationContract]
    string HelloWorld()
    {
        return &amp;quot;Hello world&amp;quot;;
    }
          
    [OperationContract]
    string HelloComputer()
    {
        return &amp;quot;Hello computer&amp;quot;;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;&lt;font size="3"&gt;Interfaces&lt;/font&gt;&lt;/b&gt; 

  &lt;br /&gt;It&amp;#39;s always best to have an interface for our contract and have our actual service implement the interface. That&amp;#39;s because&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Interfaces can extend/inherit other interfaces &lt;/li&gt;

  &lt;li&gt;A single class can implement multiple interfaces &lt;/li&gt;

  &lt;li&gt;You can modify the implementation of a service without breaking the contract &lt;/li&gt;

  &lt;li&gt;You can version your service via old and new interfaces &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The attributes also must be specified in the interface.&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;[ServiceContract]
public interface IHello
{
   [OperationContract]
   string HelloWorld();

   [OperationContract]
   string HelloComputer();
}

public class Hello : IHello
{
   string IHello.HelloWorld()
   {
       return &amp;quot;Hello world&amp;quot;;
   }

   string IHello.HelloComputer()
   {
       return &amp;quot;Hello computer&amp;quot;;
   }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;
&lt;b&gt;&lt;font size="4"&gt;Defining Data Contracts&lt;/font&gt;&lt;/b&gt; 

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;While a &lt;strong&gt;service contract&lt;/strong&gt; defines the shape and rules for interfaces along with their associated messages and operations, the &lt;strong&gt;data contract&lt;/strong&gt; defines the shape and rules for the &lt;b&gt;data&lt;/b&gt; that is exchanged through operation&amp;#39;s input and output messages.&lt;/p&gt;

&lt;p&gt;See below a data contract for a person:&lt;/p&gt;

&lt;pre class="brush: csharp"&gt;[DataContract]
public class Person
{
  [DataMember]
  public int Id;
  [DataMember]
  public string FirstName;
  [DataMember]
  public string LastName;
}&lt;/pre&gt;
This article shows the basic concept of WCF service. Although there are many other stuff needs to be understand but I think above is sufficient to achieve our basic goal: &lt;strong&gt;&lt;font size="3"&gt;Add multiple pushpins on the Bing Map by fetching data from SQL Server.&lt;/font&gt;&lt;/strong&gt; 

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;In the next article I will show that….&lt;/p&gt;</description></item></channel></rss>