首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Silverlight 2 WCF 服務模板配置避坑指南:basicHttpBinding 與 ClientConfig 完整修正

     編輯:關於.NET

環境:Silverlight 2 Tools for VS2008 SP1 + VSTS2008 SP1 + MSSQL 2005。使用「啟用 Silverlight 的 WCF 服務」項目模板時,預設配置存在兩大問題需手動修正。

問題一:服務端 web.config 預設使用 customBinding

VS2008 英文版生成的 web.config 綁定為 customBinding:

<bindings>
  <customBinding>
    <binding name="customBinding0"></binding>
  </customBinding>
</bindings>
<services>
  <service behaviorConfiguration="wcf.WCFBehavior" name="wcf.WCF">
    <endpoint address="" binding="customBinding" bindingConfiguration="customBinding0" contract="wcf.WCF" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

Silverlight 僅支援 basicHttpBinding,需修改兩處:

<bindings>
  <basicHttpBinding>
    <binding name="customBinding0"></binding>
  </basicHttpBinding>
</bindings>
<services>
  <service ...>
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="customBinding0" contract="wcf.WCF" />
    ...
  </service>
</services>

問題二:客戶端 ServiceReferences.ClientConfig 為空

預設僅含 </configuration>,導致「關鍵字不在資料字典中」錯誤。需手動添加完整配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_WCF" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost:2254/WCF.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WCF" contract="MyData.WCF" name="BasicHttpBinding_WCF" />
    </client>
  </system.serviceModel>
</configuration>

完整驗證範例

服務端介面與實作

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCF {
    [OperationContract]
    public int CountUsers() { return 2; }
    [OperationContract]
    public IEnumerable<User> GetUser() {
        var list = new List<User>();
        list.Add(new User { IsMember = false, Name = "qewerl", Age = 34 });
        list.Add(new User { IsMember = true, Name = "asdful", Age = 23 });
        list.Add(new User { IsMember = true, Name = "Paul", Age = 254 });
        list.Add(new User { IsMember = false, Name = "John", Age = 64 });
        return list.AsEnumerable();
    }
}

[DataContract]
public class User {
    [DataMember] public bool IsMember { get; set; }
    [DataMember] public string Name { get; set; }
    [DataMember] public int Age { get; set; }
}

Silverlight 客戶端調用

public partial class MainPage : UserControl {
    public MainPage() {
        InitializeComponent();
        WCFClient c = new WCFClient();
        c.GetUserAsync();
        c.GetUserCompleted += (s, e) => {
            ObservableCollection<User> p = e.Result;
            p.ToList<User>();  // 可綁定至 DataGrid 等控制項
        };
    }
}

ObservableCollection<User> 可直接繫結至 DependencyObject.DataSource。服務引用過程省略,綁定細節網上資源豐富。

總結

  1. 服務端:customBinding → basicHttpBinding(兩處)
  2. 客戶端:補全 ServiceReferences.ClientConfig 完整配置
  3. 設定 maxBufferSize/maxReceivedMessageSize 避免大資料截斷
  4. security mode="None" 簡化開發測試

修正後即可正常運行 Silverlight-WCF 通訊。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved