using System; using System.ServiceModel; public class MyService : IMyService { public string GetData(int value) { return string.Format("You entered: {0}", value); } } public class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(MyService)); host.Open(); Console.WriteLine("Service is running"); Console.ReadLine(); host.Close(); } }
using System; using System.ServiceModel; [ServiceContract] public interface IMyService { [OperationContract] string GetData(int value); } public class MyService : IMyService { public string GetData(int value) { return string.Format("You entered: {0}", value); } } public class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(MyService)); host.AddServiceEndpoint(typeof(IMyService), new NetTcpBinding(), "net.tcp://localhost:8000/MyService"); host.Open(); Console.WriteLine("Service is running"); Console.ReadLine(); host.Close(); } }In this example, we define a WCF service and interface, just like in example 1. However, this time we also create a service endpoint using the AddServiceEndpoint method. We use the NetTcpBinding and specify the URL for the endpoint. We open the host and wait for user input before closing it. Package library: System.ServiceModel.