2013-05-15 73 views
0

我在連接WCF服務和Windows Phone時遇到問題。我有一個沒有源代碼的服務(只在網上發佈),我需要在Windows Phone 7應用程序中使用它。Windows Phone 7中的WCF服務

我添加項目的服務引用,VS產生的自述文件ServiceReferences.ClientConfig:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="HttpBinding_IWcfMobilServices"> 
      <security mode="None" /> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://www.www.com/WcfMobilServices.svc" 
      binding="basicHttpBinding" bindingConfiguration="HttpBinding_IWcfMobilServices" 
      contract="MobileService.IWcfMobilServices" name="HttpBinding_IWcfMobilServices"></endpoint> 
    </client> 
    </system.serviceModel> 
</configuration> 

當我調用服務的方法:在LoginCompleted

public static void Login() 
{ 
    var proxy = new MobileService.WcfMobilServicesClient(); 
    proxy.LoginAsync("loginName", "paswword"); 
    proxy.LoginCompleted += (s, a) => 
     { 
       //some code 
     }; 
} 

應用把我的異常上。結果:

System.ServiceModel.ProtocolException: Content Type text/xml; charset=utf-8 was not supported by service http://www.www.com/WcfMobilServices.svc . The client and service bindings may be mismatched. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClasse.b_d(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b_0(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result) --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at W7App.MobileService.LoginCompletedEventArgs.get_Result()} System.ServiceModel.CommunicationException {System.ServiceModel.ProtocolException}

任何想法有什麼不對?

我試圖改變在配置文件中的wsHttpBinding綁定,但VS顯示我一個警告:

The element 'bindings' has invalid child element 'wsHttpBinding'. List of Possible elements expected: 'basicHttpBinding, CustomBinding'.

感謝

+0

如果繼續進行警告會發生什麼?這可能是一個可能的重複:http://stackoverflow.com/questions/8250251/wcf-content-type-text-xml-charset-utf-8-was-not-supported-by-service – cvraman

+0

如果我忽略警告在配置中,應用程序在創建wcf類的實例時拋出一個錯誤:服務參考配置中無法識別的元素'wsHttpBinding'。請注意,Silverlight中只有Windows Communication Foundation配置功能的一個子集可用。 – Davecz

+1

不幸的是Silverlight/WP7僅支持Basic HTTP。你知道你試圖連接的端點是否支持基本的HTTP綁定?它可能不是? – Belogix

回答

2

從我們的談話繼。 Silverlight/Windows Phone 7 僅限支持basicHttpBindingCustomBinding。通過創建控制檯應用程序,您可以使用wsHttpBinding進行連接。因此,您的選擇是:

  1. 要求將basicHttpBinding添加到終點。
  2. 創建另一個位於Windows Phone和其他WCF服務之間的WCF服務。這樣,您可以連接basicHttpBinding,然後中間WCF可以使用wsHttpBinding連接到其他服務。

很明顯,選項2會更慢,增加複雜性和麻煩的任何更新,但除非選項1是可能的,我不認爲你有很多其他選擇。