【问题标题】:Enable WCF calls in a Windows Phone Mango在 Windows Phone Mango 中启用 WCF 调用
【发布时间】:2012-03-24 19:08:49
【问题描述】:

我在托管 Windows 服务中开发了一个 WCF,基于此 tutorial

这就是我的界面的定义方式:

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 一起通信。

【问题讨论】:

  • 您是否尝试过添加服务参考?查看这篇文章:eliasmarkelis.wordpress.com/2011/02/27/windows-phone-7-with-wcf
  • 您是否在服务端添加了 CrossDomainPolicy 文件?
  • 在 Desktop Silverlight 中,您需要一个文件来指定您可以访问该服务。它被称为 clientaccesspolicy.xml 并且需要在域的根目录中。也许这种行为是继承到 Windows Phone 的。
  • @DmitriyReznik 我做到了,我可以实例化a服务,只是我无法访问公共功能。
  • @VitorCanova 我发现使用 Silverlight 的人也有同样的问题,我正在尝试看看我能解决这个问题。

标签: c# wcf visual-studio windows-phone-7 sdk


【解决方案1】:

我认为 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多