从另一个应用程序(C#)启动WCF服务应用程序
问题描述:
我有一个WCF服务应用程序和一个Windows窗体应用程序。我想从WCF获取一些数据,但它只能从IDE自动启动。从另一个应用程序(C#)启动WCF服务应用程序
我的问题很简单:如何从Windows窗体应用程序启动WCF服务应用程序?
UPD:这2个应用程序有不同的位数,所以不能在同一个进程中托管。
答
可以使用ServiceHost类
像这样
public static void Main()
{
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
{
try
{
// Open the ServiceHost to start listening for messages.
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadLine();
// Close the ServiceHost.
serviceHost.Close();
}
catch (TimeoutException timeProblem)
{
Console.WriteLine(timeProblem.Message);
Console.ReadLine();
}
catch (CommunicationException commProblem)
{
Console.WriteLine(commProblem.Message);
Console.ReadLine();
}
}
}
但是你可以很容易地适应的WinForms,或者如果你愿意,你可以 Host a WCF Service in a Managed Windows Service
是否安装了服务?如果是这样,它的名字是什么? – rene 2015-02-07 21:59:00
@rene不,它不是 – AndrewR 2015-02-07 22:01:06
看看我更新的答案 – 2015-02-07 22:33:22