【问题标题】:WCF Service(NetTcpBinding) + host on managed WPF applicationWCF 服务(NetTcpBinding)+ 托管 WPF 应用程序上的主机
【发布时间】:2019-07-17 14:40:40
【问题描述】:

我想用 WCF 服务创建一个 WPF 应用程序来监听某个端口。我创建 C# CLass Library(UfebsSignServiceLibrary) 并添加到 WCF 服务中。在库中之后,我添加了接口和类实现器。 我创建了 WPF 应用程序并希望在其中托管我的 WCF 服务。我在我的 WPF APP 配置 app.config 中添加了对 UfebsSignServiceLibrary 的引用。 WPF 已启动,但当我尝试在 WCF 服务上添加服务引用时出现错误 - 元数据包含无法解析的引用。

我已经尝试启用“httpGetEnabled”、“httpsGetEnabled”并为 http 协议添加了 baseAddress ()。我阅读了许多关于同一问题的主题,但我没有找到适合我情况的答案。

带有 WCF 服务代码的我的类库:

namespace UfebsSignServiceLibrary
    {
        [ServiceContract]
        public interface IUfebsSignService
        {
            [OperationContract]
            byte[] SignData(byte[] data, string profileName);
        }

namespace UfebsSignServiceLibrary
{
    public class UfebsSignService : IUfebsSignService
    {
        public byte[] SignData(byte[] data, string profileName)
        {
            // Some Code
        }
    }

我的 WPF 应用程序代码:

public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
                using (host = new ServiceHost(typeof(UfebsSignService)))
                {
                    host.Open();
                }

                Stop.IsEnabled = true;
                Start.IsEnabled = false; 
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            host.Close();
            Stop.IsEnabled = false;
            Start.IsEnabled = true;
        }

App.config 代码(wpf 应用程序):

<system.serviceModel>

    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata  /> <!--httpGetEnabled="false"-->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior" name="UfebsSignServiceLibrary.UfebsSignService">
        <host>
          <baseAddresses>
            <!--<add baseAddress="http://localhost:8080/UfebsSignService" />-->
            <add baseAddress="net.tcp://localhost:8585/UfebsSignService/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" contract="UfebsSignServiceLibrary.IUfebsSignService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />   
      </service>
    </services>
  </system.serviceModel>

URI 前缀无法识别。 元数据包含无法解析的引用:“net.tcp://localhost:8585/UfebsSignService/”。 无法连接到 net.tcp://localhost:8585/UfebsSignService/。连接尝试持续了 00:00:02.0140574 的时间跨度。 TCP错误代码10061

【问题讨论】:

    标签: c# wpf wcf


    【解决方案1】:

    代码sn-ps有个小问题。当我们使用 USING 语句时,它会自动释放托管资源。请考虑使用以下代码启动服务主机。

    host = new ServiceHost(service);
    host.Open();
    

    如果问题仍然存在,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多