Web服务 - SoapClient不在程序集引用中

时间:2017-10-02 06:00:23

标签: c# visual-studio web-services uwp soap-client

我正在做一些课程工作而且非常困惑,因为课程对于不同的事情使用看似相同的名字。

它创建一个Web服务,然后该应用程序使用它并显示结果。

Web服务有一个名为MBCProductDetails()的方法 使用它的应用程序中的实际服务引用也称为MBCProductDetails

这是课程让我使用的代码 -

private async void btnGetRates_Click(object sender, RoutedEventArgs e)
{
    MBCProductDetails.MBCProductDetailsSoapClient serviceMBCProductDetails = new MBCProductDetails.MBCProductDetailsSoapClient();

    MBCProductDetails.GetProductDetailsResponse MBCProductDetailsResponse = await serviceMBCProductDetails.GetProductDetailsAsync(Convert.ToInt32(txtProductCode.Text));

    tbProductNameValue.Text = "Product: " + MBCProductDetailsResponse.Body.GetProductDetailsResult.ProductName;
    tbInterestRateValue.Text = "Interest Rate: " + MBCProductDetailsResponse.Body.GetProductDetailsResult.InterestRate.ToString();
    tbAccountKeepingFeeValue.Text = "Account Fee: " +  MBCProductDetailsResponse.Body.GetProductDetailsResult.AccountFee.ToString();

}

现在我正在尝试自己这样做,但是当我这样做时,我得到MBCProductDeatilsSoapClient does not exist in the namespace.

有没有人可以告诉我这里发生了什么,哪些名字去哪里等等因为我彻底搞糊涂了。干杯

编辑:

这是来自Web服务的代码

public struct ProductDetails
{
    public int ProductCode;
    public string ProductName;
    public double InterestRate;
    public double AccountFee;
}

private ProductDetails Products;

public MBCProductDetails()
{
    Products.ProductCode = 0;
    Products.ProductName = "";
    Products.InterestRate = 0;
    Products.AccountFee = 0;
}

private void AssignValues(int ProductCode)
{

    Products.ProductCode = ProductCode;

    if (ProductCode == 1)
    {
        Products.ProductName = "Everyday Loan Account";
        Products.InterestRate = 4.5;
        Products.AccountFee = 10;
    }
    else if (ProductCode == 2)
    {
        Products.ProductName = "Business Loan Account";
        Products.InterestRate = 3.5;
        Products.AccountFee = 12;
    }
    else if (ProductCode == 3)
    {
        Products.ProductName = "Offset Loan Account";
        Products.InterestRate = 5.0;
        Products.AccountFee = 15;
    }
    else
    {
        Products.ProductName = "Loan Account not found";
        Products.InterestRate = 0.0;
        Products.AccountFee = 0;
    }
}

[WebMethod(Description = "This method call will get the product name, interest rate and the account fee for a given product code.", EnableSession = false)]
public ProductDetails GetProductDetails(int ProductCode)
{
    AssignValues(ProductCode);
    ProductDetails RequestedProductDetails = new ProductDetails();
    RequestedProductDetails.ProductCode = Products.ProductCode;
    RequestedProductDetails.ProductName = Products.ProductName;
    RequestedProductDetails.InterestRate = Products.InterestRate;
    RequestedProductDetails.AccountFee = Products.AccountFee;
    return RequestedProductDetails;
}

1 个答案:

答案 0 :(得分:0)

  

Web服务有一个名为MBCProductDetails()

的方法

在Web服务中,MBCProductDetails()这里应该是MBCProductDetails类的Constructor方法。因此,Web服务类应该是MBCProductDetails,完整的结构体网络服务应如下所示:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class MBCProductDetails : System.Web.Services.WebService
{ 
    [WebMethod(Description = "This method call will get the product name, interest rate and the account fee for a given product code.", EnableSession = false)]
    public ProductDetails GetProductDetails(int ProductCode)
    {
     ...
    }
    public MBCProductDetails()
    {
     ...
    }
    public struct ProductDetails
    {
      ...
    }
    private ProductDetails Products;
    private void AssignValues(int ProductCode)
    { 
    ...
    }
}
  

现在我正在尝试自己这样做,但当我这样做时,我得到MBCProductDeatilsSoapClient在命名空间中不存在。

在UWP客户端,对于代码行MBCProductDetails.MBCProductDetailsSoapClient,此处MBCProductDetails应该是添加的服务引用的名称空间。默认情况下,命名空间为ServiceReference1,如果您要使用上面的客户端代码段,则需要将其重命名为MBCProductDetails,如下图所示,否则您将获得“不存在”异常,因为那里不是名为MBCProductDetails的服务引用名称空间。 enter image description here

确保在添加服务引用后,在连接的服务中有MBCProductDetails

enter image description here

我身边的测试结果:

enter image description here

此外,我建议您使用WCF服务,因为“ASP.NET Web服务”(又名ASMX)是一项传统技术。您可以参考this thread详细信息。