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