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

查看:26
本文介绍了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();
}

这是我实现它的地方:

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)

几个小时以来,我一直在用头撞墙,但在 Google 上什么也找不到,而且我的同事也很困惑.为什么添加了两个定义中没有的out参数,并改变了返回类型?任何帮助将不胜感激.如果我遗漏了任何有用的信息,我深表歉意.

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 服务,但没有看到这样的问题 - 我只看到了一个 Specified 属性(并且它作为 int 旁边的一个属性,而不是作为两个 out 参数),当服务定义不允许指定 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).

如果您找不到更好的解决方案,这里有一个使用 部分类的解决方法 :(这必须在您返回 struct 时执行,而不仅仅是 ints)

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-Interoperability-Removing-the-Annoying-xx 有一个(有点笨拙的)解决方案,并告诉我们根本原因是 WCF 生成糟糕的(可以说是不准确的)WSDL - 他们在真正不需要它的元素上有一个 minOccurs="0".Web References 按原样读取它,并生成笨拙的代码来处理它,这正是您要处理的.根据他的文章,您可以返回此类型而不是 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天全站免登陆