代码之家  ›  专栏  ›  技术社区  ›  Program.X

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

  •  2
  • Program.X  · 技术社区  · 17 年前

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

    1 回复  |  直到 17 年前
        1
  •  7
  •   Program.X    17 年前

    我创建了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);
    
    推荐文章