【问题标题】:WCF array of doubles not called successfullyWCF 双打数组未成功调用
【发布时间】:2012-03-13 08:01:15
【问题描述】:

所以我设法使用 SOAP 没问题,但我在使用 WCF 做同样的工作时遇到了困难。

我的应用程序获取一个数字列表,然后使用 Web 服务将它们相加,我可以使用 SOAP 成功地做到这一点,但现在我在尝试使用 WCF 时遇到了麻烦。

        Webservices09004961.ServiceReference1.Service1SoapClient SumCLient = new ServiceReference1.Service1SoapClient();
        Webservices09004961.ServiceReference2.IService1 SumClientWCF = new ServiceReference2.Service1Client();

请注意,我以同样的方式调用客户端方法。

但是,当我尝试相同的方法时,我似乎无法像以前使用肥皂的方法那样调用 ArrayOfDoubles:

        Webservices09004961.ServiceReference2.Service1Client arrayOfdoubles = new ServiceReference2.Service1Client(); //this line is wrong but I tryed it anyway
        //Webservices09004961.ServiceReference1.ArrayOfDouble arrayOfDoubles = new Webservices09004961.ServiceReference1.ArrayOfDouble(); 
        arrayOfDoubles.AddRange(myArrai12);
        string f = SumCLientWCF.CalculateSum(arrayOfDoubles);
        Console.WriteLine("The sum total equals: " + f);

我担心我的问题在于我的 wcf 方法,Service1.svc.cs 看起来像这样:

namespace WcfSum
{
    // 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 CalculateSum(List<double> listDouble)
        {
            return listDouble.Sum().ToString();

             //return listDouble.Select(n => (double)n).ToString();

        }
    }
}

这对我来说似乎很好?我的 IService 可能是我的问题所在:

namespace WcfSum
{
    // 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 IService1
    {

        [OperationContract]
        string CalculateSum(List<double> listDouble);

    }

通常我会期望一旦我键入 Myservice.ServiceReference1。 arrayofdoubles 会出现,但这次我没有那个选项,我得到的唯一选项是 Service1 或 serviceclient。

编辑

为了澄清一点,因为我使用这种方法(因为 ArrayOfDoubles 不像我的肥皂版本那样被注释掉了)

    Webservices09004961.ServiceReference2.Service1Client arrayOfdoubles = new ServiceReference2.Service1Client(); //this line is wrong but I tryed it anyway
    //Webservices09004961.ServiceReference1.ArrayOfDouble arrayOfDoubles = new Webservices09004961.ServiceReference1.ArrayOfDouble(); 

我收到错误消息:

Error   1   'Webservices09004961.ServiceReference2.Service1Client' does not contain a definition for 'AddRange' and no extension method 'AddRange' accepting a first argument of type 'Webservices09004961.ServiceReference2.Service1Client' could be found (are you missing a using directive or an assembly reference?)

在 AddRange 下的这一行。

arrayOfdoubles.AddRange(myArrai12);

我正在努力找出原因:

Webservices09004961.ServiceReference2. 不会显示 ArrayOfDoubles 就像我的肥皂版本一样。

这可能有助于显示发生了什么:

【问题讨论】:

  • 你的双数之和是一个字符串??
  • 你知道,我很乐意用所有这些命名空间换取一点错误消息。现在甚至不清楚你是否有运行时或编译时错误。
  • 如果你让它与 SOAP 一起使用,那么你确定你没有它与 WCF 一起使用吗?您需要更具体地了解您的工作......
  • 大家好,我更新了。我最初认为这是我的 wcf 方法,因为我不能像使用我的肥皂版本那样调用 ArrayOfDoubles。而且我所做的一切都完全相同,只是改变了我的网络服务?
  • 我将 servicereference1 放入只是为了向您展示我之前使用肥皂的方法,以防它可能会混淆。

标签: c# wcf web-services soap


【解决方案1】:

好的,一些修复:

    var arrayOfdoubles = new ServiceReference2.Service1Client(); 
    var inputData = new List<double> { 1.1, 2.2 };
    string f = arrayOfdoubles.CalculateSum(inputData);
    Console.WriteLine("The sum total equals: " + f);

显然arrayOfdoubles 不是一个很好的名称(它是客户端代理),但我将其作为对您代码的参考。

【讨论】:

  • 嘿 Henk 非常感谢它的工作原理,但为什么我的方法与我用于肥皂的方法不同?我的意思是它应该显示我的 ArrayOfDoubles 现在真的很烦我,但你的方法也很好。 (至少我们知道它有效)
  • 嘿 Henk 我上传了一张服务引用的图片,我注意到它没有像我的肥皂版本那样显示我的 ArrayOfDouble。这是为什么呢?
猜你喜欢
  • 2013-08-04
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
相关资源
最近更新 更多