为什么我的ASP C#WCF服务无法正常工作?

时间:2016-04-27 03:35:05

标签: c# asp.net wcf

我正在为学校开展项目,我需要创建一个可以在原始解决方案中使用的WCF服务。

我通过转到文件创建了该服务 - >新项目 - > WCF服务应用程序(添加到解决方案)然后我通过右键单击Assignment8解决方案和新引用下的引用添加了服务引用。然后我在解决方案中发现了服务并添加了产品服务。

我目前正在收到错误:

  

' Assignment8.Product.ProductClient'不包含' UpdateProduct'的定义并且没有扩展方法' UpdateProduct'接受类型' Assignment8.Product.ProductClient'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

错误发生在具有Button1_Click方法中if语句的行上的UpdateProduct.aspx.cs中。我做错了什么?

以下是我的文件:

IProduct.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace ProductService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IProduct
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        Boolean UpdateProduct(int productID, string productName, string productDescription, decimal productPrice, decimal productCost);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

Product.svc

<%@ ServiceHost Language="C#" Debug="true" Service="ProductService.Product" CodeBehind="Product.svc.cs" %>

Product.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace ProductService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Product : IProduct
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

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

        public bool UpdateProduct(int productID, string productName, string productDescription, decimal productPrice, decimal productCost)
        {
            return false;
        }
    }
}

UpdateProduct.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
        {
            using (Product.ProductClient orderproxy = new Product.ProductClient())
            {
                if(orderproxy.UpdateProduct(Request.QueryString["ID"], txtProductName.Text, txtProductName.Text, txtProductDescription.Text, Convert.ToDecimal(txtProductPrice.Text), Convert.ToDecimal(txtProductCost.Text))){
                    Response.Redirect("~/ProductList.aspx");
                } else {
                    lblStatus.Text = "The product could not be updated.";
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您的UpdateProduct函数定义只需要5个参数,但在您的调用中,您传递的是6个参数