[英]how to add delegate class for service manager class when calling soap service in c#?
First of all, I want to share my scenario what i want to build - 首先,我想分享我想要构建的场景 -
Scenario: I am building a client app using wpf. 场景:我正在使用wpf构建客户端应用程序。 In some cases, I need to call a web service to get data from the server.
在某些情况下,我需要调用Web服务来从服务器获取数据。 In order to do this, I added a web reference using wsld url.
为此,我使用wsld url添加了一个Web引用。 And I created a ServiceManager class that will call service method.
我创建了一个将调用服务方法的ServiceManager类。 For security reason, I need to add some header info at soap xml request for example, UserToken, SAML Token and so on.
出于安全原因,我需要在soap xml请求中添加一些头信息,例如UserToken,SAML Token等。 I can this from my ServiceManager class.
我可以从我的ServiceManager类中获取此信息。 But I want to add another class which will be called before sending request to the server.
但是我想添加另一个在向服务器发送请求之前调用的类。 In that class, I will do something like adding security header to soap xml request with request and then send it to the server.
在那个类中,我会做一些事情,比如将soap xml请求的安全头添加到请求中,然后将其发送到服务器。
I used SOAP Extension to fulfill my purpose and it works well. 我使用SOAP扩展来实现我的目的,它运行良好。 But the problem is, every-time I need to add annotation in Reference.cs (for each web service reference) file at top of the service method.
但问题是,每次我需要在服务方法顶部的Reference.cs(对于每个Web服务引用)文件中添加注释。 I believe that there is some other easiest way to make this working better than SOAP Extension.
我相信还有一些其他最简单的方法可以使它比SOAP扩展更好地工作。 Is there any way where I can only call the service and a delegate class will be called automatically and I don't need to add any annotation to the reference file?
有什么方法我只能调用服务,自动调用委托类,我不需要在参考文件中添加任何注释? I will share my sample code here.
我将在这里分享我的示例代码。
ServiceManage class: ServiceManage类:
public class ServiceManager
{
public UserDataService dataService; //web service added at Web Reference
public ServiceManager()
{
dataService = new UserDataService();
getUserServiceRequest rqst = new getUserServiceRequest();
getUserServiceResponse resp = dataService.getUser(rqst);
}
}
Reference.cs Reference.cs
[TraceExtensionAttribute(Name = "First")]
public getUserServiceResponse getUser([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] getUserServiceRequest request) {
object[] results = this.Invoke("getUser", new object[] {
request});
return ((getUserServiceResponse)(results[0]));
}
TraceExtensionAttribute.cs TraceExtensionAttribute.cs
[AttributeUsage(AttributeTargets.Method)]
public class TraceExtensionAttribute : SoapExtensionAttribute
{
private string mstrName = null;
public override Type ExtensionType
{
get { return typeof(TraceExtension); }
}
public override int Priority
{
get { return 1; }
set { }
}
public string Name
{
get { return mstrName; }
set { mstrName = value; }
}
}
TraceExtension.cs TraceExtension.cs
public class TraceExtension : SoapExtension
{
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attr){//..do something}
public override void Initialize(object initializer){//..do something}
public override Stream ChainStream(Stream stream){//...do something}
public override void ProcessMessage(SoapMessage message) {//..do something}
}
Finally, I found the solution. 最后,我找到了解决方案。 Just through out Web Reference and add Service Reference instead.
只需通过Web Reference并添加Service Reference即可。 Then go to the following link .
然后转到以下链接 。 It works for me.
这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.