我是 WCF 的新手。以前我将 WCF 服务用于字符串、int32 等基本数据类型。但是当我尝试使用BitmapImage
类时,它的测试客户端会给出以下错误
添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。
当我用它替换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>