2011-11-23 208 views
1

我試着在Windows窗體應用程序中運行WCF服務。我複製並修改了Microsoft提供的WCF示例中的代碼。運行WCF Sample時,服務顯示在我使用的端口監視器(CurrPorts)中。當我運行我的代碼我看不到我的服務......Windows窗體應用程序中的WCF

這是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace NoName.Server 
{ 
    [ServiceContract(Namespace="http://NoName")] 
    public interface IApplicationService 
    { 
     [OperationContract()] 
     NoName.Entities.MediaParameter[] GetParametersForMediaObject(string mediaObjectId); 
     [OperationContract()] 
     NoName.Entities.MediaParameter GetMediaParameter(string parameterId); 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace NoName.Server 
{ 
    public class ApplicationService : IApplicationService 
    { 
     public Entities.MediaParameter[] GetParametersForMediaObject(string mediaObjectId) 
     { 
      throw new NotImplementedException(); 
     } 

     public Entities.MediaParameter GetMediaParameter(string parameterId) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

我從窗體運行它作爲

private void toolStripButton1_Click(object sender, EventArgs e) 
{ 
    using (ServiceHost host = new ServiceHost(typeof(ApplicationService))) 
    { 
     host.Open(); 
    } 
} 

而且配置在app.config中:

<system.serviceModel> 
    <services> 
     <service name="NoName.Server.ApplicationService" behaviorConfiguration="ApplicationServiceBehavior"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8000/NoName/ApplicationService"/> 
      </baseAddresses> 
     </host> 
     <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/NoName/ApplicationService --> 
     <endpoint address="" binding="wsHttpBinding" contract="NoName.Server.IApplicationService"/> 
     <!-- the mex endpoint is exposed at soap.tcp://localhost:8000/NoName/ApplicationService/mex --> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true--> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ApplicationServiceBehavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

它編譯沒有錯誤,當我運行它時沒有例外。它不會存在。想法?

+1

你是什麼意思「它不會存在」?你看它在端口監視器佔用一個端口,所以它確實存在。 – kroonwijk

回答

2

嗯 - 你使用的是using塊,這通常是一個好東西 - 但在這裏,服務主機將再次就在using塊結束時關閉 - 這當然不是你想要什麼!

using (ServiceHost host = new ServiceHost(typeof(ApplicationService))) 
{ 
    host.Open(); // host is opened here 
}     // host is disposed and closed here 

所以你ServiceHost開着,準備接收請求 - 要的只是第二一小部分......,然後將其關閉並設置自using { .. }塊結束了.....

你需要做的是這樣的:

  • 添加一個私人成員變量例如您的主要形式

    private ServiceHost _serviceHost = null; 
    
  • 在您的代碼的某個位置打開服務主機,例如在你的方法你有:

    private void toolStripButton1_Click(object sender, EventArgs e) 
    { 
        _serviceHost = new ServiceHost(typeof(ApplicationService)); 
    } 
    
  • 讓它開放,直到例如Winforms應用程序關閉(或用戶選擇其他菜單項以實際關閉服務主機)

+1

是的,當然(有少量embaressment ... :) :) 現在一切正常! – user1062729

+0

@ user1062729:有時,深入編程,你只是沒有看到樹木的森林了。像這樣的事情發生在任何人身上。 –