2

I am currently working to understand SOAP protocol with C#, I find some examples in Google and understand the envelope, header, body.

I authenticate with the webservice but I want to know where can I to implement a class or method to access a database with the user and password provided, I mean, soap header has user="john" pass="odos223kiwi0X" the server received the header, now access to database with the user provided and check the password.

if a right option create a custom method in the soap Class to do it?

3
  • 2
    I'd recommend using Windows Communication Foundation to abstract the nitty gritty details of SOAP implementation so that you can focus on just writing the code you need to handle your business logic May 5, 2011 at 13:28
  • It's not clear what technology you are using here. Have you implemented the entire service stack yourself? Or are you using asmx services?
    – MattDavey
    May 5, 2011 at 13:45
  • hi MattDavey! I am using asmx services.
    – Dev9
    May 5, 2011 at 14:04

1 Answer 1

2

you can create a class just as the following :

using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.Web.Services;
using System.Net;

[System.Web.Services.WebServiceBindingAttribute(
Name = "FunctionName",
Namespace = "nameSpace")]
public class ClassName:
System.Web.Services.Protocols.SoapHttpClientProtocol
{
    public ClassName(string uri) // Constractor
    {
        this.Url = uri; // the full path for your server we  will make later on in the answer
    }

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
    "nameSpace/ClassName",
    RequestNamespace = "nameSpace",
    ResponseNamespace = "nameSpace",
    Use = System.Web.Services.Description.SoapBindingUse.Literal,
    ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

    public object[] FunctionName(string Parameter1)
    {
        object[] results = { };

        try
        {
            results = this.Invoke("FunctionName", new object[] { Parameter1});
            return ((object[])(results[0]));
        }
        catch (Exception error)
        {
            object[] webException = { -1, error.Message };
            return (webException);
        }
    }
}

and now we create the asmx service:

create a web service and add this under the namespace :

[WebService(Namespace = "NameSpace")] //same namespace you wrote in the class

then add your function and Object[] as returning value.

[WebMethod]
public object[] FunctionName(string Parameter1) // function name and parameters should be the same in your class where you called the web service (case sensitive)
{
   ... // your code
}

** you can download http://www.fiddler2.com/fiddler2/version.asp that will allow you to see and trace the out going requests

please send me back if you need any farther info.

5
  • -1: why not use "Add Service Reference", or "Add Web Reference" if stuck at .NET 2.0? Sep 11, 2013 at 22:34
  • @JohnSaunders, I was looking for the easiest way to call a service too. I tried both Add Web Reference (which does not work in VS 2010 anymore) and Add Service reference. I also looked at wsdl.exe and svcutil.exe. All 4 methods generate massive code for me. The service that I am calling has 634 methods but I only need one method. so I just took Arrabi's answer because the code is more readable (which is actually part of the code generated by wsdl.exe minus Async parts).
    – AaA
    Jan 22, 2015 at 8:40
  • @BobSort: why does the amount of code in Reference.cs matter to you? What usually matters is the size of the code that you have to maintain, not the size of the code that is generated for you. Jan 22, 2015 at 13:51
  • It is not usual specially when you are generating code for machines with smaller memory. On the other hand 10 years ago we were thinking Add Web Reference is the best and yet it become obsolete. In 10 years I rather explain a 12 line code to my programmers than explaining how a 640Kb source file created and they cannot create such file anymore. And yes not only we have programs running since 2005 but also we have programs coded in 1990s
    – AaA
    Jan 23, 2015 at 6:46
  • also check this question for a few more reasons why you need to understand the code stackoverflow.com/questions/132720/…
    – AaA
    Jan 23, 2015 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.