【问题标题】:How can a Silverlight application connect to an existing WCF service?Silverlight 应用程序如何连接到现有的 WCF 服务?
【发布时间】:2011-12-06 18:12:04
【问题描述】:

我有一个 WPF 客户端,它连接到一个非常简单的 ISS 应用程序上的 WCF 端点。我想将 Silverlight 应用程序连接到相同的 WCF 服务。我读过我必须启用 OData。这在4中还有必要吗?如果是这样,我该怎么做?我如何实际连接端点?我是否需要使用 RIA 服务来促进连接?我可以使用同一个 IIS 应用程序来提供两个端点吗?

提前感谢您的帮助。这个让我难过。

编辑:

以下是我的 WCF 服务和 Silverlight 客户端的配置。

WCF 服务器配置(混淆):

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding maxReceivedMessageSize="2147483647">
                <security mode="Transport">
                    <transport clientCredentialType="None"/>
                </security>
            </binding>
        </wsHttpBinding>
        <basicHttpBinding>
            <binding maxReceivedMessageSize="2147483647">
                <security mode="Transport">
                    <transport clientCredentialType="None"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service name="MyNamespace.Services.MyService">
            <endpoint contract="MyNamespace.ServiceContracts.IMyService"
                      address=""
                      binding="wsHttpBinding"
                      />
            <endpoint contract="MyNamespace.ServiceContracts.IMyService"
                      address="basic"
                      binding="basicHttpBinding"
                      />
            <endpoint contract="IMetadataExchange"
                      address="mex"
                      binding="mexHttpBinding"
                      />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="True" />
                <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Silverlight 客户端配置(混淆):

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="Binding.Secure" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
                <security mode="Transport" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>

        <endpoint name="MyServiceReferenceNamespace.MyService"
                  contract="MyServiceReferenceNamespace.IMyService"
                  address="https://www.mydomain.com/MyVirtualDirectory/MyContract.svc"
                  binding="basicHttpBinding"
                  bindingConfiguration="Binding.Secure"
                  />

</system.serviceModel>

(为了保护我的客户,我们更改了名称。)

【问题讨论】:

    标签: silverlight wcf wcf-ria-services


    【解决方案1】:

    您不需要启用 OData;只要现有服务中的端点使用与 Silverlight 兼容的绑定(例如 BasicHttpBinding),SL 应用程序就可以像 WPF 一样使用它。如果绑定不兼容,您可以向服务添加一个新的端点(是的,它可以在同一个 IIS 应用程序中),供 SL 应用程序使用。

    在您的 SL 项目中,您可以选择“添加服务引用”,这将在该项目中创建一个代理,该代理知道如何与 WCF 服务“对话”。

    【讨论】:

    • 当您添加服务引用时,将在您的 SL 应用程序中创建一个名为 ServiceReferences.ClientConfig 的文件,该文件类似于 app.config 在 WPF 应用程序中配置客户端端点所做的工作.
    • 我遇到了与跨域访问有关的安全异常(或者至少这表明问题可能存在)。我需要设置一些特定的安全性吗? XAP 由包含 WCF 服务的同一应用程序提供服务。
    • 异常表明它“可能是由于...跨域”,但不一定是这样。查看msdn.microsoft.com/en-us/library/dd470100(VS.95).aspx 的页面,其中列出了您可以采取的许多步骤来调试此类错误。
    • 不走运。我感觉它与我的配置有关。我将在上面添加我的端点配置。
    • 如果您使用 Fiddler 之类的工具查看请求,您会看到 404(未找到)还是其他错误?如果您看到其他内容(例如 400 或 500),则响应本身可能会有一些附加信息。如果没有,那么您可以尝试在服务中启用跟踪,它应该包含有关问题的更多信息。
    【解决方案2】:

    在这篇 MSDN 文章中可以找到最好的信息:Accessing Web Services in Silverlight

    Silverlight 是否托管在与您的 WCF 服务相同的 IIS 上?如果没有,您应该阅读:Making a Service Available Across Domains Boundaries

    【讨论】:

    • 这也很有帮助。是不是说必须特别启用 WCF 服务才能与 Silverlight 前端一起使用?我已经在我的服务应用程序的根目录上设置了一个跨域实例。我仍然遇到安全异常。
    • 是的,没错。您可以在托管 WCF 服务的 IIS 上添加 clientaccesspolicy.xml。
    猜你喜欢
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多