Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

c# - WCF will not return an 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;
  }
}

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();

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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;
    }
}

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";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...