添加WCF服务参考可以回退到XmlSerializer

时间:2011-10-04 16:44:39

标签: c# asmx wcf-client

我们通过向项目添加WCF“服务引用”来使用ASMX服务。当我们这样做时,默认情况下它应该使用DataContractSerializer,如果出现问题,它将回退到XmlSerializer

我在尝试生成代理类时尝试强制DataContractSerializer,但是当我这样做时,它们是不完整的,并且缺少webservice 使用的所有自定义类(只保留接口为Soap,SoapChannel和SoapClient类)

嗯,出现了问题,它正在回归使用XmlSerializer。生成引用时,我没有看到任何错误或警告。

如何找出导致DataContractSerializer失败并退回XmlSerializer的原因?

1 个答案:

答案 0 :(得分:0)

长话短说,我们无法强迫VS使用DataContractSerializer。相反,我们最终编写了代表Web服务的自己的WCF服务合同。当我们使用服务时,我们通过使用我们的OWN服务合同来创建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();
     }
 }