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

Lie*_*ero 6 c# wcf nettcpbinding windows-10 uwp

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

我PlatformNotSupportedExcetpion: Operation is not supported on this platform.试图在Windows Universal App中使用双面wcf服务时遇到运行时异常,目标是Windows 10(10.0; Build 10240)

根据msdn,它支持API.

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

我有创建服务主机的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();
Run Code Online (Sandbox Code Playgroud)

服务合约:

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

public interface IMyServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void OnFrame(int i);
}
Run Code Online (Sandbox Code Playgroud)

我曾尝试两者的ChannelFactory并通过添加服务引用对话框,既WCF客户端生成NetHttpBinding,并NetTcpBinding在在UWP应用.

当我尝试创建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()
Run Code Online (Sandbox Code Playgroud)

小智 1

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

GitHub 上报告了一个关于 WCF 双工实现中反射的错误使用的错误。此错误已在 .net core 的最新版本中修复,您可以包含Nuget gallery 中的单个包。但是,此包要求您还包含 System.Runtime 和 System.Threading 的预发行版本。

在此输入图像描述

希望能帮助到你,