【问题标题】:Which data types WCF service can handle ?WCF 服务可以处理哪些数据类型?
【发布时间】:2012-10-07 18:05:02
【问题描述】:

我是 WCF 的新手。以前我将 WCF 服务用于字符串、int32 等基本数据类型。但是当我尝试使用 BitmapImage 类时,它的测试客户端会出现以下错误

添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。

当我用 String 替换 BitmapImage 时,它工作正常。这意味着我缺少一些代码。

为了更好地理解这里是我的代码。

WCF 接口代码

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MyWcfService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void MyMethod(MyDataContract obj);
    }

    [DataContract]
    public class MyDataContract
    {
        [DataMember]        
        public BitmapImage MyProperty { get; set; }
    }
}

WCF 服务代码

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MyWcfService
{
    public class Service1 : IService1
    {
        public void MyMethod(MyDataContract obj)
        {
            //No code. It is blank.
        }
    }
}

Web.Config 代码

  <system.serviceModel>
    <services>
      <service name="MyWcfService.Service1" behaviorConfiguration="MyWcfService.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" contract="MyWcfService.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyWcfService.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

【问题讨论】:

  • 您能否发布您的 app.config 文件以获取更多信息 :)
  • 您是否尝试过将其转换为 byte[] 并在客户端将其转换回位图?

标签: c# wcf datacontract wcftestclient


【解决方案1】:

您应该在 MyDataContract 类中添加 BitmapImage 的 KnownType

[KnownType(typeof(BitmapImage))]
[DataContract]
    public class MyDataContract
    {
        [DataMember]        
        public BitmapImage MyProperty { get; set; }
    }

这是因为 string 是原始类型而 BitmapImage 不是,所以你应该“告诉”编译器在序列化/反序列化时它会处理哪些数据类型。

【讨论】:

  • ***** 这是你的 WCF 服务 :)
【解决方案2】:

我的猜测是 BitmapImage 不是 DataContractSerializer 支持的类型之一(请参阅 http://msdn.microsoft.com/en-us/library/ms731923.aspx ),因此数据合同变得无效(因此元数据生成失败,导致您发布的错误消息)。

如果是这种情况,您需要从 BitmapImage 属性中删除 [DataMember],并创建一个新的 [DataMember] 属性来手动处理序列化(将 MyProperty 转换为某些受支持的类型,例如 byte[])

【讨论】:

  • 转换为byte[] 是一种解决方案,我知道。但是,判断哪个类可序列化或哪个不可序列化的正确方法是什么?
  • 对于框架中的大多数类型,只需寻找 [Serializable] 属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多