2011-08-25 38 views
3

我正在嘗試使用WCFTestClient來測試自己託管的wcf服務。我得到一個錯誤,像這樣:自我託管的WCF服務不能通過WCFTestClient測試

Error: Cannot obtain Metadata from http://localhost:2303/MyService If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:2303/MyService Metadata contains a reference that cannot be resolved: 'http://localhost:2303/MyService'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:2303/MyService . The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..HTTP GET Error URI: http://localhost:2303/MyService There was an error downloading 'http://localhost:2303/MyService'. The request failed with HTTP status 400: Bad Request.

我的項目結構如下

    充當主機
  1. 控制檯應用程序
  2. 服務合同
  3. 服務實現

這裏有我的服務實施和合同類,它們分屬於兩個獨立的項目。

namespace MyService 
{ 
    public class MyService : IMyService 
    { 
     public string GetGreeting(string name) 
     { 
      return "Hello " + name; 
     } 

     public string GetYelling(string name) 
     { 
      return "What the hell " + name + "!!"; 
     } 
    } 
} 

namespace MyService 
{ 
    [ServiceContract] 
    public interface IMyService 
    { 
     [OperationContract] 
     string GetGreeting(string name); 

     [OperationContract] 
     string GetYelling(string name); 
    } 
} 

這是控制檯應用程序

namespace MyWCFHost 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      ServiceHost serviceHost = new ServiceHost(typeof(MyService.MyService), new Uri("http://localhost:2303")); 
      serviceHost.Open(); 

      Console.WriteLine("MyService is running..."); 
      Console.ReadKey(); 
      serviceHost.Close(); 
     } 
    } 
} 

這是配置文件

<configuration> 

    <system.serviceModel> 
    <services> 
     <service name ="MyService.MyService" behaviorConfiguration="MyService.MyServiceBehavior"> 
     <endpoint address="http://localhost:2303/MyService" binding="basicHttpBinding" contract="MyService.IMyService"/> 
     <endpoint address="mex" binding="mexHttpBinding" name="mexpoint" contract="IMetadataExchange" /> 
     </service> 

    </services> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior name="MyService.MyServiceBehavior"> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    </system.serviceModel> 


</configuration> 

我在做什麼錯?

感謝您的時間...

編輯

的服務工作,當我嘗試通過一個WinForms客戶端來運行它,所以我知道該服務工作。問題是如何使用WcfTestClient將其準備好進行測試。

+1

offtopic但你有類相同的命名空間,困惑下來,我同意行 – V4Vendetta

+0

完美元素的名稱,但是這是一個研究項目,所以我順其自然。 – user20358

回答

3

我懷疑你的MEX端點有問題。您目前只指定一個相對地址(「MEX」) - 但沒有基址爲您服務定義HTTP ......

我會建議:

  • 要麼定義基地地址,然後「上面」的僅使用相對地址 - 您經常和你的MEX終結

OR:

  • 定義地址爲充分,完整的地址 - 不僅適用於您的常規端點,而且也適用於MEX端點。

因此改變你的配置是這樣的:

<service name ="MyService.MyService" behaviorConfiguration="MyService.MyServiceBehavior"> 
    <endpoint 
     address="http://localhost:2303/MyService" 
     binding="basicHttpBinding" 
     contract="MyService.IMyService"/> 
    <endpoint name="mexpoint" 
     address="http://localhost:2303/MyService/mex" 
     binding="mexHttpBinding" 
     contract="IMetadataExchange" /> 
    </service> 

,然後我希望你應該能得到你的元數據,從而連接到您服務!

+0

真棒!那工作。謝謝馬克。但是,現在它在WcfTestClient中工作,我試着檢查當我在瀏覽器窗口中點擊URL http:// localhost:2303/MyService時是否可以看到某些內容。我收到一個HTTP 400錯誤的請求。任何指針在這裏? – user20358

+0

另外,對於cassini託管的服務,我們不需要指定基地址。該配置是我從卡西尼託管的wcf服務中提取出來的。卡西尼是否在封面上添加了什麼? – user20358

+1

@ user20358:這是一個** SOAP **服務 - 您無法從瀏覽器「點擊」SOAP服務 - 您的瀏覽器不會與SOAP進行通信。您應該可以通過轉到http:// localhost:2303/MyService/mex?WSDL' –

1

您是否正在運行Windows 7?

嘗試運行:

netsh http add urlacl url=http://+:2303/MyService user=DOMAIN\user 

更多info這裏too

+0

是的,我使用Win 7.另外,我爲DOMAIN \ user放置了什麼..我在沒有連接到任何域的家用PC上運行這個。用戶是否需要成爲登錄用戶?..我想它確實......是嗎? – user20358

+0

當我嘗試通過winforms客戶端運行它時,該服務起作用,所以我知道該服務正在運行。問題是如何使用WcfTestClient將其準備好進行測試。 – user20358

+0

命令netsh http add urlacl url = http:// +:2303/MyService user = PCNAME \ mylogin給了我這個:網址保留添加失敗,錯誤:87 – user20358

0

嘗試運行你的視覺工作室擔任管理員。然後你不需要手動運行命令(url = http:// +:2303/MyService user = PCNAME \ mylogin)

+0

我正在運行它作爲管理員。 – user20358