在WP7 App中引用WCF时清空配置文件

时间:2012-06-17 20:13:17

标签: wcf windows-phone-7 wcf-client

我正在尝试从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>

当我在Console Client中引用WCF服务时,客户端Config文件如下所示:

<?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)

引用应该在绑定后返回配置文件,我不知道出了什么问题,我没有在IIS上托管WCF,我只是在试用期间在后台运行WCF库的项目模拟器上的WP7应用程序。

3 个答案:

答案 0 :(得分:2)

Windows Phone 7的运行时间是silverlight,不支持WSHttpBinding。您必须将服务绑定更改为basicHttpBinding,这是silverlight项目支持的唯一绑定。 :(

KeyNotFoundException是由于服务的绑定列表中不存在默认绑定(BasicHttpBinding)(在您的服务中仅找到WSHttpBinding)。

如果您不能或者您不想触及原始WSHttpBinding配置的WCF服务,您仍然可以通过利用适配器设计模式来解决此问题。< / p>

当我开发使用WSHttpBinding WCF服务的Windows Phone应用程序时,我遇到了这个问题。最后,我必须实现一个适配器 WCF服务,该服务本身已BasicHttpBinding配置并使用原始WSHttpBinding服务。然后使Windows Phone应用程序使用适配器服务。

虽然这种适配器方法需要一些额外的工作,但它仍然是一个优雅的解决方案。

答案 1 :(得分:0)

我遇到了同样的问题。我做的是:
1.右键单击服务参考。
2.选择“配置服务参考...”
3.无需在出现的对话框上更改任何内容。
4.单击对话框中的“确定”。

这为我重新生成了“ServiceReferences.ClientConfig”文件。

希望这会有所帮助。

答案 2 :(得分:0)

要在vs2013上使用Windows Phone 8项目的WCF服务,我遇到了同样的问题(空配置客户端),我无法创建客户端服务引用的实例。 以下对我有用:

1-右键单击ServiceRefernce文件夹下的引用服务。 2-在菜单中选择配置服务参考以访问服务参考设置。 3-在程序集列表上方,选择“在指定的引用程序集中重用类型:”。 4-选中“ System.ServiceModel ”列表,然后按确定

我希望这会有所帮助。