2012-06-22 46 views
0

我正在尋找從W /託管它的同一個.exe連接到WCF服務。我的主機服務在WPF應用程序爲:的當通過WPF自主主機連接時,通過net.tcp服務WCF超時

<service name="Namespace.Service" > 
    <endpoint address="Contract/tcp" 
       binding="netTcpBinding" 
       contract="Namespace.IContract"/> 
    <host> 
     <baseAddresses> 
     <add baseAddress="net.tcp://localhost:9002"/> 
     </baseAddresses> 
    </host> 
    </service> 

 ServiceHost svc = new ServiceHost(typeof("Namespace.Service")); 
     svc.Open(); 

和配置文件這讓我火了一個單獨的VS實例,創建一個控制檯應用程序,並執行成功如下:

 IChannelFactory<IContract> facContract = new ChannelFactory<IContract>(new NetTcpBinding()); 
     IContract contract = facContract.CreateChannel(new EndpointAddress("net.tcp://localhost:9002/Contract/tcp")); 

     string x = contract.GetProperty; //returns value I would expect 

但是如果我修改我原來的WPF的ServiceHost代碼以下,當我訪問服務(注超時異常被拋出:如果我做同樣的事情在控制檯表觀s ervice主人,我沒有得到超時...):

 ServiceHost svc = new ServiceHost(typeof("Namespace.Service")); 
     svc.Open(); 

     IChannelFactory<IContract> facContract = new ChannelFactory<IContract>(new NetTcpBinding()); 
     IContract contract = facContract.CreateChannel(new EndpointAddress("net.tcp://localhost:9002/Contract/tcp")); 

     string x = contract.GetProperty; //<-!!WCF Timeout exception thrown.. 

WCF跟蹤不提供用戶互動,增加細節(只是證實了超時異常被拋出)。有什麼想法嗎? {這不是關於WCF異常管理最佳實踐的問題;我正在尋找訪問服務主機&上午由這個奇怪的超時異常}

編輯: 當服務主機是一個WPF應用程序(也許是其他人);但控制檯應用程序作爲服務主機的行爲與我預期的相同(例如,可以訪問自託管服務)。我已經更新了標題和標籤,以反映這個新的信息...提前

感謝, 牛逼

回答

1

這是因爲WPF的ServiceHost是在同一個線程消費服務託管服務。問題在這裏定義:http://social.msdn.microsoft.com/Forums/en/wcf/thread/74bc9d15-c458-4f1f-81a0-ebded46b68c4。分辨率在這裏詳細描述:http://msdn.microsoft.com/en-us/library/ms752260.aspx

分辨率是啓動服務主機上的一個單獨的線程ALA 端點

private void StartHostThread() 
    { 
     // Before opening host, add endpoints... 
     host.AddServiceEndpoint(typeof(IContract)), 
           new NetTcpBinding(), 
           "Contract/tcp"); //Assumes base address of net.tcp://localhost:9002/ 

     //For hosting on its own thread, be sure to mark service attribute 
     // as UseSyncContext == false 
     ServiceBehaviorAttribute behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>(); 
     behavior.UseSynchronizationContext = false; 

     host.Open(); 
    } 

一旦主機打開上線,則可以消耗在主UI線程ALA服務:

 EndpointAddress epoint = new EndpointAddress("net.tcp://localhost:9002/Contract/tcp"); 
     IContract proxy = ChannelFactory<IContract>.CreateChannel(new NetTcpBinding(), epoint); 
     string xyz; 
     using (proxy as IDisposable) 
     { 
      xyz = proxy.GetProperty; 
     } 
0

我不知道你contract.GepProperty做什麼(但它不應該在一個屬性第1名)

可正常工作

---- ---- HOST

static void Main(string[] args) 
     { 
      var svc = new ServiceHost(typeof(Service1)); 
      svc.Open(); 

      IChannelFactory<IService1> facContract = new ChannelFactory<IService1>(new NetTcpBinding()); 
      var contract = facContract.CreateChannel(new EndpointAddress("net.tcp://localhost:9002/Contract/tcp")); 

      var x = contract.DoWork(); 
     } 

---服務---

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    string DoWork(); 
} 

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. 
public class Service1 : IService1 
{ 
    public string DoWork() 
    { 
     return "Hello Work."; 
    } 
} 

--- ---配置

<configuration> 
    <system.serviceModel> 
     <services> 
      <service name="HostAndClient.Service1"> 
       <endpoint address="Contract/tcp" binding="netTcpBinding" contract="HostAndClient.IService1"> 
        <identity> 
         <dns value="localhost" /> 
        </identity> 
       </endpoint> 
       <host> 
        <baseAddresses> 
         <add baseAddress="net.tcp://localhost:9002" /> 
        </baseAddresses> 
       </host> 
      </service> 
     </services> 
    </system.serviceModel> 
</configuration> 
+0

嗯......奇怪,爲什麼它不適合我然後。我確實添加了您的安全節點;沒有任何運氣。 FWIW GetProperty只是一個屬性獲取器(例如.get_GetProperty()),所以不要認爲它是任何東西。 – TOB

+0

好吧 - 看起來像這樣可以與控制檯應用程序一起作爲服務主機使用;但不適用於WPF應用程序作爲服務主機(也許是其他人)。我會更新我的原始帖子,以便更具體。 – TOB

相關問題