[Silverlight] WCF客户端的编程配置

时间:2009-09-11 13:07:24

标签: wcf silverlight

我们正在通过WCF公开基于服务器的API开发Silverlight客户端。

我正在尝试将我的WCF客户端代码(工作正常)从基于配置的模型移动到编程模型。这将使我能够拥有一个“root”URL,我可以在启动时应用它,而不需要安装来维护庞大的配置文件。

我正在将我的配置转换为支持Silverlight的代码。

我在下面的服务中提供了以下配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_ISilverlightHelper">
                    <binaryMessageEncoding />
                    <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
                        <extendedProtectionPolicy policyEnforcement="Never" />
                    </httpTransport>
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:50072/API/WCF/Silverlight/SilverlightHelper.svc"
                binding="customBinding" bindingConfiguration="CustomBinding_ISilverlightHelper"
                contract="API.WCF.Silverlight.ISilverlightHelper" name="CustomBinding_ISilverlightHelper" />
        </client>
    </system.serviceModel>
</configuration>

我无法弄清楚如何创建equivelant client-config代码。目前我有:

CustomBinding customBinding = new CustomBinding();
// I see I need to do something with customBinding but the properties don't seem 
    // logical
    // I have used BasicHttpBinding, but it just returns with "Not Found" (the service does resolve to a valid URL)
BasicHttpBinding basicHttpBinding = new BasicHttpBinding() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50072/API/WCF/Silverlight/SilverlightHelper.svc");
ISilverlightHelper silverlightHelper= new ChannelFactory<ISilverlightHelper>(basicHttpBinding, endpointAddress).CreateChannel();
AsyncCallback asyncCallback = delegate(IAsyncResult result)
{
    ISilverlightHelper asyncSilverlightHelper = (ISilverlightHelper)result.AsyncState;
    string[] files=asyncSilverlightHelper.EndGetPlugInXapNames(result).ToArray();
};
silverlightHelper.BeginGetPlugInXapNames(asyncCallback, silverlightHelper);

任何线索都会受到赞赏。我整个上午花了Googling / Binging / Overflowing,但没有遇到过这种情况。或者我可能就是这么错......

1 个答案:

答案 0 :(得分:7)

对它进行排序。

我创建了BinaryMessageEncodingBindingElement和HttpTransportBindingElements,将它们添加到CustomBinding中,一切正常。

这是我带注释的代码:

// create the binding elements
BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };

// add the binding elements into a Custom Binding
CustomBinding customBinding = new CustomBinding(binaryMessageEncoding,httpTransport);

// create the Endpoint URL (I'll use a configured URL later - all web services will then move as one)
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50072/API/WCF/Silverlight/SilverlightHelper.svc");

// create an interface for the WCF service
ISilverlightHelper silverlightHelper= new ChannelFactory<ISilverlightHelper>(customBinding, endpointAddress).CreateChannel();

// set-up the asynchronous callback
AsyncCallback asyncCallback = delegate(IAsyncResult result)
{
    ISilverlightHelper asyncSilverlightHelper = (ISilverlightHelper)result.AsyncState;
    string[] files=asyncSilverlightHelper.EndGetPlugInXapNames(result).ToArray();
};

// execute the call
silverlightHelper.BeginGetPlugInXapNames(asyncCallback, silverlightHelper);