如何从客户端控制台应用程序在hostconsole应用程序中使用wcf服务。 这是wcf服务:
namespace HostConsoleApplication
{
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();
}
}
}
}
天空,再次感谢你。你已经读懂了我的想法。
但是我遇到了一个问题:
以下是我为主机和客户端修改上述代码的方法:
主持人
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();
}
}
}
客户
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
}
}
}
主机运行正常,当客户端在这里运行是我得到的例外:
无法连接到net.tcp:// localhost:8001 / Service。连接尝试持续时间跨度为00:00:00.8899011。 TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:8001。
如果您有任何建议,请与我们联系。 谢谢 Ñ
答案 0 :(得分:1)
最简单的实现,不知道细节...并且有一些关于错误处理和处理错误的问题,但这应该让你去....
您需要确保为服务实现了一个接口,并确保服务和客户端之间的绑定和地址匹配。
注意:我使用net.tcp有几个原因.1)我知道它符合激励这个问题的要求2)它消除了处理2008年/ vista / 7上托管http服务的很多陷阱
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();
}
}
}