使用 SOAP 访问数据库的最佳方式 [英] best way to access database using SOAP

查看:26
本文介绍了使用 SOAP 访问数据库的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力了解 C# 的 SOAP 协议,我在 Google 中找到了一些示例并了解了信封、标头、正文.

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

我使用网络服务进行身份验证,但我想知道在哪里可以实现一个类或方法来使用提供的用户和密码访问数据库,我的意思是,soap 标头具有 user="john" pass="odos223kiwi0X"服务器接收到标头,现在使用提供的用户访问数据库并检查密码.

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.

如果一个正确的选项在soap类中创建一个自定义方法来做到这一点?

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

推荐答案

您可以创建一个类,如下所示:

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);
        }
    }
}

现在我们创建 asmx 服务:

and now we create the asmx service:

创建一个 Web 服务并将其添加到命名空间下:

create a web service and add this under the namespace :

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

然后添加您的函数和 Object[] 作为返回值.

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
}

** 你可以下载 http://www.fiddler2.com/fiddler2/version.asp 这将允许您查看和跟踪发出的请求

** 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.

这篇关于使用 SOAP 访问数据库的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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