2012-10-11 52 views
1

我有一個簡單的WCF服務如下所示。wsHttpBinding不能在自我託管的WCF服務中工作;基於SOAP的綁定可以工作

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    string GetData(int value); 
} 

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

服務器Web.config文件是

<?xml version="1.0"?> 
<configuration> 
<appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
</appSettings> 

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="webHttpEndpointBehavior"> 
       <webHttp faultExceptionEnabled="true" /> 
      </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
      <behavior name="serviceBehaviourDebug"> 
       <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
       <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
       <!-- To receive exception details in faults for debugging purposes, set the value below to true. 
        Set to false before deployment to avoid disclosing exception information --> 
       <serviceDebug includeExceptionDetailInFaults="true"/> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 

    <services> 
     <service name="Diws.Service1" behaviorConfiguration="serviceBehaviourDebug"> 
      <endpoint 
       address="/basicHttp" 
       binding="basicHttpBinding" 
       contract="Diws.IService1"/> 
      <endpoint 
       address="/webHttp" 
       binding="webHttpBinding" 
       behaviorConfiguration="webHttpEndpointBehavior" 
       contract="Diws.IService1"/> 
      <endpoint 
       address="/wsHttp" 
       binding="wsHttpBinding" 
       contract="Diws.IService1"/> 
      <endpoint 
       address="mex" 
       binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 

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

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
    To browse web app root directory during debugging, set the value below to true. 
    Set to false before deployment to avoid disclosing web app folder information. 
    --> 
    <directoryBrowse enabled="true"/> 
</system.webServer> 
</configuration> 

客戶端是一個控制檯應用程序,其App.config中是這樣的。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
</startup> 

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="webHttpEndpointBehavior"> 
       <webHttp /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IService1" /> 
     </basicHttpBinding> 
     <wsHttpBinding> 
      <binding name="WsHttpBinding_IService1" /> 
     </wsHttpBinding> 
     <webHttpBinding> 
      <binding name="WebHttpBinding_IService1" /> 
     </webHttpBinding> 
    </bindings> 

    <client> 
     <endpoint 
      address="http://localhost:50001/Service1.svc/basicHttp" 
      binding="basicHttpBinding" 
      bindingConfiguration="BasicHttpBinding_IService1" 
      contract="ServiceReference1.IService1" 
      name="BasicHttpEndpoint_IService1" /> 
     <endpoint 
      address="http://localhost:50001/Service1.svc/webHttp" 
      behaviorConfiguration="webHttpEndpointBehavior" 
      binding="webHttpBinding" 
      bindingConfiguration="WebHttpBinding_IService1" 
      contract="ServiceReference1.IService1" 
      name="WebHttpEndpoint_IService1" /> 
     <endpoint 
      address="http://localhost:50001/Service1.svc/wsHttp" 
      binding="wsHttpBinding" 
      bindingConfiguration="WsHttpBinding_IService1" 
      contract="ServiceReference1.IService1" 
      name="WsHttpEndpoint_IService1"/> 
    </client> 
</system.serviceModel> 

<system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
</system.web> 
</configuration> 

而客戶端程序是這樣的。

class Program 
{ 
    static void Main(String[] args) 
    { 
     String response = ""; 

     Service1Client basicHttpClient = new Service1Client("BasicHttpEndpoint_IService1"); 
     response = basicHttpClient.GetData(10); 
     basicHttpClient.Close(); 
     Console.WriteLine(response); 

     ///* Some communication exception 
     Service1Client webHttpClient = new Service1Client("WebHttpEndpoint_IService1"); 
     response = webHttpClient.GetData(20); 
     webHttpClient.Close(); 
     Console.WriteLine(response); 
     //*/ 

     Service1Client wsHttpClient = new Service1Client("WsHttpEndpoint_IService1"); 
     response = wsHttpClient.GetData(30); 
     wsHttpClient.Close(); 
     Console.WriteLine(response); 

     Console.WriteLine(); 
     Console.WriteLine("Done"); 
     Console.ReadLine(); 
    } 
} 

basicHttpClient和wsHttpClient完美地工作。然而,webHttpClient拋出異常「System.ServiceModel.CommunicationException是未處理,HResult的= -2146233087,消息=內部服務器錯誤」

我不能在服務器側的Visual Studio 2012調試說 「無法自動調試' MyProject'。遠程過程無法調試,這通常表明服務器上的調試功能尚未啓用。「

但是,調試已啓用。在開啓診斷功能後,我無法獲得使用SvcTraceViewer的任何見解。

我的主要興趣是找出爲什麼使用WebHttpBinding的REST調用失敗,但幫助獲得服務器端調試工作,也將不勝感激。我正在使用多個啓動項目在VS2012中調試客戶端和服務器。 Localhost是唯一涉及的服務器。

我知道REST端點不會顯示在WcfTestClient中,因爲它不提供元數據交換,但我希望能夠通過該端點調用服務,並且我看到我的代碼和調用RESTful WCF服務。

+0

你可以發佈你的服務合約的樣子嗎? – Rajesh

回答

0

要訪問REST端點,請嘗試使用瀏覽器或HttpClient發送HTTP POST請求到URL:$ http:// localhost:50001/Service1.svc/webHttp/GetData $。當您在代碼中使用webHttpClient來調用服務時,您將發送REST端點無法處理的SOAP請求。我相信這就是您的其他兩個端點正常工作的原因,但不是這個。

+0

我已更改地址,如下所示。 「主機已經開始 終點 地址:http://本地主機:50000/basicHttp 綁定:System.ServiceModel.BasicHttpBinding 合同:Diws.IService1 地址:http://本地主機:50000/wsHttp 綁定: System.ServiceModel.WSHttpBinding 合同:Diws.IService1 地址:http://本地主機:50000/webHttp 綁定:System.ServiceModel.WebHttpBinding 合同:Diws.IService1 地址:http://本地主機:50000/mex 綁定:System.ServiceModel.WSHttpBinding 合同:Syste m.ServiceModel.Description.IMetadataExchange 按[ENTER]結束。' – Dave

+0

還向服務類添加'[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]'。當使用URL'http:// localhost:50000/webHttp/GetData?value = 40'從Web瀏覽器調用服務時,我在[EndpointDispatcher異常]處得到[ContractFilter不匹配](http:// stackoverflow。COM /問題/ 5487791/WCF的contractfilter不匹配,在最endpointdispatcher-除外)。 – Dave

+0

我認爲,只要客戶端的配置與服務器的配置相匹配,Visual Studio生成的客戶端服務代理就能夠處理基於SOAP和基於REST的端點。這是不是真的?客戶端代理只處理基於SOAP的端點,我應該使用類似[WebClient類](http://msdn.microsoft.com/zh-cn/library/system.net.webclient%28v=vs.80 %29.aspx)訪問通過wsHttpBinding端點公開的基於REST的服務? – Dave

相關問題