How to consume a wcf service in a hostconsole application from client console app
How to consume a wcf service in a hostconsole application from client console app. Here is the wcf service:
namespace Host开发者_Python百科ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(FirstWcfService.Service)))
{
host.Open();
Console.WriteLine("Sai");
Console.ReadLine();
}
}
}
}
Sky, Thank you once more. You have read my mind.
However i am running into an issue:
Here is how i have modified the above code for the host and the client:
The host
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
const string endpointAddress = "net.tcp://localhost:8001/Service";
var baseAddress = new Uri(endpointAddress);
using (var host = new ServiceHost(typeof(FirstWcfService.Service), baseAddress))
{
host.AddServiceEndpoint(typeof(FirstWcfService.IService), new NetTcpBinding(), baseAddress);
host.Open();
}
Console.WriteLine("Sai");
Console.ReadLine();
}
}
}
The Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
namespace ConsoleApplication2
{
class Program
{
private static void Main(string[] args)
{
const string endpointAddress = "net.tcp://localhost:8001/Service";
// this is the client code.. simply put this in another console app
using (var factory = new ChannelFactory<FirstWcfService.IService>(new NetTcpBinding(), endpointAddress))
{
FirstWcfService.IService channel = factory.CreateChannel();
//DateTime d = channel.GetDate();
string h = channel.Hello();
//Console.WriteLine(String.Format("It is {0}", d));
Console.WriteLine(String.Format("It is {0}", h));
}
//// end client code
}
}
}
The host runs fine, when the client is ran here is the exception i am getting:
Could not connect to net.tcp://localhost:8001/Service. The connection attempt lasted for a time span of 00:00:00.8899011. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8001.
Please let me know if you have any suggestions. Thanks N
The simplest implementation, without knowing details... and there are a few concerns with error handling and dealing with faults but this should get you going....
You need to make sure you implemented an interface for your service and make sure that the binding and address match between service and client.
Note: i am using net.tcp for a couple reason.. 1) i know it fits the requirements that are motivating this question and 2) it eliminates dealing with a lot of gotchas hosting http services on 2008/vista/7.
using System;
using System.ServiceModel;
namespace ConsoleApplication1
{
[ServiceContract]
public interface IService
{
[OperationContract]
DateTime GetDate();
}
public class ServiceImplementer : IService
{
#region IService Members
public DateTime GetDate()
{
return DateTime.Now;
}
#endregion
}
internal class Program
{
private static void Main(string[] args)
{
const string endpointAddress = "net.tcp://localhost:8001/Service";
var baseAddress = new Uri(endpointAddress);
using (var host = new ServiceHost(typeof (ServiceImplementer), baseAddress))
{
host.AddServiceEndpoint(typeof (IService), new NetTcpBinding(), baseAddress);
host.Open();
// this is the client code.. simply put this in another console app
using (var factory = new ChannelFactory<IService>(new NetTcpBinding(), endpointAddress))
{
IService channel = factory.CreateChannel();
DateTime d = channel.GetDate();
Console.WriteLine(String.Format("It is {0}", d));
}
// end client code
}
Console.WriteLine("Sai");
Console.ReadLine();
}
}
}
精彩评论