添加WCF服务引用的任务落在XmlSerializer上
本文关键字:XmlSerializer 任务 WCF 服务 引用 添加 | 更新日期: 2023-09-27 18:10:29
我们通过向项目中添加WCF "服务引用"来消费ASMX服务。当我们这样做时,默认情况下应该使用DataContractSerializer
,如果出现问题,它将返回到XmlSerializer
。
我已经尝试在生成代理类时强制DataContractSerializer
,但是当我这样做时,它们是不完整的,并且缺少webservice 使用的所有自定义类(只留下Soap, SoapChannel和SoapClient类的接口)。
嗯,出了什么问题,它正在回到使用XmlSerializer
。当我生成引用时,没有看到任何错误或警告。
如何找出导致DataContractSerializer
失败并回落到XmlSerializer
的原因?
长话短说,我们无法强制VS使用DataContractSerializer。相反,我们最终编写了自己的代表web服务的WCF服务契约。当我们使用服务时,我们使用自己的服务契约来创建ChannelFactory。下面是我们用来创建通道的代码。
/// <summary>
/// A generic webservice client that uses BasicHttpBinding
/// </summary>
/// <remarks>Adopted from: http://blog.bodurov.com/Create-a-WCF-Client-for-ASMX-Web-Service-Without-Using-Web-Proxy/
/// </remarks>
/// <typeparam name="T"></typeparam>
public class WebServiceClient<T> : IDisposable
{
private readonly T channel;
private readonly IClientChannel clientChannel;
/// <summary>
/// Use action to change some of the connection properties before creating the channel
/// </summary>
public WebServiceClient(string endpointUrl, string bindingConfigurationName)
{
BasicHttpBinding binding = new BasicHttpBinding(bindingConfigurationName);
EndpointAddress address = new EndpointAddress(endpointUrl);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
this.clientChannel = (IClientChannel)factory.CreateChannel();
this.channel = (T)this.clientChannel;
}
/// <summary>
/// Use this property to call service methods
/// </summary>
public T Channel
{
get { return this.channel; }
}
/// <summary>
/// Use this porperty when working with Session or Cookies
/// </summary>
public IClientChannel ClientChannel
{
get { return this.clientChannel; }
}
public void Dispose()
{
this.clientChannel.Dispose();
}
}