有没有自定义SOAP头在C#中的一个更好的办法 [英] Is there a better way of customizing SOAP headers in C#

查看:486
本文介绍了有没有自定义SOAP头在C#中的一个更好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去,我需要建立一个C#项目自定义SOAP头被使用导入的WSDL Web引用。我找到了一种方法来做到这一点,但我从来没有喜欢过它,我有意识想知道是否有更好的方法。我所做的就是创建一个派生自SOAPHEADER一个标题:

In the past I have needed to create custom SOAP headers in a C# project that was using an imported WSDL web reference. I found a way to do it but I was never happy with it and I have sense wondered if there was a better way. What I did was create a header that derives from SoapHeader:

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://model.test.net")]
[System.Xml.Serialization.XmlRootAttribute("securitytoken", Namespace = "http://model.test.net", IsNullable = false)]
public class SpecialHeader : SoapHeader
{ 
  [System.Xml.Serialization.XmlTextAttribute()]
  public string aheadervalue;
}

然后我不得不修改从WSDL中产生的code,并添加一个参照的文 CE到新的报头的一个实例,每个网页调用之前下了 我想包含自定义标题:

I then had to modify the code that was generated from the WSDL and add a referen ce to an instance of the new header and the following before each web call that I wanted to contain the custom header:

[System.Web.Services.Protocols.SoapHeaderAttribute("instancename", Direction=System.Web.Services.Protocols.SoapHeaderDirection.InOut)]

在哪里实例名称是在生成的类的自定义页眉的实例变量名。

Where "instancename" is the custom header's instance variable name in the generated class.

这工作得很好,只是在WSDL中的任何改变都需要,这一切都做一遍,因为它重新生成的类。在其他语言头可以的产生code以外的添加,所以也许我缺少的是用C#做的方式。是否有这样做的更好的办法?

This works fine except that any change in the WSDL requires that it all be done over again since it regenerates the class. In other languages the headers can be added outside of the generated code so maybe I'm missing the way that is done in C#. Are there better ways of doing this?

推荐答案

看来你使用的是.NET 2.0和ASMX web服务。 你知道,有一个在.NET 3.0称为WCF(Windows通信框架)的框架。 我知道这是不容易迁移到一个新的框架,但与WCF你这么多。 此外WCF可用于比Web服务(远程,MSMQ和更多)这么多。它是微软将赌注押在未来的框架。 IE浏览器。处理SOAP头是使用MessageContracts。

It seems that you are using .Net 2.0 and asmx webservices. Do you know that there is a framework called WCF (Windows Communication Framework) in .Net 3.0. I know that it is not easy to migrate to a new framework, but with WCF you get so much. Furthermore WCf can be used for so much more than WebServices (remoting, msmq and more). It is the framework that Microsoft is betting on for the future. Ie. manipulation a soap header is done using MessageContracts.

因此​​,答案是,在WCF你可以用MessageContracts做到这一点。

So the answer is that in WCF you can do this with MessageContracts.

这篇关于有没有自定义SOAP头在C#中的一个更好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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