如何在Windows通用应用程序中使用双工WCF服务

6

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

当尝试在针对Windows 10(10.0; Build 10240)的Windows通用应用程序中使用双工wcf服务时,我会得到“PlatformNotSupportedExcetpion:此平台不支持操作。”运行时异常。

根据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应用程序中使用NetHttpBinding和NetTcpBinding。
当我尝试创建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错误。 - jsanalytics
我在这个特定解决方案中有23个项目,它们都使用AnyCPU平台,除了新创建的UWP项目,它只接受/允许x86x64平台,但不支持AnyCPU。所以,我想问题就出在这里。我尝试过手动添加AnyCPU到项目文件中...但当然没成功。再次强调,在Windows 8.1下没有任何问题,这可能与UWP项目模板或类似的问题有关。 - jsanalytics
另一个线程中有人指出,在移除CallbackContract属性后,UWP客户端可以创建连接,因此基本的WCF可以工作。然后他在尝试在UWP中创建双工WCF客户端时遇到了困难。 - Rami Sarieddine
1
同样的问题。 - 翔 羽
2个回答

4
上个月,作为.NET Core 1.0的一部分,WCF的稳定版本刚刚发布。通过在UWP项目的project.json文件中引用Microsoft.NETCore.UniversalWindowsPlatform软件包的5.2.2版本,可以支持双工和许多其他WCF功能的Windows通用应用程序。请参考WCF features

1

即使在最新的.NET Core v5.1.0版本中,也不支持复工作场景。

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

enter image description here

希望它能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接