2012-06-17 46 views
1

我試圖從WP7應用程序中使用我自己的WCF服務。在WP7中引用WCF時的空配置文件應用

在WCF服務

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 

     <service name="mergedServices.MergedService"> 

     <endpoint address="" binding="wsHttpBinding" contract="mergedServices.RelationGeneratorServiceInterface"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 

     <endpoint address="" binding="wsHttpBinding" contract="mergedServices.keywordSearchServiceInterface"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 

     <endpoint address="" binding="wsHttpBinding" contract="mergedServices.QAServiceInterface"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 

     <endpoint address="" binding="wsHttpBinding" contract="mergedServices.CompareWithOnePredicateInterface"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 

     <endpoint address="" binding="wsHttpBinding" contract="mergedServices.ComparisonServiceInterface"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 


     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/Design_Time_Addresses/mergedServices/" /> 
      </baseAddresses> 
     </host> 
     </service> 

    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above 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> 
    </system.serviceModel> 

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 

當我在一個控制檯客戶參考WCF服務配置文件,客戶端配置文件看起來像這樣:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_RelationGeneratorServiceInterface" 
        closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" 
        sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" 
        hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" 
        maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" 
        useDefaultWebProxy="true" allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
       <binding name="WSHttpBinding_keywordSearchServiceInterface" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
       <binding name="WSHttpBinding_QAServiceInterface" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
       <binding name="WSHttpBinding_CompareWithOnePredicateInterface" 
        closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" 
        sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" 
        hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" 
        maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" 
        useDefaultWebProxy="true" allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
       <binding name="WSHttpBinding_ComparisonServiceInterface" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
        allowCookies="false"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
          algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:8732/Design_Time_Addresses/mergedServices/" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_RelationGeneratorServiceInterface" 
       contract="ServiceReference1.RelationGeneratorServiceInterface" 
       name="WSHttpBinding_RelationGeneratorServiceInterface"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
      <endpoint address="http://localhost:8732/Design_Time_Addresses/mergedServices/" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_keywordSearchServiceInterface" 
       contract="ServiceReference1.keywordSearchServiceInterface" 
       name="WSHttpBinding_keywordSearchServiceInterface"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
      <endpoint address="http://localhost:8732/Design_Time_Addresses/mergedServices/" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_QAServiceInterface" 
       contract="ServiceReference1.QAServiceInterface" name="WSHttpBinding_QAServiceInterface"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
      <endpoint address="http://localhost:8732/Design_Time_Addresses/mergedServices/" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_CompareWithOnePredicateInterface" 
       contract="ServiceReference1.CompareWithOnePredicateInterface" 
       name="WSHttpBinding_CompareWithOnePredicateInterface"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
      <endpoint address="http://localhost:8732/Design_Time_Addresses/mergedServices/" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ComparisonServiceInterface" 
       contract="ServiceReference1.ComparisonServiceInterface" name="WSHttpBinding_ComparisonServiceInterface"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

問題是,當我嘗試從WP7項目中使用WCF服務,當我參考服務時,這是配置文件中的內容:

<configuration /> 

,這就是它......... 當我啓動應用程序,將出現以下運行時錯誤:

System.Collections.Generic.KeyNotFoundException was unhandled 
    Message=KeyNotFoundException 
    StackTrace: 
     at System.ThrowHelper.ThrowKeyNotFoundException() 
     at System.Collections.Generic.Dictionary`2.get_Item(String key) 
     at System.ServiceModel.Configuration.ServiceModelSectionGroup.get_Client() 
     at System.ServiceModel.Description.ConfigLoader.LookupChannel(String configurationName, String contractName, Boolean wildcard) 
     at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) 
     at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName) 
     at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) 
     at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) 
     at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName) 
     at System.ServiceModel.EndpointTrait`1.CreateSimplexFactory() 
     at System.ServiceModel.EndpointTrait`1.CreateChannelFactory() 
     at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) 
     at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef() 
     at System.ServiceModel.ClientBase`1..ctor() 
     at MergedServicesConsumer.WeetitService.QAServiceInterfaceClient..ctor() 
     at MergedServicesConsumer.MainPage.button1_Click(Object sender, RoutedEventArgs e) 
     at System.Windows.Controls.Primitives.ButtonBase.OnClick() 
     at System.Windows.Controls.Button.OnClick() 
     at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 
     at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) 
     at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) 

的引用後綁定應該返回config文件,我不確定出了什麼問題,我沒有在IIS上託管WCF,我只是在模擬器上試用WP7應用程序時在後臺運行WCF庫的項目。

回答

0

我面臨同樣的問題。我所做的是:
1.右鍵單擊服務參考。
2.選擇「配置服務參考...」
3.無需在出現的對話框中更改任何內容。
4.單擊對話框上的「確定」。

這爲我重新生成了「ServiceReferences.ClientConfig」文件。

希望這會有所幫助。

2

windows phone 7的運行時間是silverlight,它不支持WSHttpBinding。您必須將服務綁定更改爲basicHttpBinding,這是silverlight項目支持的唯一綁定。 :(

KeyNotFoundException是由於默認綁定(BasicHttpBinding)在服務的綁定列表不存在(僅WSHttpBinding在你的服務中找到)。

如果您不能或不想碰原來WSHttpBinding配置WCF服務,您還可以通過採取適配器設計模式的優勢變通辦法。

我遇到了這個確切的問題,當我開發Windows消耗一個WSHttpBinding WCF服務電話應用程序。最後,我只好實施a dapter WCF服務本身是BasicHttpBinding配置和使用原始WSHttpBinding服務。然後讓Windows Phone應用程序使用適配器服務。

雖然這適配器方法需要一些額外的工作,它仍然是一個優雅的解決方法。

0

爲了從vs2013上的windows phone 8項目中使用WCF服務,我遇到了同樣的問題(空配置客戶端),並且我無法創建客戶端服務引用的實例。 以下方式適用於我:

1-右鍵單擊ServiceRefernce文件夾下的引用服務。 2-在菜單中選擇配置服務參考以訪問服務參考設置。 3-在裝配清單上方,選擇「指定的引用裝配中的重新使用類型:」。 4-檢查列表「System.ServiceModel」並按確定

我希望這會有所幫助。

相關問題