WCF Service
IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NetTcpServicetoHostinWindowsServices
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
}
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace NetTcpServicetoHostinWindowsServices
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="NetTcpServicetoHostinWindowsServices.Service1" behaviorConfiguration ="MyBehavior">
<host>
<baseAddresses>
<add baseAddress = "net.tcp://localhost:9999/Service1/" />
</baseAddresses>
</host>
<endpoint name ="NetTcpEndPoint"
address =""
binding="netTcpBinding"
contract="NetTcpServicetoHostinWindowsServices.IService1">
</endpoint>
<endpoint name ="NetTcpMetadataPoint"
address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="MyBehavior">
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Windows Service
Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using NetTcpServicetoHostinWindowsServices;
namespace HostingWindowsService
{
public partial class Service1 : ServiceBase
{
internal static ServiceHost myHost = null;
BackgroundWorker worker;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
if (myHost != null)
{
myHost.Close();
}
myHost = new ServiceHost(typeof(NetTcpServicetoHostinWindowsServices.Service1));
myHost.Open();
}
protected override void OnStop()
{
if (myHost != null)
{
myHost.Close();
myHost = null;
}
}
}
}
Client Application
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleClient.ServiceReference1;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
Service1Client proxy = new Service1Client();
Console.WriteLine(proxy.GetData(9));
Console.Read();
}
}
}
Leave a Reply