【问题标题】:Why is this service method not showing up?为什么这个服务方法没有出现?
【发布时间】:2012-10-07 03:34:02
【问题描述】:

这是我第一次使用 Visual Studio 和 C# 进行编程。我正在尝试创建 Web 服务,但我的 GetProduct 没有出现。

  namespace GettingStartedHost
 {
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public int GetProduct(int a, int b)
    {
        return a * b;
    }

    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        int GetProduct(int a, int b);

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);



    }


    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }

}

}

当我按 CTRL-F5 启动测试服务器时,只显示 2 个方法。 GetProduct 不显示。怎么了?

【问题讨论】:

  • 样本缺少IService1的定义...
  • 您是否更新了客户端项目中的服务引用?
  • 您能详细说明一下吗?我似乎无法让它工作
  • 您必须填写我的姓名,您的回复才能显示在我的收件箱中;)我的意思是:您是否在您的客户项目中右键单击了您的服务参考并点击了Update Service Reference?

标签: c# asp.net visual-studio-2010 wcf web-services


【解决方案1】:

请试试这个代码。

namespace GettingStartedHost
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public int GetProduct(int a, int b)
        {
            return a * b;
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        int GetProduct(int a, int b);

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

}

【讨论】:

  • @Alan : 在你的代码中你有 Service1 : IService1 但你的接口代码有 IService 你缺少 1。
  • 您好 Jams,感谢您的帮助。我收到“命名空间已经包含 IService1 的定义”它仍然无法正常工作:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 2013-02-20
  • 2015-02-06
  • 2016-01-05
相关资源
最近更新 更多