1

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

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

4

3 回答 3

2

windows phone 7 的运行时间是 silverlight,不支持WSHttpBinding. 您必须将服务绑定更改为basicHttpBindingSilverlight 项目支持的唯一绑定。:(

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

如果您不能或不想接触原始WSHttpBinding配置的 WCF 服务,您仍然可以通过利用适配器设计模式来解决它。

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

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

于 2013-02-17T07:33:36.983 回答
0

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

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

希望这会有所帮助。

于 2012-10-03T07:36:45.297 回答
0

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

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

我希望这将有所帮助。

于 2014-03-10T12:50:34.313 回答