使用SoapExtensionAttribute调用方法

时间:2009-09-09 09:08:53

标签: c# asmx soap-extension

我有问题,我想在web方法中设置soap扩展属性: Web服务中的Soap扩展:

public class EncryptMessageAttribute : SoapExtensionAttribute
{
    private string strKey="null";

    public void  setKey(string s)
    {
        strKey=s;
    }
}

Soap扩展类:

public class EncryptMessage : SoapExtension
{
....
}

Web方法上的Soap扩展:

public class Service1 : System.Web.Services.WebService
{
    public string k;

    [WebMethod]
    [EncryptMessageAttribute(setKey(k))]
    public string test2()
    {
        return "ok";
    } 

    [WebMethod]
    [EncryptMessage(setKey(k))]
    public string test2()
    {
        return "ok";
    }
}

完成此编译错误:

  

错误1名称'setKey'没有   存在于当前上下文错误2中   需要对象引用   非静态字段,方法或   属性

更新1:

我试过了:

public class Service1 : System.Web.Services.WebService
{
    public const string strAttribute = "something";

    [WebMethod]
    [EncryptMessage SetKey =strAttribute)]
    public string test2()
    {
        return "ok";
    }
}

有效。但是我想在客户端调用web方法之前想要更改属性,有可能,或者属性必须是const?

例如:public string strAttribute不起作用。

更新2:

我有另一个问题:

我有类,变量num:

public class EncryptMessage : SoapExtension
{
    public int num=10;
    ....
}

Web方法上的Soap扩展:

public class Service1 : System.Web.Services.WebService
{
    public const string k = "something";

    /*in this place I want call some methods, which change variable num in class 
      EncryptMessage,
      before that is soap extension used on web method .. it is possible ?
      If yes, how can I change variable in class EncryptMessage
    */

    int num2 = 5;
    someMethods(num2); // this methods change variable num in class EncryptMessage

    [WebMethod]
    [EncryptMessage(SetKey =k)]
    public string test2()
    {
        return "ok";
    }
}

1 个答案:

答案 0 :(得分:1)

你不能像你正在做的那样在属性上调用方法

使用属性,而不是方法:

public class EncryptMessageAttribute : SoapExtensionAttribute
{
    private string strKey="null";

    public string Key
    {
        get { return strKey; }
        set { strKey = value; }
    }
}

[WebMethod]
[EncryptMessageAttribute(Key = "null")]
public string test2()
{
    return "ok";
}