我正在爲一所學校的項目工作,我需要創建一個WCF服務,我可以在我的原始解決方案中使用它。爲什麼我的ASP C#WCF服務不工作?
我通過轉到文件 - >新建項目 - > WCF服務應用程序(添加到解決方案)創建了服務,然後通過在Assignment8解決方案和新引用下右鍵單擊References來添加服務引用。然後我發現解決方案中的服務並添加了產品服務。
我目前得到錯誤:UpdateProduct.aspx.cs上發生在Button1_Click的方法中的if語句行
'Assignment8.Product.ProductClient' does not contain a definition for 'UpdateProduct' and no extension method 'UpdateProduct' accepting a first argument of type 'Assignment8.Product.ProductClient' could be found (are you missing a using directive or an assembly reference?)
錯誤。我究竟做錯了什麼?
這裏是我的文件:
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.";
}
}
}