【发布时间】:2010-02-12 15:34:36
【问题描述】:
如何从客户端控制台应用程序使用主机控制台应用程序中的 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();
}
}
}
}
Sky,再次感谢您。你已经读懂了我的想法。
但是我遇到了一个问题:
这是我为主机和客户端修改上述代码的方式:
主持人
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。
如果您有任何建议,请告诉我。 谢谢 否
【问题讨论】:
-
使用工作示例编辑答案
标签: wcf