2013-02-21 32 views
0

在我的書中,它希望我使用兩個綁定公開兩個終結點:WsHttpBinding & NetTCPBinding並在主機應用程序中託管服務。使用兩個綁定時出現「無法下載元數據」

我用下面的代碼在C#中嘗試連接到我的服務:

Uri BaseAddress = new Uri("http://localhost:5640/SService.svc"); 

Host = new ServiceHost(typeof(SServiceClient), BaseAddress); 
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior(); 
Behaviour.HttpGetEnabled = true; 
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
Host.Description.Behaviors.Add(Behaviour); 
Host.Open(); 

在服務方面,我有:

[ServiceContract] 
public interface IService... 

public class SService : IService.... 

然後在我的配置文件我有:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.serviceModel> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="Behavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <services> 
     <service behaviorConfiguration="WsHttpBehaviour" name="SService.SService"> 

     <endpoint 
      address="" 
      binding="wsHttpBinding" 
      bindingConfiguration="WsHttpBindingConfig" 
      contract="SService.IService" /> 

     <endpoint 
      address="" 
      binding="netTcpBinding" 
      bindingConfiguration="NetTCPBindingConfig" 
      contract="SService.IService" /> 

     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:5640/SService.svc" /> 
      <add baseAddress="net.tcp://localhost:5641/SService.svc" /> 
      </baseAddresses> 
     </host> 

     </service> 
    </services> 

    </system.serviceModel> 
</configuration> 

但是,當我嘗試添加服務引用到我的主機應用程序,它說無法下載元數據地址。我不明白什麼是錯誤的,老師從來沒有教過這個。我決定繼續前進並提前學習。我使用編輯器來創建上面顯示的WebConfig。

任何人都可以指向正確的方向嗎?我通過編輯器添加了元數據行爲,並將HttpGetEnabled設置爲true。

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-02-21 18:57:19

回答

1

我能找到你的代碼的一些問題,可能會導致此問題:

。請通過typeof(SService.SService)而不是typeof(SServiceClient)。像這樣改變:

Host = new ServiceHost(typeof(SService.SService)) 

<service behaviorConfiguration="WsHttpBehaviour"。我想這應該是「行爲」,因爲你已經定義了。由於您在config中啓用了元數據,因此您可以刪除將ServiceMetadataBehavior添加到servicehost的代碼行。

這裏是你可以借鑑的例子:

<system.serviceModel> 
    <services> 
     <service name="ConsoleApplication1.Service"> 
     <endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" /> 
     <endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://<machinename>:5640/SService.svc" /> 
      <add baseAddress="net.tcp://<machinename>:5641/SService.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="False"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      ServiceHost host = new ServiceHost(typeof(Service)); 
      host.Open(); 

      Console.ReadLine(); 
     } 
    } 

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

    public class Service : IService 
    { 
     public string DoWork() 
     { 
      return "Hello world"; 
     } 
    } 
} 

元數據將被自動提供在配置中定義的HTTP baseaddress。

+0

我得到:「無法找到與綁定NetTcpBinding的端點匹配scheme net.tcp的基地址,註冊的基地址方案爲[http]。」當我做你的改變。 – Brandon 2013-02-21 04:44:26

+0

我可以看到你有一個在配置中定義的net.tcp基地址。所以不需要在ServiceHost()中傳遞baseAddress。你嘗試刪除它嗎? – Praburaj 2013-02-21 05:00:15

+0

是的,我試過。我仍然無法工作。 – Brandon 2013-02-21 14:51:42

相關問題