best way to access database using SOAP

2020-07-23 12:00发布

问题:

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?

回答1:

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.