首页 > 解决方案 > WCF Service Reference.cs 方法给出错误:非静态字段、方法或属性需要对象引用

问题描述

我多次看到这个问题并且明白我需要在原始方法声明中添加静态。问题是这不是我控制的方法。在我的 C# 代码中,我添加了一个服务参考。它是 SOAP (XML) API。这不是我的 API。编译器错误出现在我在程序中调用的 Reference.cs 中的 WCF void 方法上。

程序.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Console.ThirdPartyService;

namespace Console
{
    class Program
    {
        static void Main(string[] args)
        {
            // New Third Party Service Client
            ThirdPartyServiceClient client = new ThirdPartyServiceClient();

            // Set user email address
            // Method is type void.
            // Compiler generates object reference error for non-static method.
            client.SetEMail("test@example.com");
        }
    }
}

参考.cs

...
public void SetEmail(string email) {
    base.Channel.SaveUserInfo(email);
}
...

假设服务提供商不会更新他们的 API 来解析,如果这是解决方案的话。我还能如何解决这个问题?

标签: c#wcfsoapstatic-methodsservice-reference

解决方案


推荐阅读