【问题标题】:How to consume duplex wcf service in Windows Universal App如何在 Windows 通用应用程序中使用双工 wcf 服务
【发布时间】:2016-08-17 04:59:26
【问题描述】:

如何在 Windows 通用应用程序中使用双工合同的 wcf 服务?

我在尝试使用 Windows 通用应用程序中的双工 wcf 服务时遇到 PlatformNotSupportedExcetpion: Operation is not supported on this platform. 运行时异常,目标是 Windows 10 (10.0; Build 10240)

根据msdn 支持API。

如果不可能,我应该如何在我的场景中进行?我有两个应用程序(控制台和 windows 通用 xaml 应用程序)在同一台机器上运行,我需要双向通信。

我有创建服务主机的经典 .net 4.6 控制台应用程序:

var host = new ServiceHost(typeof(MyService), new Uri("net.tcp://localhost:8008/MyService"));

var binding = new NetTcpBinding(); //I've also tried net http binding
binding.Security.Mode = SecurityMode.None;

host.Description.Behaviors.Add(new ServiceMetadataBehavior());
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, 
                        MetadataExchangeBindings.CreateMexTcpBinding(),
                        "mex");  

host.AddServiceEndpoint(typeof(IMyService), binding, "");
host.Open();

服务合同:

[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void Initialize();
}

public interface IMyServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void OnFrame(int i);
}

我已经尝试过 ChannelFactory 和通过添加服务引用对话框生成的 wcf 客户端,以及 UWP 应用中的 NetHttpBindingNetTcpBinding

当我尝试创建 wcf 客户端的实例时,它会抛出 PlatformNotSupportedExcetpion。

来源:System.Private.ServiceModel

堆栈跟踪:

 at System.ServiceModel.ReflectionExtensions.GetInterfaceMap(Type type, Type interfaceType)
   at System.ServiceModel.Description.TypeLoader.GetIOperationBehaviorAttributesFromType(OperationDescription opDesc, Type targetIface, Type implType)
   at System.ServiceModel.Description.TypeLoader.<>c__DisplayClass8.<AddBehaviorsFromImplementationType>b__10(Type currentType, KeyedByTypeCollection`1 behaviors)
   at System.ServiceModel.Description.TypeLoader.AddBehaviorsAtOneScope[IBehavior,TBehaviorCollection](Type type, TBehaviorCollection descriptionBehaviors, ServiceInheritanceCallback`2 callback)
   at System.ServiceModel.Description.TypeLoader.AddBehaviorsFromImplementationType(ServiceEndpoint serviceEndpoint, Type implementationType)
   at System.ServiceModel.ChannelFactory`1.ReflectOnCallbackInstance(ServiceEndpoint endpoint)
   at System.ServiceModel.ChannelFactory`1.CreateDescription()
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(Binding binding, EndpointAddress address)
   at System.ServiceModel.DuplexChannelFactory`1..ctor(Object callbackObject, Binding binding, EndpointAddress remoteAddress)
   at System.ServiceModel.ClientBase`1..ctor(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress)
   at System.ServiceModel.DuplexClientBase`1..ctor(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress)
   at App1.ServiceReference1.MyServiceClientBase..ctor(InstanceContext callbackInstance)
   at App1.ServiceReference1.MyServiceClient..ctor(MyServiceClientCallback callbackImpl)
   at App1.ServiceReference1.MyServiceClient..ctor()
   at App1.MainPage.<button_Click>d__1.MoveNext()

【问题讨论】:

  • 我有一个 UWP 客户端应用程序,该应用程序在连接到双工 net.tcp WCF 服务时运行良好。迁移到 Windows10 后,我从头开始重新创建项目,现在我得到了相同的 PlatformNotSupportedException
  • 我在这个特定的解决方案中有 23 个项目。他们都使用AnyCPU平台,除了新创建的UWP项目,它只接受/允许x86x64平台,而不是AnyCPU。所以,我想这就是问题所在。我试图将项目文件弄乱,手动添加AnyCPU....但这当然并不顺利。再一次,这曾经在 Windows 8.1 下没有问题。 UWP 项目模板或类似性质的东西可能存在一些问题。
  • 另一个线程中的某人注意到了这一点。删除 CallbackContract 属性后,UWP 客户端可以创建连接,因此基本的 WCF 可以工作。然后他被困在 UWP 中创建双工 WCF 客户端

标签: c# wcf windows-10 uwp nettcpbinding


【解决方案1】:

作为 .NET Core 1.0 一部分的稳定版 WCF 于上个月刚刚发布。通过在 UWP 项目的 project.json 文件中引用 Microsoft.NETCore.UniversalWindowsPlatform 包的 5.2.2 版本,现在可以在 Windows 通用应用中支持 Duplex 和许多其他 WCF features

【讨论】:

    【解决方案2】:

    即使在 10580 版本(最新的 .NETCore v5.1.0)中也不支持双工方案。

    在 GitHub 上报告了一个关于 WCF 双工实现中错误使用反射的错误。此错误已在 .net 核心的最新版本中修复,您可以包含来自 Nuget 库的 individual package。但是,此软件包要求您还包含 System.Runtime 和 System.Threading 的预发布版本。

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多