Sunday, July 26, 2015

Self Hosting of WCF Service

WCF service can be hosted with IIS, Self hosting & WAS server.

Self Hosting : In self hosting of WCF service we can host the service in our own application domain.
Whole WCF service can consists in an assembly, as you run the application (assembly) service got started and can be accessed.

Port you are using for service should not be in use by other IIS site and this port should be open on server in order to access it publicly.



Source Code:


Self hosting class
namespace ConsoleApplication1
{
    public class clsSelfHosting
    {
        static void Main(string[] args)
        {
            ServiceHost host;
            Uri uri = new Uri("http://localhost:1300/MyWCFSelfHosting");

            host = new ServiceHost(typeof(ConsoleApplication1.Service1), uri);
            host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);
            host.Open();
            Console.WriteLine("Service Started...");
            Console.ReadLine();
        }       
    }
}


Service Contract
namespace ConsoleApplication1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }
   
// Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
 +   public class CompositeType  ……
   
}

Service Class
namespace ConsoleApplication1
{  
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }

           + public CompositeType GetDataUsingDataContract(CompositeType composite) ……
           
        }  
}


No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...