简体   繁体   English

如何调用非静态webmethod表单客户端静态方法?

[英]how to call non static webmethod form client static method?

I' m trying to do something but I'm not sure if it is allowed in c# here is what I'm tring: 我正在尝试做一些事情,但我不确定它是否被允许在c#这里是我正在做的事情:

I have a Web Method which is not a static here it is: 我有一个Web方法,它不是一个静态的:

   [WebMethod]
    public Byte[] recStuff(Byte[] recstuffile)
    {
        myfile = Unzip(muStuff);

        return null;
    }

and here is my client: 这是我的客户:

 public static  XmlDataService.StufServiceSoapClient lhaservice = null;
        public static void Autoupload()
        {
            string fileContents = File.ReadAllText(XMLStuffName);
            string text = fileContents;
            byte r2 = Zip(text);
            lhaservice.recStuff(r2);
        }

I am getting Error that: 我收到错误:

Object reference not set to an instance of an object.

what can I do here? 我能在这做什么?

It is very logical. 这是非常合乎逻辑的。 lhaservice = null. lhaservice = null。 Initialize it. 初始化它。

In any case you have to instantiate lhaservice first before you use it in your (static) constructor: 无论如何,在你的(静态)构造函数中使用它之前,你必须首先实例化lhaservice

lhaservice = new XmlDataService.StufServiceSoapClient();

...but unless you show all your relevant code we don't really know for sure what the problem could be in your code. ...但除非您显示所有相关代码,否则我们并不确定您的代码中可能存在的问题。

Note: avoid static classes and operations if they don't make any sense. 注意:如果没有任何意义,请避免使用静态类和操作 Make them non-static and create an instance before using the Autoupload operation. 在使用Autoupload操作之前,将它们设置为非静态并创建实例。 Your code will become more flexible and testable. 您的代码将变得更加灵活和可测试。 So you might want to rethink your code. 所以你可能想重新考虑你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM