2013-03-04 149 views
2

我正在尋找從我的應用程序中創建自我託管的WCF服務。我試圖按照Microsoft網站http://msdn.microsoft.com/en-us/library/ms731758(v=vs.100).aspx上的示例進行操作,但遇到了問題。自我託管的WCF服務

當程序運行時我可以去到的網頁,它說,我可以運行svcutil.exe的生成客戶端類或當我進入WCF測試客戶端,該教程說我得到的錯誤

Service metadata may not be accessible. Make sure your service is running and exposing metadata. 

從svcutil.exe的我得到以下錯誤:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>svcutil http://localhost: 
6525/hello 
Microsoft (R) Service Model Metadata Tool 
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1] 
Copyright (c) Microsoft Corporation. All rights reserved. 

Attempting to download metadata from 'http://localhost:6525/hello' using WS-Meta 
data Exchange or DISCO. 
Generating files... 
Warning: No code was generated. 
If you were trying to generate a client, this could be because the metadata docu 
ments did not contain any valid contracts or services 
or because all contracts/services were discovered to exist in /reference assembl 
ies. Verify that you passed all the metadata documents to the tool. 

Warning: If you would like to generate data contracts from schemas make sure to 
use the /dataContractOnly option. 

下面是我的WCF應用程序的代碼

public interface IHelloWorldService 
     { 
      [OperationContract] 
      string SayHello(string firstName, string lastName); 
     } 

     static void Main(string[] args) 
     { 
      Uri baseAddress = new Uri("http://localhost:6525/hello"); 

      using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress)) 
      { 
       ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
       smb.HttpGetEnabled = true; 
       smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
       host.Description.Behaviors.Add(smb); 

       host.Open(); 

       Console.WriteLine("The service is ready at: {0}", baseAddress); 
       Console.WriteLine("Press <Enter> to stop the service"); 
       Console.ReadLine(); 
       host.Close(); 

      } 
     } 

     public class HelloWorldService : IHelloWorldService 
     { 
      public string SayHello(string firstName, string lastName) 
      { 
       return string.Format("Hello {0} {1}", firstName, lastName); 
      } 
     } 

感謝您提供的任何幫助

回答

5

起初看起來很難弄清楚你的問題,但當我測試你的代碼時,我發現你的代碼中缺少某些東西。您忘記將您的合同暴露給客戶。您的界面缺少[ServiceContract]屬性。下面將解決你的問題,希望這有助於。

[ServiceContract] 
public interface IHelloWorldService 
{ 
    [OperationContract] 
    string SayHello(string firstName, string lastName); 
} 
+0

謝謝你是確切的問題。現在正常工作 – Boardy 2013-03-04 13:25:59

+0

問題是什麼? – Benjiman 2013-03-04 13:49:07

+1

@Benjiman我沒有在公共接口IHelloWorldService定義 – Boardy 2013-03-04 14:53:24

1

我剛剛有完全相同的問題。原來,在編輯app.config後,設計者停止了自動更新app.config文件。你能從app.config發佈標記嗎?

1

元數據不是這裏曝光,你需要添加新的端點爲此,需要啓用它在服務配置。請執行以下操作。

<service name="ConsoleApplication1.WCFService1" behaviorConfiguration="newBehaviour" > 
     <endpoint address="mex" binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 

     <host> 
      <baseAddresses> 
       <add baseAddress="http://localhost:80/WCFService1" /> 
      </baseAddresses> 
     </host> 
<service> 

,並在服務行爲

<serviceBehaviors> 
      <behavior name="newBehaviour"> 
      <serviceMetadata httpGetEnabled="true"/> 
      </behavior> 
</serviceBehaviors> 

我已經嘗試這樣做,所產生的代理沒有任何問題。請讓我知道任何疑慮。

+0

@Boardy,你試過這個嗎? – 2013-03-04 11:59:23