添加 WCF 服务引用回退到 XmlSerializer

发布于 2024-12-08 02:12:50 字数 435 浏览 1 评论 0原文

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

我尝试在生成代理类时强制使用 DataContractSerializer,但是当我这样做时,它们是不完整的并且缺少 Web 服务使用的所有自定义类(仅留下用于Soap、SoapChannel 和 SoapClient 类)

好吧,出了点问题,它又转而使用 XmlSerializer。生成参考时,我没有看到任何错误或警告。

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

We are consuming an ASMX service by adding a WCF "Service Reference" to our project. When we do this, by default it is supposed to use the DataContractSerializer and if something goes wrong, it will fall back to the XmlSerializer.

I've tried forcing the DataContractSerializer when generating the proxy classes, but when I do that, they are incomplete and missing all of the custom classes used by the webservice (leaving only the interface for the Soap, SoapChannel, and the SoapClient class).

Well, something is going wrong and it is falling back to the use the XmlSerializer. I do not see any errors or warnings when I generate the reference.

How can I find out what is causing the DataContractSerializer to fail and fall back to the XmlSerializer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-12-15 02:12:50

长话短说,我们无法强制 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();
     }
 }

Long story short is that we were unable to force VS to use the DataContractSerializer. Instead we ended up writing our own WCF Service Contracts that represented the webservice. When we consume the service we are instead creating the ChannelFactory generically by using our OWN Service Contracts. Below is the code that we used to create the channel.

 /// <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();
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文