为什么我会收到InvalidCastException的从OperationContext.Current.GetCallbackChannel<>() [英] Why do I get InvalidCastException from OperationContext.Current.GetCallbackChannel<>()

查看:113
本文介绍了为什么我会收到InvalidCastException的从OperationContext.Current.GetCallbackChannel<>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,使aysc回调到客户端,我需要开始/结束的方法添加到被定义为CallbackContract对我的服务的接口。 (我使用的是剪切来件装配,而不是生成代理类)

So as to enable aysc callbacks to the clients, I need to add the Begin/End methods to the interface that is defined as the CallbackContract on my service. (I am using a sheared contract assembly, rather than generating proxy classes)

[ServiceContract(CallbackContract=typeof(IClient)]
interface IEngineManager

作为第一步,我的抄袭的IClient接口从共享接口组件到本地名字空间,未做任何改动。我预计,随着界面的逻辑一样的是什么合同,WCF将允许它被使用。 但是WCF不喜欢它出于某种原因,为什么呢?

As the first step I have just copied the IClient interface from the shared interface assembly into a local name space, without making any other changes. I would expect that as the interface is logically the same as what is in the contract, WCF will allow it to be used. However WCF does not like it for some reason, why?

[ServiceContract]
public interface IClient
{
  [OperationContract(Action = "ReceiveMessage", 
     ReplyAction = "ReceiveMessageResponse")]
  void ReceiveMessage(SimMessage message);
}

//....
// this give the InvalidCastException
var client = OperationContext.Current.GetCallbackChannel<MyNameSpace.IClient>();

但是,如果我只是用原来IClient接口从共享来件装配,所有的工作!

However if I just use the original IClient interface from the shared contract assembly, it all works!

推荐答案

由于DxCK西亚德传递给OperationContext.Current.GetCallbackChannel类型&LT;吨>()必须完全按照您在[的ServiceContract]属性的界面上设置的相同类型的服务实现。

As DxCK siad the type that is passed to OperationContext.Current.GetCallbackChannel<T>() must be exactly the same type as you specified in the [ServiceContract] attribute on the interface that the service implements.

不过,我需要使用不同的接口的回调,所以我可以添加开始/结束的方法来支持aysc回调需要。

However I need to use a different interface for the callbacks so I can add the Begin/End methods needed to support aysc callbacks.

所以,首先我的新的回调接口。

So firstly my new callback interface.

[ServiceContract]
public interface IClientWithAsyncMethods : IClient
{
  [OperationContract(
    AsyncPattern = true, 
    Action = "ReceiveMessage", 
    ReplyAction = "ReceiveMessageResponse")]
  IAsyncResult BeginReceiveMessage(SimMessage message, 
    AsyncCallback callback, object asyncState);

  void EndReceiveMessage(IAsyncResult asyncResult);
}

然后需要定义一个新的接口为我服务implment:

Then need to define a new interface for my service to implment:

[ServiceContract(CallbackContract = typeof(IClientWithAsyncMethods))]
public interface IEngineManagerWithAsyncCallbacks : IEngineManager
{
}

唯一的变化向refare到新的回调接口为CallbackContract,这是正常的IClientWithAsyncMethods是IClient的子类型。

The only change to to refare to the new callback interface as the CallbackContract, this is OK as IClientWithAsyncMethods is a subtype of IClient.

然后,最后一个步骤是改变服务实现,以使用到服务接口

Then the last step is to change the serve implementation to use to the service interface:

  • 将接口类型更改服务实现
  • 通过新的接口转换成ServiceHost.AddServiceEndpoint()(和/或编辑WCF配置文件)
  • 使用IClientWithAsyncMethods在调用OperationContext.Current.GetCallbackChannel &LT; IClientWithAsyncMethods >()
  • Change the interface type the service implements
  • Pass the new interface into ServiceHost.AddServiceEndpoint() (and/or edit the WCF config files)
  • Use IClientWithAsyncMethods in the call to OperationContext.Current.GetCallbackChannel<IClientWithAsyncMethods>()

剩下的就是要求在正常方式aysc方法。

The rest is just calling the aysc method in the normal way.

这篇关于为什么我会收到InvalidCastException的从OperationContext.Current.GetCallbackChannel&LT;&GT;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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