Channel Factory

What is Channel Factory
There are three ways to create a WCF client:

  1. Proxy – Using VS or SvcUtil.exe
  2. WCF Channel Factory
  3. REST services, using HttpClient or WebClient

A Channel Factory enables you to create a communication channel to the service without a proxy.
A Channel Factory is implemented by the IChannelFactory Interface.
The Channel Factory class is useful when you want to share a common service contract DLL between the client and the server.

Using the ChannelFactory<T> class is an easier alternative to making calls to the WCF services to the laborious process of generating proxies via the SvcUtil.exe tool every time a service contract changes. Channel Factory is a factory for creating service communication channels at runtime.
ChannelFactory<T> takes a generic parameter of the service type (it must be a service contract interface) to create a channel.

Implementation
For using channel factory, the following steps are necessary:

  • Create a binding in WCF
  • Create a ChannelFactory class
  • Create a Channel
  • Send-Receive messages over that channel
[ServiceContract]
public interface IService1
{

    [OperationContract]
    string GetData(int value);

}

How to Call WCF Service Use Channel Factory

//1. Create a binding  in WCF
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress("http://localhost:3047/Service1.svc");

//2. Create a ChannelFactory class
ChannelFactory myChannelFactory = new ChannelFactory(myBinding, myEndpoint);
//3. Create a Channel
IService1 instance = myChannelFactory.CreateChannel();

//4. Send-Receive messages over that channel
Console.WriteLine(instance.GetData(10));

myChannelFactory.Close();

Difference between proxy and channel factory

PROXY Channel Factory
1 Only require URL where the service resides You must have direct access to the assembly that contains that service contract T for
2 Very Simpler Not easier
3 Easy to Understand channels are complex, network-related
4 There is Visual Studio gives you add the reference When you share a common service contract dll between the client and the server, you’ll be using the ChannelFactory class
5 Proxies have several restrictions like:

  1. Properties need to have gets and sets
  2. Contructors can’t be exposed
  3. Methods other than the service contract cannot be exposed
If you know that your entities will not change much and the client code is less, then a DLL would work better than a proxy
6 By using SVCutil.exe you will create PRoxy When you are using DLL that refers Service contract interface then use the channel factory class

When to use a ChannelFactory i.e. a shared DLL?

A DLL is helpful if the client code is under you control and you’d like to: share more than just the service contract with the client — such as some utility methods associated with entities and make the client & the service code more tightly bound. If you know that your entities will not change much and the client code is less, then a DLL would work better than a proxy. Proxies have several restrictions like:

  1. Properties need to have gets and sets
  2. Contructors can’t be exposed
  3. Methods other than the service contract cannot be exposed

What all to package in the DLL?

You’ll need to package the following:

  1. Entities
  2. Service contract — the interface
  3. RequestMsg classes (if any)
  4. ResponseMsg classes (if any)

So if you are designing a connected system and using WCF for that, then you can use a shared dll instead of a proxy, to avoid code repetition and be more effective in the use of the service entities.


Refrences:

http://www.codeproject.com/Tips/558163/Difference-between-Proxy-and-Channel-Factory-in-WC

Advertisements

Leave a comment

Filed under WCF

What does cherry-picking a commit with git means?

Cherry picking in git means to choose a commit from one branch and apply it onto another.

This is in contrast with other ways such as merge and rebase which normally applies many commits onto a another branch.

Make sure you are on the branch you want apply the commit to.

git checkout master

Execute the following:

git cherry-pick

Refrences:-
What does cherry-picking a commit with git means?

Leave a comment

Filed under .Net

Transaction in WCF

http://msdn.microsoft.com/en-us/library/ff384250(d=printer).aspx

http://www.c-sharpcorner.com/UploadFile/81a718/simple-steps-to-enable-transactions-in-wcf/

http://www.c-sharpcorner.com/uploadfile/shivprasadk/wcf-faq-part-5-transactions/

Leave a comment

Filed under WCF

Yield keyword in C#

What is the use of c# “Yield” keyword.

Leave a comment

Filed under C#

Calling Page_View jQuery function

1. Include utility.js which include below script:

$(document).ready(function () {
    var activePage = $("body");
    activePage.InitializeComponent({ Init: $("body").attr("id") });
});

(function () {
    var baseMethods = {
        InitializeComponent: function (options) {
            var defaults =
                {
                    Base: "Page_",
                    Init: "",
                    Data: "",
                    Date: ""
                };
            var opts = $.extend(defaults, options);
            var functionName = opts.Base + opts.Init;
            var C_Ref = null;
            C_Ref = $(this);
            var state = (C_Ref.data("IsBinded") === undefined || C_Ref.data("IsBinded") === null || !C_Ref.data("IsBinded")) ? false : true;

            if (typeof window[functionName] !== "undefined") {
                window[functionName]({ Handled: state, Reference: C_Ref, Data: opts.Data, Date: opts.Date });
                C_Ref.data("IsBinded", true);
            }
        }
                      
    };

    jQuery.each(baseMethods, function (i) {
        jQuery.fn[i] = this;
    });
})(jQuery);

2. Now add a Common js file all views which has js function Page_ for all view pages. Like

 function Page_HelloJQueryTest(e) {
	alert('hi Demo');
 }

here ‘HelloJQueryTest’ is the name of my view.

3. Add both above js file reference layout view. And in html body tag of layout add id attribute i.e. . So your layout will look like:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/utility.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Demo.js")" type="text/javascript"></script>
</head>

<body id="@ViewBag.PageID">
    @RenderBody()
</body>
</html>

4. Add ViewBag.PageID = “HelloJQueryTest” to each view which include above layout. Like:
For View HelloJQueryTest.cshtml

	@{
		ViewBag.Title = "HelloJQueryTest";
		ViewBag.PageId = "HelloJQueryTest";
		Layout = "~/Views/Shared/_Layout.cshtml";
	}

	<h2>HelloJQueryTest</h2>
   

5. Above view will have controller action defined. Like:

	public class DemoController : Controller
        {
             public ActionResult HelloJQueryTest()
             {
                return View();
             }
        }
       

6. Now when you run your MVC application then for “HelloJQueryTest” all script user function Page_HelloJQueryTest(e) will be called.
Happy coding

Leave a comment

Filed under jQuery

Garbage Collector

When space is not available on heap, new keyword triggers GC for heap cleanup and then reclaims the space

Leave a comment

Filed under C#

Sessionless Controller

With Sessionless Controller you can easily control your session behavior for controllers.
Before going further better to understand Concurrent Requests in MVC & what are the limitation with the same. And how it is resolved with Sessionless Controller

Leave a comment

Filed under MVC