开发者

Reflection and WCF

开发者 https://www.devze.com 2023-02-13 21:37 出处:网络
i am calling a WCF method using InvokeMember Method.The WCF method takes an integer and anout object as parameter. this is the code in WCF service:

i am calling a WCF method using InvokeMember Method.The WCF method takes an integer and an out object as parameter. this is the code in WCF service:

 public int SimpleTest(int n, out object OBJ)
    {
        OBJ = new Int32();
        OBJ = 12;
        return n;
    }

when i use InvokeMember to call the function with parameters new Object[]{1 , obj} , obj becomes 12 as expected.

but when OBJ insid开发者_运维问答e SimpleTest is set to a complex object (OBJ = new MyClass()) i get the following exception on the Page that called the method: Exception has been thrown by the target of an invocation.

the inner exception states that The underlying connection was closed: The connection was closed unexpectedly.

i can't understand why this exception occured. can anybody explain?


What does MyClass' constructor do? Does MyClass by any chance have a static constructor?

Exception has been thrown by the target of an invocation. can for example be raised by an exception inside a static constructor for a class, so it seems that the static constructor for MyClass is trying to connect to something (like a database), but is not able because the connection is already closed.

Remember that the static constructor is not run when you start the program, but before the first instance of MyClass is created.


Configuring WCF tracing on the server will show you exactly what is going wrong.

I think you will find that the service-side channel stack is throwing an exception as it is trying to serialize the out parameter into a response message to send back to your client. This faults the service-side channel, and the exception you see on the client side is the client-side view of the consequential tearing down of the connection, initiated by the service.

The reason for the exception while serializing is that your data contract tells the operation formatter to expect a vanilla object but your method is outputting an EntityObject. If you want to support output parameter values other than basic simple types, you need to give the formatter more information about the concrete types which may need to be serialized, either by using the KnownTypeAttribute or by explicit plumbing in code in the service channel stack.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号