反射和WCF
我正在使用 InvokeMember 方法调用 WCF 方法。WCF 方法采用一个整数和一个 out 对象作为参数。这是 WCF 服务中的代码:
public int SimpleTest(int n, out object OBJ)
{
OBJ = new Int32();
OBJ = 12;
return n;
}
当我使用 InvokeMember 调用带有参数 new Object[]{1 , obj}
的函数时,obj 按预期变为 12。
但是当 SimpleTest 中的 OBJ 设置为复杂对象 (OBJ = new MyClass())
时,我在调用该方法的页面上收到以下异常: Exception has returned by the target of调用。
内部异常指出底层连接已关闭:连接意外关闭。
我无法理解为什么会发生此异常。有人能解释一下吗?
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 inside 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MyClass 的构造函数做什么? MyClass 是否有静态构造函数?
调用的目标已引发异常。
例如,可以由类的静态构造函数内的异常引发,因此看起来 MyClass 的静态构造函数正在尝试连接到某些东西(像数据库一样),但无法,因为连接已经关闭。请记住,静态构造函数不会在启动程序时运行,而是在创建
MyClass
的第一个实例之前运行。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.在服务器上配置 WCF 跟踪将向您显示到底出了什么问题。
我想您会发现服务端通道堆栈正在抛出异常,因为它试图将输出参数序列化为响应消息以发送回客户端。这会导致服务端通道出现故障,并且您在客户端看到的异常是由服务发起的连接随之断开的客户端视图。
序列化时出现异常的原因是您的数据协定告诉操作格式化程序需要一个普通的
对象
,但您的方法正在输出一个EntityObject
。如果要支持基本简单类型以外的输出参数值,则需要为格式化程序提供有关可能需要序列化的具体类型的更多信息,方法是使用KnownTypeAttribute
或通过显式管道服务通道堆栈中的代码。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 anEntityObject
. 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 theKnownTypeAttribute
or by explicit plumbing in code in the service channel stack.