WCF服务可以处理哪些数据类型?

时间:2012-10-07 18:05:03

标签: c# wcf datacontract wcftestclient

我是WCF的新手。以前我使用WCF服务用于基本数据类型,如string,int32等。但是当我尝试使用BitmapImage类时,其Test Client会出现以下错误

  

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

当我用BitmapImage替换String时,它可以正常工作。这意味着我错过了一些代码。

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

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>

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:2)

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

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