how to add delegate class for service manager class when calling soap service in c#?

523 Views Asked by At

First of all, I want to share my scenario what i want to build -
Scenario: I am building a client app using wpf. In some cases, I need to call a web service to get data from the server. In order to do this, I added a web reference using wsld url. And I created a ServiceManager class that will call service method. For security reason, I need to add some header info at soap xml request for example, UserToken, SAML Token and so on. I can this from my ServiceManager class. 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.

I used SOAP Extension to fulfill my purpose and it works well. 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. I believe that there is some other easiest way to make this working better than SOAP Extension. 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:

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

[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

[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

 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}
}
1

There are 1 best solutions below

0
On

Finally, I found the solution. Just through out Web Reference and add Service Reference instead. Then go to the following link. It works for me.