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

How to consume duplex wcf service in Windows Universal App

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

我在 Windows 通用应用程序中尝试使用双工 wcf 服务时遇到 PlatformNotSupportedExcetpion: Operation is not supported on this platform. 运行时异常,目标是 Windows 10(10.0;内部版本 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);
}

我在 UWP 应用程序中尝试了 ChannelFactory 和通过添加服务引用对话框生成的 wcf 客户端以及 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()

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

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

的预发布版本

希望对您有所帮助,

WCF 的稳定版本作为 .NET Core 1.0 的一部分刚刚于上个月发布。 UWP 项目的 project.json 文件中的 Duplex 和许多其他 WCF features can now be supported in Windows Universal Apps by referencing the 5.2.2 version of Microsoft.NETCore.UniversalWindowsPlatform 包。