3# SmileTable Architecture – take a look WCF

In this post I want to show How WCF works on SmileTable application example. This is how I am understending WCF after created some hello world applications and watched/ readed many totorials.

In second part of SmileTable series I described how SmileTable solution architecture looks like. If You are interested in this post You can open that link to better understending this post.

Okey so let’s start saying what I have sayed already. In service oriented architecture client is connecting to services which are some kind of api. What I mean is service goal is to do the same what Web API is doing but in different way. Services exposing methods to client applications by Service Contracts. What is important in SOA database is on the service side, so client applications don’t have connection strings, so services are connected to database.

We have SmileTable ASP.NET MVC5 client application. That is correct, we don’t have access to database and our application is just some kind of presentation layer WebUI.
In SmileTable application the key is to publish, rate and comment post basically.So there is need to have that data. And to have that data we need to connect to database. Like I said we don’t have database so we need to connect to sth what give us access that data.
Like I mentioned in this post there is possibility to know what kind of methods service is exposing to us. Of course in our case we don’t need to know that because It’s my own WCF service. In case when we use services which are property of somebody from outside we glad to know what we are able to use. To have that knowledge creating WCF we can add additional configuration in app.config on which base is generated WSDL document. This WSDL expose SOAP message with avaliable service methods.To access that SOAP message all You have to do is type your endpoint adress to browser. For example ://localhost:8080/?wsdl.


Now we can move next. We want to display in our main page all posts. Firstly we have to create Proxy class. We are creating that class to be able to access to WCF service. Proxy class have to inherit from ClientBase<T> where T is service contract. If our proxy class implements service contracts we have access to service contracts methods. We need to call Chanell what is ClientBase<T> property to invoke service contract method.

SmileTable Proxy class

public class PublishClient : ClientBase&amp;amp;lt;IPublishService&amp;amp;gt;, IPublishService
public IEnumerable&amp;amp;lt;PostData&amp;amp;gt; GetAllPosts()
return Channel.GetAllPosts();

Endpoint configuration

Now It’s a good time to talk about endpoints, because wee need to know how exactly we are going to have access to that service from our Proxy class.
In SmileTable Application we have client folder and we have business folder where we have our logic associated with services: contracts and services. Let’s simplyfi we have two boxes:

1. CLIENT: with proxy class, client UI and configuration.
2. SERVICE: with ServiceContract, DataContract Service, ServiceHost and Configuration

From CLIENT box we want to connect to SERVICE box. When Proxy class is invoked, It looks for endpoint information so we have to add that configuration in CLIENT box App.config. In SmileTable I put It to ST.SmileTableClient Web.config and It looks like:

contract=&quot;ST.Business.Contracts.Service_Contracts.IPublishService&quot; /&amp;amp;gt;

Basically in endpoint we must declare all informations which are necessary to connect to service. Firstly we have address where we are declaring weher is our service hosted. Secondly we have binding. In binding we are declaring which protocol we are going to use to contact to the server. In contract we are declaring which operations are exposed to the client.

Service Host

If we run that and will try to invoke service method we got error because we haven’t access to Service yet. To do that we will need to implement Service Host to open that communication. On the service side we need also have endpoint configuration which I put in ServiceHost class App.config in SmileTable Aplication . That configuration is very similiar to configuration on aplication side and works in the same way.
Of course one service can have many endpoints for example in different type of binding

&amp;amp;lt;service name=&quot;ST.Business.Services.Services.PublishService&quot;&amp;amp;gt;
contract=&quot;ST.Business.Contracts.Service_Contracts.IPublishService&quot; /&amp;amp;gt;

There is convenction for Proxy classes. Proxy class should have the same name like service with „Client” end word. So If we have PublishService.cs proxy class should name PublishClient.cs.

To be able to use service method we just must run service host which is in SmileTable project console application. Now we have access to services methods.

To run our application we have two options. We can right click on host console application select debug -> start new instance and do the same thing on client application, or we can right click on our solution -> Properties -> choose multiple startup projects -> select host and client application and click OK. Now if You will click your Start button You will starting host and client application on the same time.

service started


public class PublishService : IPublishService
var postRepository = new PostRepository&lt;/pre&gt;
&lt;pre&gt;public IEnumerable&lt;PostData&gt; GetAllPosts()
IEnumerable&lt;Post&gt; posts = postRepository.GetAllPosts();

List&lt;PostData&gt; postsData = posts
.Select(p =&gt; new PostData
PostId = p.PostId,
Title = p.Title,
Message = p.Message,
User = new UserData { UserId = p.User.UserId, FirstName = p.User.FirstName }

return postsData;

In services I am connecting to database but I am not doing this in service. Iam using for that additional repository layer. This is meaning in Service class I don’t use database context. Instead of this I create new instance of my repository in service, and run repository method GetAllPosts(). In repository class I have database context and methods -> CRUD operations which are using that context. So repository return DTO objects and in service I map that DTO object to DataContract, because DataContract It’s what we return from WCF service to client and next In client I map DataContrac to view model to display that data for example in my view.

That Diagram ilustrate what I have written above





Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s