2015-02-07 148 views
1

我有一個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

回答

1

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

相關問題