WCF不会返回一个int [英] WCF will not return an int

查看:155
本文介绍了WCF不会返回一个int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中建立一个WCF和客户端在同一时间使用它。出于某种原因,我有麻烦返回一个int的方法。这里是我的合同:

I am building a WCF in C#, and a client to consume it at the same time. For some reason I am having trouble getting a method to return an int. Here is my contract:

[ServiceContract]
public interface IMData
{
  [OperationContract]
  int ReturnAnInt();

  [OperationContract]
  String HelloWorld();
}

下面是我已经实现了它:

Here is where I have implemented it:

public class MData : IMData
{
  public String HelloWorld()
  {
    return "Hello World";
  }

  public int ReturnAnInt()
  {
    return 5;
  }
}



我在使用Visual Studio,以及客户端我今年进口WCF作为一个Web引用。现在,出于某种原因,当我宣布MDATA的实例,并尝试调用HelloWorld的,是没有问题的,但我得到调用ReturnAnInt当编译错误。

I'm using Visual Studio, and for the client, I imported this WCF as a Web Reference. Now for some reason when I declare an instance of MData and try to call HelloWorld, there is no problem, but I get a compile error when calling ReturnAnInt.

MData m = new MData();
String helloWorld = m.HelloWorld();
int result = m.ReturnAnInt();



我ReturnAnInt得到的错误是:
无过载方法'ReturnAnInt需要0参数
于是我鼠标,看看有什么Visual Studio的期待,和它说,该方法应该是这样的:

The error I get with ReturnAnInt is: "No overload for method 'ReturnAnInt' takes 0 arguments" So then I mouse over to see what Visual Studio is expecting, and it says that the method should look like:

void MData.ReturnAnInt(out int ReturnAnIntResult, out bool ReturnAnIntResultSpecified)

我一直在敲打我的头撞墙在这个数小时现在可以找到谷歌什么,它有我的同事感到困惑以及。为什么把它添加两头在外不在定义参数,并更改返回类型?任何帮助将不胜感激。我道歉,如果我离开了,这将是有帮助的任何信息。

I have been banging my head against a wall over this for hours now and can find nothing on Google, and it has my coworkers baffled as well. Why did it add two out parameters that aren't in the definition, and change the return type? Any help would be greatly appreciated. I apologize if I left out any information that would be helpful.

推荐答案

您可以导入它作为一个服务引用(新技术)而不是Web引用(较老的技术)?我通过服务引用与WCF服务工作,并没有看到这样一个问题 - 我只看到一个指定属性(这是旁边的<$ C $属性C> INT ,而不是两个退出 PARAMS)当服务定义允许任何 INT 被指定(WCF生成的服务定义了,以我的经验,当过预期)。

Can you import it as a Service Reference (newer tech) instead of a Web Reference (older tech)? I work with WCF services through service references and haven't seen such an issue - I've only seen a Specified property (and that as a property alongside the int, not as two out params) when the service definition allows no int to be specified (WCF-generated service definitions have, in my experience, worked as expected).

如果你不能找到一个更好的解决方案,这是一个使用一种解决方法< A HREF =htt​​p://msdn.microsoft.com/en-us/library/wa80x488.aspx相对=nofollow>部分类:在(这将不得不做你返回的任何时间结构,而不仅仅是 INT S)

If you can't find a better solution, here's a workaround using partial classes: (this would have to be done any time you return a struct, not just ints)

public partial class MData
{
    public int ReturnAnInt()
    {
        int result;
        bool specified;
        this.ReturnAnInt(out result, out specified);
        if (!specified) throw new InvalidOperationException();
        return result;
    }
}



更新 http://www.codeproject.com/Articles/323097/ WCF,ASMX互操作性去除最恼人的-XX 有一个(有点klunky)解决方案,并通知我们,根本原因是WCF产生不良(可以说是不准确的)WSDL中 - 他们有一个的minOccurs =0上真的不需要它的元素。 Web引用读取此原样,并产生klunky代码来处理它,这就是你要处理一下。根据他的文章,你可以返回此类型,而不是一个 INT

Update http://www.codeproject.com/Articles/323097/WCF-ASMX-Interoperability-Removing-the-Annoying-xx has a (somewhat klunky) solution, and informs us that the root cause is that WCF generates poor (arguably inaccurate) WSDLs - they have a minOccurs="0" on elements that really don't need it. Web References reads this as-is, and generates klunky code to deal with it, which is what you're trying to deal with. Based on his article, you can return this type instead of an int:

[MessageContract(IsWrapped = false)]
public class MyInt
{
    [MessageBodyMember]
    public int Result { get; set; }

    public static implicit operator MyInt(int i)
    {
        return new MyInt { Result = i };
    }

    public static implicit operator int(MyInt m)
    {
        return m.Result;
    }
}

随着修改方法的返回类型:

Along with modifying the return type of the method:

[ServiceContract]
public interface IMData
{
    [OperationContract]
    MyInt ReturnAnInt();

    [OperationContract]
    String HelloWorld();
}
public class Service1 : IMData
{
    public MyInt ReturnAnInt()
    {
        return 4;
    }

    public string HelloWorld()
    {
        return "Hello World";
    }
}

这篇关于WCF不会返回一个int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆