Search

Host DLL as WCF Service

Using .Net framework 4 we can host a DLL as WebServiceand here is how to do it. All you have to do is the following: Create an Interface for the DLL Code Add Reference to "using System.ServiceModel" in the code

Service Interface

// an interface as contract
namespace killercodes_in.Dll_as_WS
{
[ServiceContract()]
interface IWS_Class1
{
[OperationContract()]
int AddNum(int one, int two);

}
}





then you should implement the interface like


using System.ServiceModel;
using System.ServiceModel.Description;

// class implementing the contract(interface)
namespace killercodes_in.Dll_as_WS
{
// Service Implementation
public class WS_Class1:IWS_Class1
{
public int AddNum(int one, int two)
{
return one + two;
}
}
}

The reason using "System.ServiceModel.Description" reference is because we Need a host application to host this DLL as WS. So Add a new Class call it "Host_WS_Class1"
// Hosting WS
namespace killercodes_in.Dll_as_WS
{
public class Host_WS_Class1
{
public Uri httpUrl { get; set; }
public string Status { get; set; }
private ServiceHost WsSvcHost;
public bool IsLive { get; set; }

public Host_WS_Class1(string HostingURL)
{
//Create a URI to serve as the base address
httpUrl = new Uri(HostingURL);
//Create ServiceHost
WsSvcHost = new ServiceHost(typeof(killercodes_in.Dll_as_WS.WS_Class1), httpUrl);
//Add a service endpoint
WsSvcHost.AddServiceEndpoint(typeof(killercodes_in.Dll_as_WS.IWS_Class1), new WSHttpBinding(), "");
//Enable metadata exchange
ServiceMetadataBehavior svcMetaBehaviour = new ServiceMetadataBehavior();
svcMetaBehaviour.HttpGetEnabled = true;
WsSvcHost.Description.Behaviors.Add(svcMetaBehaviour);
IsLive = false;


}

public string Start()
{
//Start the Service
WsSvcHost.Open();
IsLive = true;
Status = string.Format("Service started at {0}", DateTime.Now.ToString().Replace('/', ':'));
return Status;
}

public string Stop()
{
//Start the Service
WsSvcHost.Close();
IsLive = false;
Status = string.Format("Service stopped at {0}", DateTime.Now.ToString().Replace('/', ':'));
return Status;
}
}
}





Now all you need is an application to call/host this DLL.
 
killercodes_in.Dll_as_WS.Host_WS_Class1 hostCls = new Host_WS_Class1("http://localhost:5000/NewSvc");
Console.WriteLine(hostCls.Start());
Console.ReadKey();




Business Delegate

Business Delegate Pattern is used to decouple presentation tier and business tier. It is basically use to reduce communication or remote lookup functionality to business tier code in presentation tier code.

In business tier we've following entities.

Client - Presentation tier code may be JSP, Asp, servlet or UI code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate
{
    class Client
    {
        BusinessDelegate businessService;

        public Client(BusinessDelegate businessService)
        {
            this.businessService = businessService;
        }

        public void doTask()
        {
            businessService.doTask();
        }
    }
}
Business Delegate - A single entry point class for client entities to provide access to Business Service methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate
{
    public class BusinessDelegate
    {
        private BusinessLookUp lookupService = new BusinessLookUp();
        private BusinessService businessService;
        private String serviceType;

        public void setServiceType(String serviceType)
        {
            this.serviceType = serviceType;
        }

        public void doTask()
        {
            businessService = lookupService.getBusinessService(serviceType);
            businessService.doProcessing();
        }
    }
}
LookUp Service - Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate
{
    class BusinessLookUp
    {
        public BusinessService getBusinessService(String serviceType)
        {
            if (serviceType.Equals("EJB"))
            {
                return new Services.Service1();
            }
            else
            {
                return new Services.Service2();
            }
        }
    }
}
Business Service - Business Service interface. Concrete classes implements this business service to provide actual business implementation logic.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate
{
    interface BusinessService
    {
        void doProcessing();
    }
}
and services as Service1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate.Services
{
    class Service1:BusinessService
    {
        public void doProcessing()
        {
            Console.WriteLine("Service 1 invoked");
        }
    }
}
and Service2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate.Services
{
    class Service2:BusinessService
    {
        public void doProcessing()
        {
            Console.WriteLine("Service 2 invoked");
        }
    }
}

To use these we need a main methods as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business_Delegate
{
    class BusinessDelegatePatternDemo
    {
        public static void main(String[] args)
        {

            BusinessDelegate businessDelegate = new BusinessDelegate();
            businessDelegate.setServiceType("WCF");

            Client client = new Client(businessDelegate);
            client.doTask();

            businessDelegate.setServiceType("EJB");
            client.doTask();
        }
    }
}

Using Dynamic Object

Dynamic objects expose members such as properties and methods at run time, instead of in at compile time. This enables you to create objects to work with structures that do not match a static type or format. For example, you can use a dynamic object to reference the HTML Document Object Model (DOM), which can contain any combination of valid HTML markup elements and attributes. Because each HTML document is unique, the members for a particular HTML document are determined at run time. A common method to reference an attribute of an HTML element is to pass the name of the attribute to the GetProperty method of the element. To reference the id attribute of the HTML element <div id="Div1">, you first obtain a reference to the <div> element, and then use divElement.GetProperty("id"). If you use a dynamic object, you can reference the id attribute as divElement.id.

Provides a base class for specifying dynamic behavior at run time. This class must be inherited from; you cannot instantiate it directly.