2013-06-23 66 views
0

我有自定義綁定作爲端點的WCF Web服務。我想從我的客戶端應用程序調用此Web服務(託管在IIS上)。如何在具有CustomBinding端點的客戶端中調用Web服務?

服務合同看起來如下:

[ServiceContract(Namespace = "http://schemas.microsoft.com/windows/management/2012/01/enrollment")] 
    [XmlSerializerFormat] 
    public interface IDiscoveryService 
    {  
     [OperationContract(Name = "Get")] 
     [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml)] 
     string DiscoveryGet(); 
    } 

Web.config文件中的內容是這樣的:

<system.serviceModel> 
    <bindings> 
     <customBinding> 
     <binding name="NewBinding0"> 
      <textMessageEncoding /> 
      <httpTransport /> 
     </binding> 
     </customBinding> 
    </bindings> 
    <services> 
     <service name="DiscoveryWebService.DiscoveryService"> 
     <endpoint address="" binding="customBinding" bindingConfiguration="NewBinding0" 
      contract="DiscoveryWebService.IDiscoveryService" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false before deployment --> 
      <serviceMetadata httpGetEnabled="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="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

客戶端應用程序代碼的樣子:

string uri = " http://localhost/EnrollmentServer/Discovery.svc"; 

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); 
req.ContentType = "unknown"; 
req.Method = "GET"; 
WebResponse response = req.GetResponse(); 

StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), false); 

string responseString = loResponseStream.ReadToEnd(); 

我正在獲取WSDL文件的HTML內容,而不是Get方法返回的字符串。我不知道我是否以正確的方式來做這件事?

我希望在這方面的幫助。

+0

我已經按照「carlosfigueira」提供的以下答案進行了嘗試。看起來有點不對,這些更改甚至連我的Web服務也不能與WCF Test客戶端一起工作。 –

回答

1

[WebGet](和[WebInvoke])屬性僅適用於使用綁定元素的端點;使用HTTP傳輸,manualAddressing屬性設置爲true,還有<webHttp/>終結點​​行爲 - 您的服務不具備此功能。如果你把下面列出的變化,它應該工作:

合同

服務

[ServiceContract(Namespace = "http://schemas.microsoft.com/windows/management/2012/01/enrollment")] 
[XmlSerializerFormat] 
public interface IDiscoveryService 
{  
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] 
    string DiscoveryGet(); 
} 

的Web.config

<system.serviceModel> 
    <bindings> 
     <customBinding> 
     <binding name="NewBinding0"> 
      <webMessageEncoding /> 
      <httpTransport manualAddressing="true" /> 
     </binding> 
     </customBinding> 
    </bindings> 
    <services> 
     <service name="DiscoveryWebService.DiscoveryService"> 
     <endpoint address="" binding="customBinding" bindingConfiguration="NewBinding0" 
      contract="DiscoveryWebService.IDiscoveryService" behaviorConfiguration="Web" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="Web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

客戶端代碼

string uri = "http://localhost/EnrollmentServer/Discovery.svc/DiscoveryGet"; 

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); 
req.Method = "GET"; 
WebResponse response = req.GetResponse(); 

StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), false); 

string responseString = loResponseStream.ReadToEnd(); 
+0

在這種情況下,我如何使用HTTP GET和POST來發送和接收soap xml?請你迴應這個查詢嗎? http://stackoverflow.com/questions/17303466/wcf-how-to-choose-right-binding-and-service-contract-when-the-request-and-respo –

+0

當我閱讀了一些博客,它傳達Soap 1.2支持HTTP GET和POST。你能建議我如何使用soap 1.2服務而不是web http綁定來實現這一點嗎? –