打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
silverlight调用wcf报错“给定关键字不在数据字典中”
一、             KeyNotFoundException,给定关键字不在字典中
         可通过使用启用silvlerlight功能的WCF服务。当然,普通的WCF也是可以使用在SL中的,只不过在配置上比较繁琐。
二、             无法序列化
1.         第一类错误:无法从未标记有 DataContractAttribute 或 SerializableAttribute 的类型继承类型……
         这类错误可以通过加上如上提示的属性解决,但更多时候需要检查是否调用的是“启用silvlerlight功能的WCF服务”。
 
2.         第二类错误:尝试对参数 http://tempuri.org/ 进行序列化时出错。
         同第一类。也有可能是代码缺少必要的属性。
 
3.         第三类错误:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错。
         同第一类。也可从修改reference.cs中的属性的namespace解决。
 
4.         第四类错误:“Element”命名空间“***”中的“***”并非所需元素。所需元素应为“__identity”
同第一类。
 
PS: 参数类或者返回值类不能包含任何方法和构造函数。不然也会出现不能序列化。     
三、             MarshalByRefObject
如果调用的是普通的WCF,则极有可能在reference.cs产生的客户端代码中,有System.MarshalByRefObject。这应该是微软WCF的一个BUG。因为SL中根本就没有这个类。查看WEBSERVICE,客户端代码中自动生成了MarshalByRefObject。那么参照WS,我们也可以生成一个供WCF使用的MarshalByRefObject。如下:
 view plaincopy to clipboardprint?
[System.Runtime.Serialization.KnownTypeAttribute(typeof(DbParameter))]  
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(SqlParameter))]  
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]  
    [System.Runtime.Serialization.DataContractAttribute(Name = "MarshalByRefObject", Namespace = "http://tempuri.org/")]  
    public abstract partial class MarshalByRefObject : object, System.ComponentModel.INotifyPropertyChanged  
    {  
 
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;  
 
        protected void RaisePropertyChanged(string propertyName)  
        {  
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;  
            if ((propertyChanged != null))  
            {  
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));  
            }  
        }  
    } 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(DbParameter))]
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(SqlParameter))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name = "MarshalByRefObject", Namespace = "http://tempuri.org/")]
    public abstract partial class MarshalByRefObject : object, System.ComponentModel.INotifyPropertyChanged
    {
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

四、             SqlParameter传值的问题
         SqlParameter这个参数比较特殊,在SL调用webservice的时候是支持的,在SL调用WCF的时候是不支持。
         如果一定要传这类参数,可以自己创建一个SqlParameter类,在SL端和WCF端均调用即可。
 
五、             异步转同步
         WCF默认不支持同步调用,需要开发者自己拓展。见http://blog.csdn.net/luminji/archive/2010/02/02/5281944.aspx
六、             典型配置ServiceReferences.ClientConfig
<configuration>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomBinding_IYiPinPropWCF">
          <binaryMessageEncoding />
          <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
            <extendedProtectionPolicy policyEnforcement="Never" />
          </httpTransport>
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8894/YiPinPropWCF.svc" binding="customBinding"
        bindingConfiguration="CustomBinding_IYiPinPropWCF" contract="ServiceReferenceYiPinProp.IYiPinPropWCF"
        name="CustomBinding_IYiPinPropWCF" />
    </client>
  </system.serviceModel>
</configuration>
 
6:典型配置Web.config
<system.serviceModel>
  <behaviors>
   <serviceBehaviors>
    <behavior name="YiPin.QuestionsDbSL.Web.YiPinPropWCFBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
  <bindings>
   <customBinding>
    <binding name="customBinding0">
     <binaryMessageEncoding />
     <httpTransport/>
    </binding>
   </customBinding>
  </bindings>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  <services>
   <service behaviorConfiguration="YiPin.QuestionsDbSL.Web.YiPinPropWCFBehavior"
    name="YiPin.QuestionsDbSL.Web.YiPinPropWCF">
    <endpoint address="" binding="customBinding" bindingConfiguration="customBinding0"
     contract="YiPin.QuestionsDbSL.Web.IYiPinPropWCF" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
   </service>
  </services>
 </system.serviceModel>
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Winform客户端引用WCF客户端后,部分类无法正常使用
C# 使用MarshalByRefObject跨程序调用方法
.net 真实代理和透明代理的交互
System.MarshallByRefObject.cs
【重要】VB.NET中的多行字符串
Silverlight Wcf 获取集合数据并绑定
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服