Silverlight应用程序如何连接到现有WCF服务?

Silverlight应用程序如何连接到现有WCF服务?,silverlight,wcf,wcf-ria-services,Silverlight,Wcf,Wcf Ria Services,我有一个WPF客户端,它连接到一个非常简单的ISS应用程序上的WCF端点。我想将Silverlight应用程序连接到相同的WCF服务。我已经读到我必须启用OData。这在4中仍然是必要的吗?如果是,我该怎么做?如何实际连接端点?我是否需要使用RIA服务来促进连接?我可以使用相同的IIS应用程序来提供两个端点吗 提前感谢您的帮助。这件事把我难住了 编辑: 下面是我的WCF服务和Silverlight客户端的配置 WCF服务器配置混淆: <system.serviceModel>

我有一个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>
<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客户端配置混淆:

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

名称已更改以保护我的客户。

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


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

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


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

有关此MSDN文章的最佳信息:


Silverlight是否与您的WCF服务托管在相同的IIS上?如果没有,您应该阅读:

在这篇MSDN文章中可以找到最佳信息:


Silverlight是否与您的WCF服务托管在相同的IIS上?如果没有,则应阅读:

添加服务引用时,将在SL应用程序中创建一个名为servicerences.ClientConfig的文件,这类似于app.config在WPF应用程序中配置客户端端点时所做的操作。我发现了与跨域访问有关的安全异常,或者至少这表明问题可能存在。我需要设置一些特定的安全设置吗?XAP正由包含WCF服务的同一应用程序提供服务。异常状态表明可能是由于。。。跨域,但不一定是跨域的。请看上的页面,其中列出了调试此类错误的许多步骤。运气不好。我觉得这和我的配置有关。我将在上面添加我的端点配置。如果您使用Fiddler之类的工具查看请求,您是否看到404未找到或其他错误?如果您看到其他内容,例如400或500,则响应本身可能会有一些附加信息。如果没有,那么您可以尝试在服务上启用跟踪,它应该包含有关问题的更多信息。添加服务引用时,将在SL应用程序中创建一个名为ServiceReferences.ClientConfig的文件,这类似于app.config在WPF应用程序中配置客户端端点时所做的操作。我发现了与跨域访问有关的安全异常,或者至少这表明问题可能存在。我需要设置一些特定的安全设置吗?XAP正由包含WCF服务的同一应用程序提供服务。异常状态表明可能是由于。。。跨域,但不一定是跨域的。请看上的页面,其中列出了调试此类错误的许多步骤。运气不好。我觉得这和我的配置有关。我将在上面添加我的端点配置。如果您使用Fiddler之类的工具查看请求,您是否看到404未找到或其他错误?如果您看到其他内容,例如400或500,则响应本身可能会有一些附加信息。如果没有,那么您可以尝试在服务中启用跟踪,它应该包含有关问题的更多信息。这也非常有用。这是否意味着必须特别启用WCF服务才能使用Silverlight前端?我已经在我的服务应用程序的根上设置了一个跨域实例。我仍然收到安全异常。是的,这是正确的。您可以在承载WCF服务的IIS上添加clientaccesspolicy.xml。这也非常有用。这是否意味着必须特别启用WCF服务才能使用Silverlight前端?我已经在我的根目录上设置了一个跨域实例
服务应用程序。我仍然收到安全异常。是的,这是正确的。您可以在承载WCF服务的IIS上添加clientaccesspolicy.xml。