从另一个应用程序(C#)启动WCF服务应用程序

问题描述:

我有一个WCF服务应用程序和一个Windows窗体应用程序。我想从WCF获取一些数据,但它只能从IDE自动启动。从另一个应用程序(C#)启动WCF服务应用程序

我的问题很简单:如何从Windows窗体应用程序启动WCF服务应用程序?

UPD:这2个应用程序有不同的位数,所以不能在同一个进程中托管。

+0

是否安装了服务?如果是这样,它的名字是什么? – rene 2015-02-07 21:59:00

+0

@rene不,它不是 – AndrewR 2015-02-07 22:01:06

+0

看看我更新的答案 – 2015-02-07 22:33:22

可以使用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

+0

如果我从winforms应用程序调用它,服务将托管在相同的过程中,对吗? – AndrewR 2015-02-07 22:09:31

+0

是的,你是对的 – 2015-02-07 22:15:30

+0

而且有没有办法避免它? WCF服务和winforms应用程序具有不同的位数,所以它不会以这种方式运行。 – AndrewR 2015-02-07 22:21:46