简体   繁体   English

如何在Windows Universal App中使用双工wcf服务

[英]How to consume duplex wcf service in Windows Universal App

How can I consume wcf service with duplex contract in Windows universal app? 如何在Windows通用应用程序中使用双工合同来使用wcf服务?

I'm getting PlatformNotSupportedExcetpion: Operation is not supported on this platform. 我正在使用PlatformNotSupportedExcetpion: Operation is not supported on this platform. runtime exception when trying to consume duplex wcf service in Windows Universal App, targeting Windows 10 (10.0; Build 10240) 尝试在Windows Universal App中使用双面wcf服务时出现运行时异常,目标是Windows 10(10.0; Build 10240)

According msdn it is supported API. 根据msdn,它支持API。

If it is not possible, how should I proceed in my scenario? 如果不可能,我应该如何进行我的方案? I have two applications (console and windows universal xaml app) running on the same machine and I need two-way communication. 我有两个应用程序(控制台和Windows通用xaml应用程序)在同一台机器上运行,我需要双向通信。

I have clasic .net 4.6 console app, that create the service host: 我有创建服务主机的clasic .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();

service contract: 服务合约:

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

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

I have tried both, ChannelFactory and generated wcf client by Add Service Reference dialog and both NetHttpBinding and NetTcpBinding in the in UWP app. 我已经通过“添加服务引用”对话框以及UWP应用程序中的NetHttpBindingNetTcpBinding同时尝试了ChannelFactory和生成的wcf客户端。

When I try to create instance of the wcf client, it throws the PlatformNotSupportedExcetpion. 当我尝试创建wcf客户端的实例时,它会抛出PlatformNotSupportedExcetpion。

Source: System.Private.ServiceModel 来源:System.Private.ServiceModel

StackTrace: 堆栈跟踪:

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

Stable versions of WCF as part of .NET Core 1.0 was just released last month. 作为.NET Core 1.0的一部分,WCF的稳定版本于上个月发布。 Duplex and many other WCF features can now be supported in Windows Universal Apps by referencing the 5.2.2 version of Microsoft.NETCore.UniversalWindowsPlatform package in the project.json file of a UWP project. 通过在UWP项目的project.json文件中引用5.2.2版本的Microsoft.NETCore.UniversalWindowsPlatform包,现在可以在Windows Universal Apps中支持双工和许多其他WCF功能

Duplex scenario is not supported even in the 10580 build (latest .NETCore v5.1.0). 即使在10580版本(最新的.NETCore v5.1.0)中也不支持双工方案。

There was a bug reported ion GitHub about the wrong usage of reflection in WCF duplex implementation. 有一个错误报告了离线GitHub关于WCF双工实现中错误使用反射的问题。 This bug was fixed in the latest build for .net core and you can include the individual package from Nuget gallery. 在.net核心的最新版本中修复了此错误,您可以在Nuget gallery中包含单个包 However, this package require you to also include the prerelease versions of System.Runtime and System.Threading. 但是,此程序包要求您还包括System.Runtime和System.Threading的预发布版本。

在此输入图像描述

Hope it helps, 希望能帮助到你,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM