【问题标题】:WCF client in a universal app通用应用程序中的 WCF 客户端
【发布时间】:2015-10-09 21:00:44
【问题描述】:

是否可以从通用应用程序调用 WCF 服务?

我添加了一个服务引用,并且代理生成得很好。 但是,当以编程方式创建 NetTcpBinding 并将其传递给代理的构造函数时,服务模型会抛出异常 PlatformNotSupported。

在模拟器和本地机器上运行应用程序都会产生相同的异常。

发生“System.PlatformNotSupportedException”类型的异常 在 System.Private.ServiceModel.dll 中但未在用户代码中处理

“不支持此操作”

EndpointAddress address = new EndpointAddress("net.tcp://test:9000/ServicesHost/PublishService");
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;

PublishingService.PublishClient proxy = new PublishingService.PublishClient(binding, address);

有人有 UAP 中工作 WCF 客户端的示例吗?

编辑

这与服务是双工服务有关!

原始合同:

[ServiceContract(CallbackContract = typeof(IPublishCallback))]
public interface IPublish { }

删除 CallbackContract 属性后,UAP 客户端可以创建连接,因此基本的 WCF 可以正常工作。 所以我想最好改写这个问题。 是否可以在通用应用程序中创建 duplex WCF 客户端?

编辑主机的服务模型

<system.serviceModel>
    <bindings>
        <netTcpBinding>         
            <binding name="netTcpPublishService" openTimeout="00:00:10" receiveTimeout="infinite">
                <reliableSession inactivityTimeout="24.20:31:23.6470000" enabled="true" />
                <security mode="Transport">
                    <transport clientCredentialType="Windows" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>  
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehaviour">      
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="serviceBehaviour" name="PublishService.Publish">
        <endpoint binding="mexHttpBinding" name="mexPublishService"
          contract="IMetadataExchange" />
        <endpoint address="PublishService" binding="netTcpBinding" bindingConfiguration="netTcpPublishService"
          name="netTcpPublishService" contract="PublishService.IPublish" />   
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8004/ServicesHost/PublishService" />
            <add baseAddress="net.tcp://localhost:9004/ServicesHost/PublishService" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

【问题讨论】:

  • 应该可以,我已经在我的应用程序中完成了(虽然没有向代理构造函数传递任何东西,所以可能不适用)。
  • 这是在 Windows 10 UWP 应用程序中吗?

标签: c# .net wcf win-universal-app


【解决方案1】:

是的,这是可能的。这是我在不久前做的示例应用程序中的连接方式:

 using Tradeng.Srvc.Client.WinAppSimple.SrvcRefTradeng;

 private InstanceContext instanceContext;
 private TradengSrvcClientBase serviceProxy;

            instanceContext = new InstanceContext(this);
            serviceProxy = new TradengSrvcClientBase(instanceContext);

            bool result = await serviceProxy.ConnectAsync();

            if (result)
            {
              // connected...
            }

我使用了在您添加对服务的引用时生成的配置文件中的绑定。

这就是应用程序的外观。尖端的东西.... :O)

https://www.youtube.com/watch?v=YSg6hZn1DpE

顺便说一句,服务本身作为WebRoleAzure 上运行。

【讨论】:

  • 如果可能,请发布完整的堆栈跟踪。可能有一些内部异常可以更好地提示问题。
  • 这是完整的堆栈跟踪吗? PlatformNotSupportedException去哪儿了?
  • 一些事情:1)我希望有一个干净的异常链导致PlatformNotSupportedException,这不是你所展示的。 2)您正在手动创建代理,这不是必需的,如果没有别的,只会让事情变得比他们真正需要的更难。您所要做的就是转到您的项目和Add Service Reference,然后在您的代码中添加using 语句,请参阅我更新的帖子。 3) 使用 WCF 时,必须启用服务端跟踪。并且还使用服务跟踪查看工具。它以令人痛苦的细节向您展示正在发生的事情。
  • 关于您的编辑,我的服务也是双工的,所以我怀疑这会是问题所在。您如何托管服务?如果您在 IIS 下托管,则必须将其设置为使用 net.tcp 协议。
  • 我的服务参考生成了 5 个不同的构造函数。您是否在服务主机的配置文件中正确设置了元数据端点以及元数据交换 (mex) 行为?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 2012-11-21
相关资源
最近更新 更多