2010-03-04 94 views
1

我有一個WCF服務,需要由第三方應用程序調用,發佈一些原始XML。嘗試使用WebRequest調用WCF服務

我想通過構建一個簡單的WebRequest並向服務發出請求來測試我的服務。

這裏是我的服務代碼:

接口:

[ServiceContract(Namespace = "http://test.mydomain.com")] 
public interface ITest 
{ 
    [WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method="POST")] 
    [OperationContract] 
    Stream SaveXML(Stream input); 
} 

服務:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(Namespace = "http://test.mydomain.com")] 
public class Test : ITest 
{ 
    public Stream SaveXML(Stream input) 
    { 
     StreamReader streamReader = new StreamReader(input); 
     string rawString = streamReader.ReadToEnd(); 
     streamReader.Dispose(); 

     // here need to save the input stream to xml format file 
     Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); 
     WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
     byte[] returnBytes = encoding.GetBytes(rawString); 
     return new MemoryStream(returnBytes); 
    } 
} 

配置:

<services> 
    <service behaviorConfiguration="Blah.TestBehavior" name="Blah.Test"> 
    <endpoint address="http://localhost:51494/Blah/Test.svc" binding="basicHttpBinding" contract="Blah.ITest"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

錯誤的客戶端代碼:

  string postData = "<Message version=\"1.5\" xmlns=\"http://test.mydomain.com\" ><books>Blah</books></Message>"; 
     WebRequest request = WebRequest.Create("http://localhost:51494/Blah/Test.svc/SaveXML"); 
     request.Method = "POST"; 

     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
     request.ContentType = "application/x-www-form-urlencoded"; 
     //request.ContentType = "text/xml; charset=utf-8"; 
     //request.ContentType = "text/xml;"; 
     //request.ContentType = "application/xml;"; 
     request.ContentLength = byteArray.Length; 

     Stream dataStream = request.GetRequestStream(); 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     dataStream.Close(); 

     // Get the response. 
     WebResponse response = request.GetResponse(); 

在最後一行我得到一個400(壞請求)或415(不支持的媒體類型)錯誤,這取決於我的contentType指定。另外,如果我在我的客戶端應用程序中添加服務引用,並使用API​​調用服務,那麼它可以正常工作。任何見解都將不勝感激,因爲我是WCF的新手,並且完全陷入困境。

+0

我會做什麼:使用Fiddler http://fiddler2.com/fiddler2/來查看「API」的請求 - 一個工程。然後將它與不起作用的請求進行比較。這應該縮小它的範圍。 – Cheeso 2010-03-04 20:49:17

+0

您的'StreamReader'需要位於'using'塊中,就像其他幾個對象一樣。即使拋出異常,'使用'塊也會導致它們被丟棄。 – 2010-03-04 21:23:41

回答

7

我覺得你在這裏混淆了兩個不同的東西:

  1. WebRequestPOST[WebInvoke]屬性建議你試圖做類似REST
  2. 但是你的服務配置有basicHttpBinding - 這不會WebRequest

飛SOAP協議,這樣 - 讓你的心!

你想使用SOAP嗎?然後你基本上可以正常使用basicHttpBinding,但是你不能像使用POST的WebRequest那樣訪問SOAP服務 - 你需要在命令行中使用由Visual Studio或svcutil.exe生成的SOAP客戶端。

你想使用WebRequest和簡單的POST請求?然後,您需要創建基於REST的WCF服務 - 使用webHttpBindingWebServiceHost(而不是簡單的ServiceHost)。

對於基於SOAP的WCF服務,請查看WCF Developer Center on MSDN

對於基於REST的WCF服務(您可以在瀏覽器中導航到您可以從WebRequest調用的服務),請查看WCF REST Developer Center on MSDN並查看WCF中基於REST的優秀screencast series by Pluralsight - 最值得注意的:

+0

WCF開發人員中心似乎已停用 - 任何更新的鏈接都可用? – Default 2017-08-08 10:03:23

+0

..而且不幸的是RESTful視頻。 – Default 2017-08-08 10:35:12

+0

@Default:對不起,這些鏈接沒問題 - 在** 2010 **的時候我回答了 - 但你說得對,他們似乎已經死了。對不起,我沒有任何替代鏈接.....試着問Google或Bing,也許? – 2017-08-08 11:04:24

1

basicHttpBinding的也適合!

我花了差不多一天搞清楚什麼是錯用的WebRequest與basicHttpBinding的WCF服務,它原來我錯過了小而關鍵的東西:的SOAPAction頭已被設置!

newRequest.Headers["SOAPAction"] = "http://tempuri.org/INotificationService/MyMethodName" 
+0

你在「WebRequest」的主體中提供了什麼。它是否包含SOAPHeaders? – Abhijeet 2013-08-21 17:02:26

+0

@autrevo,你的意思是請求流?您可以安裝http代理(如Fiddler)並檢查WCF服務調用中發送的內容。您將看到請求正文和標題。我不得不在我的任務中提出請求,所以我不會自己構造請求主體。 – ADOConnection 2013-08-22 12:46:37