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

标签 wcf silverlight

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

我正在尝试将我的 WCF 客户端代码(运行良好)从基于配置的模型转移到编程模型。这将使我拥有一个单一的“根”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>

我不知道如何创建等效的客户端配置代码。目前我有:

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

任何线索将不胜感激。我整个上午都在谷歌搜索/Binging/Overflowing 上度过,但还没有遇到这种情况。或者我可能错得太远了......

最佳答案

已排序。

我创建了 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);

关于wcf - [Silverlight] WCF 客户端的编程配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1410757/

相关文章:

c# - .Net Remoting 不使用.Net Remoting?

c# - 在类型上找不到匹配的构造函数

silverlight - 虚拟化 ListBox 的 ItemsControl 的边距无法正常工作

c# - Windows Phone 7 中的 XDocument.Parse 有何不同?

wcf - 通过https访问AppHarbor上的WCF服务

c# - 带有 WCF 的 EF : The underlying connection was closed: The connection was closed unexpectedly

asp.net - 使用 ELMAH 记录 WCF 服务的异常

c# - 如何解决 silverlight 2/3 中缺少的加密类?

c# - 基于 XAML 的关卡编辑器

c# - WCF 服务客户端应用程序获取 "Object not set to an instance of an object"