2011-12-05 60 views
1

我有一個簡單的Web服務。這是在使用VS 2010的網站中使用的。我在VS 2010中使用「添加服務引用」選項添加了服務引用。它工作正常。它打印的服務地址爲http://localhost:3187/Service1.svc/MyFolder。但是,當我在瀏覽器中輸入此服務地址時,它顯示HTTP錯誤400.VS 2010中的WCF端點地址

注意:當我在服務的終點將address =「MyFolder」替換爲address =「」時,http://localhost:3187/Service1.svc顯示結果。

什麼是正確的地址,我應該在瀏覽器中輸入「MyFolder」的地址獲得服務?

頁:

namespace ClientWebApp 
{ 
    public partial class Default : System.Web.UI.Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Service1Client myClientService = new Service1Client(); 
     Response.Write(myClientService.Endpoint.Address); 

     string result = myClientService.GetData(7); 
     lblName.Text = result; 
    } 
    } 
} 

合同:

namespace MyWCFServiceApplication 
{ 
    [ServiceContract] 
    public interface IService1 
    { 
    [OperationContract] 
    string GetData(int value); 
    } 

    public class MyService : IService1 
    { 
    public string GetData(int value) 
    { 
     return string.Format("Now entered: {0}", value); 
    } 
    } 
} 

配置:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.serviceModel> 

    <services> 
     <service name="MyWCFServiceApplication.MyService" 
       behaviorConfiguration="WeatherServiceBehavior"> 

     <endpoint address="MyFolder" 
        binding="wsHttpBinding" 
        contract="MyWCFServiceApplication.IService1" /> 

     <endpoint address="mex" 
        binding="mexHttpBinding" 
        contract="IMetadataExchange" /> 
     </service> 
    </services> 

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

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

    </system.serviceModel> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

回答

1

請大家看看這個問題的答案:WCF Endpoints & Binding Configuration Issues

報價:

當託管在IIS中的WCF服務,使用以下格式形成服務的基地址: {協議}:// {主機}:{端口}/{的applicationName}/{svcFileName}。這是您可以瀏覽以獲取WCF幫助頁面和/或 元數據(使用默認配置)的地址的 地址。

爲了形成端點(所述一個你的客戶端需要 使用),則使用以下格式的實際地址: {serviceBaseAddress}/{的EndpointAddress}

在你的情況的{endpointAddress}MyFolder這解釋了爲什麼您可以使用http://localhost:3187/Service1.svc/MyFolder地址添加服務參考。然而,這並不是您的幫助頁面和元數據信息得到呈現的地址,因此您在http://.../*.svc/MyFolder上獲得HTTP Error 400的事實並不意外。

+0

「但是,這不是您的幫助頁面和元數據信息呈現的地址。」你能告訴我它會在哪裏嗎? – Lijo

+1

'{protocol}:// {host}:{port}/{applicationName}/{svcFileName}'=>所以在你的情況下'http:// localhost:3187/Service1.svc'。要查看合同,請轉到'http:// localhost:3187/Service1.svc?wsdl'。 – nowaq

0

嘗試添加 「WSDL?」 你與嘗試網址的結尾。

+0

它沒有工作。相同的結果 – Lijo