WCF服务可以处理哪些数据类型? [英] Which data types WCF service can handle ?

查看:100
本文介绍了WCF服务可以处理哪些数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WCF的新手.以前,我使用WCF服务处理字符串,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>

解决方案

我的猜测是BitmapImage不是DataContractSerializer支持的类型之一(请参阅

My guess would be that BitmapImage is not one of the types supported by the DataContractSerializer (see http://msdn.microsoft.com/en-us/library/ms731923.aspx ), and so the data contract becomes invalid (and thus metadata generation fails, resulting in the error message you posted).

If this is the case, you will need to remove [DataMember] from the BitmapImage property, and create a new [DataMember] property that handles the serialization manually (converts MyProperty to some supported type such as byte[])

这篇关于WCF服务可以处理哪些数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆