Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Friday, December 18, 2009

Best practices for creation of WCF proxy's in .Net

From past few days i was doing some findings about the best way of creating the service proxy's in WCF.

Here goes the result of that exercise..

1)

In this first case, We are creating the service proxy with the help of the ChannelFactory and we are closing that factory once we are done with the exercise,but it might so happen that during close call it might throw an exception,Afterwards that proxy state becomes faulted and it may be of no use(you cant make any call on that), hence to be on the safer side, by using a bool we are cleaning the instance if at all any exception happens. How cool right?


















2)

In this second case, using block will ensure that the proxy is Disposed, because the equivalent of the below code or using statement is




using (OrderServiceReference.OrderServiceClient proxy = new WindowsFormsApplication1.OrderServiceReference.OrderServiceClient())

{

int i = proxy.GetScore();

}





Equivalent code after compilation -

OrderServiceReference.OrderServiceClient proxy = default(OrderServiceReference.OrderServiceClient);

try
{

proxy = new WindowsFormsApplication1.OrderServiceReference.OrderServiceClient();

int i = proxy.GetScore();

}

finally
{

proxy.Dispose();

}



Thats all for now...

Regards,
-Vinayak


Thursday, December 17, 2009

Exception Handling Strategy by using WCF Services at Business layer for Enterprise .Net application

Today I would like to discuss a bit on the best practices for handling Exceptions in Enterprise applications where in WCF Service is used at the business layer

Ideally any standard Enterprise application will have 3 layers – Presentation, Business and Data Access.

Predominantly the WCF role will come in to play at Business layer (where in the real business rules are defined/ elicited).

So as a best practice it would be good if you follow these thumb rule at each layer to ensure the smoother operations or flow

Rule 1:

Identify & Define the Type of exception which you want to handle at the business layer

For ex : MyException

[DataContract]

Public class MyException

{

Describe/Collect the details which you want to handle.

}

Rule 2:

Decorate the method/operation with the identified exceptions – Define FaultContract’s for the Operations

For ex:

[ServiceContract]

Public interfact IMyService

{

[OperationContract]

[FaultContract(typeof(MyException))]

Void Get();

}

Rule 3:

Throw the FaultException at the Business layer.

For ex :






















Rule 4
At the configuration side – Enable Exception propagation












Rule 5:


At the Presentation layer catch the respective exception which you are throwing from the Business layer.









That’s it!!!

I hope this helps.

Regards,
-Vinayak

Tuesday, December 1, 2009

Kick Start to REST Full .NET WCF Service

Recently I was doing some investigation about how to create a Restful .Net WCF service. I had encountered some interesting hiccups in between during the course of action, so I just thought of to share with you all.

Steps to create the Restful WCF service
  1. Create the WCF project by using VS template

  2. Add the namespace System.Servicemodel.web to the project

  3. Decorate the default service with the REST attribute to convert it into a Restful service(WebInvoke,WebGet ,URI definition to access the respective operations etc)

  4. At the configuration side of the service change the binding of an endpoint to webHttpBinding

  5. Describe the REST endpoint behavior as shown below





  6. Link the service endpoint behavior to REST endpoint description by the attribute 'bindingConfiguration' here in this case value is - 'webHttp'

  7. Finally the entry of endpoint should similar to this






Thats all is needed to have a basic REST Service.

I hope this will help you all to have a gook kick start for the REST WCF Service.

Regards,
-Vinayak