[KB]Silverlight搭配WCF,遇到The remote server returned an error: NotFound的錯誤.

[KB]Silverlight搭配WCF,遇到The remote server returned an error: NotFound的錯誤.

最近處理了一個Silverlight搭配WCF的Case,Scenario是這樣地:

User在本機上開發Silverlight搭配WCF的專案,在Visual Studio上執行都很正常,但是佈署到Server上時,就會有問題。

WCF裡使用一個很簡單的函式去回傳一個Hello World字串給Silverlight。 使用者提供了他佈署的網站供我們測試。

 

1. 當連上網頁時,就看到瀏覽器的左下角報出這樣的錯:

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; InfoPath.2; MS-RTC LM 8; .NET CLR 3.5.30729; .NET CLR 3.0.30729)

Timestamp: Wed, 21 Oct 2009 03:42:55 UTC

Message: Unhandled Error in Silverlight Application

程式碼: 4004   

分類: ManagedRuntimeError      

訊息: System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid.  Check InnerException for exception details. ---> System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.

   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)

   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)

   at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)

   --- End of inner exception stack trace ---

   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)

   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)

   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)

   --- End of inner exception stack trace ---

   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)

   at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)

   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)

   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)

   at SilverlightApplication4.CustomerService.SampleServiceClient.SampleServiceClientChannel.EndDoWork(IAsyncResult result)

   at SilverlightApplication4.CustomerService.SampleServiceClient.SilverlightApplication4.CustomerService.SampleService.EndDoWork(IAsyncResult result)

   at SilverlightApplication4.CustomerService.SampleServiceClient.OnEndDoWork(IAsyncResult result)

   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

   --- End of inner exception stack trace ---

   at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()

   at SilverlightApplication4.CustomerService.DoWorkCompletedEventArgs.get_Result()

   at SilverlightApplication4.MainPage.client_DoWorkCompleted(Object sender, DoWorkCompletedEventArgs e)

   at SilverlightApplication4.CustomerService.SampleServiceClient.OnDoWorkCompleted(Object state)    

Line: 54

Char: 13

Code: 0

URI: http://xxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxx.html

 

2. 看到System.Net.WebException: The remote server returned an error: NotFound. 的錯誤,心中有了個底,看來,WCF這個服務不乖,沒有好好的運作阿.

 

3. 去檢視WCF服務的位置, 發現服務未正確執行. 抓到兇手了!

 

4. 後來找了一下資料,發現原來佈署後要在web.config加上<baseAddressPrefixFilters>的設定,設定佈署站台的基底位址:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
      <baseAddressPrefixFilters>
        <add prefix="http://www.helloworld.com/" />
      </baseAddressPrefixFilters>
   </serviceHostingEnvironment > 

 

這個tag的說明是這樣:

表示組態項目的集合,這個集合會指定通過篩選條件,而且這些篩選條件提供了一項機制,可在將 Windows Communication Foundation (WCF) 應用程式裝載於網際網路資訊服務 (IIS) 時挑選適當的 IIS 繫結。

 

 

簡單地說: 前置詞篩選條件為共用裝載提供者提供一種方式,使其可指定服務所要使用的 URI。

 

 

參考資訊:

1. <baseAddressPrefixFilters>

http://msdn.microsoft.com/zh-tw/library/bb924481.aspx

 

 

如果您有微軟技術開發的問題,可以到MSDN Forum發問。

如果您有微軟IT管理的問題,可以到TechNet Forum發問喔。