在Windows Phone Mango中启用WCF调用

本文关键字:启用 WCF 调用 Mango Windows Phone | 更新日期: 2023-09-27 18:28:50

我在托管Windows服务中开发了一个WCF,基于本教程

我的界面就是这样定义的:

namespace HomeAutomationWindowsService
{
    [ServiceContract]
    public interface IHomeAutomation
    {
        [OperationContract]
        string connect();
        [OperationContract]
        Boolean sendAction(string address, string command);
    }
}

我的App.config文件是这样的:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="Unsecured">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="HomeAutomationWindowsService.HomeAutomationService"
               behaviorConfiguration="HomeAutomationServiceBehavior">
      <host>
          <baseAddresses>
              <add baseAddress="http://192.168.11.178:8000/service"/>
          </baseAddresses>
      </host>
        <!-- this endpoint is exposed at the base address provided by     host: http://localhost:8000/service  -->
        <endpoint address=""
                  contract="HomeAutomationWindowsService.IHomeAutomation"
                  binding="wsHttpBinding"
                  bindingConfiguration="Unsecured" />
        <!-- the mex endpoint is explosed at http://192.168.11.178:8000/    mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HomeAutomationServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

当我从控制台或WPF应用程序进行调用时,我可以看到公共方法,但当我使用Windows Phone时,我什么都看不到。

我应该做些什么来让我的WCF或Windows Phone一起通信。

在Windows Phone Mango中启用WCF调用

我认为WP7不支持WsHttpBinding。尝试使用BasicHttpBinding:

Web.config:

  <system.serviceModel>
    <services>
      <service name="MyProject.MyService" behaviorConfiguration="behavior">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="binding" contract="MyProject.MyService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="binding">
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

在将web服务添加到您的WP7项目(添加服务引用)后,ServiceReferences.ClientConfig应该是这样的(使用WsHttpBinding添加web服务会生成一个空文件):

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_DataService" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://localhost:44300/Services/DataService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_DataService"
                contract="DataService.DataService" name="BasicHttpBinding_DataService" />
        </client>
    </system.serviceModel>
</configuration>