2

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

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

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

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

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>
4

2 回答 2

2

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

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

于 2012-10-07T20:09:52.673 回答
2

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

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

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

于 2012-10-07T18:19:38.930 回答